查询中的 SetParametres

SetParametres in Query

我正在尝试设置一个包含很多参数的查询。

对于带有 1 个参数的查询,它工作正常:

//**
    List<?> list = null;
    String query = String.format("from %s where %s = :%s ", a, uniqueField[0], uniqueField[0]);
    list = s.createQuery(query).setParameter(uniqueField[0], arg0.getSsn()).list();

我怎样才能对这个查询做同样的事情:

List<?> list = null;
String query = String.format("from %s where %s = :%s and %s = :%s ", a, uniqueField[0], uniqueField[0], uniqueField[1], uniqueField[1]);
// list = s.createQuery(query)... ?

感谢您的任何建议..

使用查询时,您可以使用命名参数或位置参数。命名参数非常简单,我不确定性能影响是什么:

因此,只需命名参数即可:“:param1”、“:param2”。然后 q.setParameter(":param1", value1).setParam(":param2", value2).

参考: 在 JPA 中哪种类型的参数更适合使用“positional/named”?