HQL:玩变量

时间:2012-08-09 09:07:12

标签: hibernate hql

我有这些测试查询:

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)?

1 个答案:

答案 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));