Java 如何为2种情况(x和Y)或(a和b))设置Hibernate标准
我想使用Hibernate创建的SQL如下:Java 如何为2种情况(x和Y)或(a和b))设置Hibernate标准,java,hibernate,Java,Hibernate,我想使用Hibernate创建的SQL如下: SELECT * FROM tableName WHERE ...(another condition I have already handled here) AND ((billingCompleted = 0 AND terminationDate IS NULL) OR (billingCompleted = 0 AND terminationDate IS NOT NULL)) 到目前为止,我的尝试没有成功: item
SELECT *
FROM tableName
WHERE ...(another condition I have already handled here)
AND ((billingCompleted = 0 AND terminationDate IS NULL)
OR (billingCompleted = 0 AND terminationDate IS NOT NULL))
到目前为止,我的尝试没有成功:
itemCriteria.add(Restrictions.and(
Restrictions.or(
(Restrictions.eq("billingCompleted", new Integer(0)),
Restrictions.isNotNull("terminationDate")),
(Restrictions.eq("billingCompleted", new Integer(0)),
Restrictions.isNull("terminationDate"))))));
我需要如何格式化才能将其设置为适合Hibernate?严格按照您的要求编写,限制是:
Restrictions.and(
Restrictions.or(
Restrictions.and(Restrictions.eq("billingCompleted", 0), Restrictions.isNotNull("terminationDate")),
Restrictions.and(Restrictions.eq("billingCompleted", 0), Restrictions.isNull("terminationDate"))));
但是,您的表达式可以简化为
Restrictions.and(Restrictions.eq("billingCompleted", 0));
因为您希望billingCompleted=0
和terminationDate为null或terminationDate不为null
。最后一个总是计算为true
,X和true==X
,因此我们可以简单地删除该条件