当遇到复合谓词时,我遇到了Hibernate EntityManager 3.5.3-Final的问题。
示例(不是实际的代码片段,但想法应该清楚):
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
Predicate predicate1 = criteriaBuilder.conjunction();
Predicate predicate2 = criteriaBuilder.conjunction();
// These are Boolean expression with common Root
predicate1.getExpressions().add(expression1);
predicate1.getExpressions().add(expression2);
predicate2.getExpressions().add(expression3);
predicate2.getExpressions().add(expression4);
//...
query.where(criteriaBuilder.or(predicate1, predicate2));
现在,我希望有类似的东西:
SELECT ... FROM ... WHERE (expression1 AND expression2) OR (expression3 AND expression4)
但是我最终得到了:
SELECT ... FROM ... WHERE expression1 AND expression2 OR expression3 AND expression4
我做错了什么或是Hibernate问题?
答案 0 :(得分:6)
根据SQL规范,两个语句的含义相同。语句是否包含()并不重要,因此Hibernate不使用它们。优先顺序是这样的,类似于1 + 2 * 3与1+(2 * 3)相同。