我有这些测试查询:
String query = "";
Query criteria;
query =
"from Test t " +
"where (:value is null OR t.id = :value)"; // update: (t.id = (:value))
criteria = session.createQuery(query);
if (.. some condition ..) {
criteria.setParameter("value", 0);
}
else {
criteria.setParameter("value", null);
}
如何仅使用id = 0
参数将where条件从(id = 0 or id = 1)
更改为id < 2
(resp。value
)?
答案 0 :(得分:1)
您可以使用“where in”运算符。然后“值”将成为收集参数。
Query criteria;
query =
"from Test t " +
"where (t.id in (:value))";
criteria = session.createQuery(query);
criteria.setParameterList("value", Arrays.asList(0, 1));