JPA 2.0 Criteria and grouping of Predicates

北战南征 提交于 2019-12-10 03:57:58

问题


I encounter problem with Hibernate EntityManager 3.5.3-Final when it comes to composite predicates.

Example (not actual code snippet, but the idea should be clear):

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

Now, I would expect something like:

SELECT ... FROM ... WHERE (expression1 AND expression2) OR (expression3 AND expression4)

However I end up with:

SELECT ... FROM ... WHERE expression1 AND expression2 OR expression3 AND expression4

Am I doing something awfully wrong or is it Hibernate issue?


回答1:


According to the SQL specification, both statements mean the same thing. It doesn't matter whether the statement contains the () or not, so Hibernate doesn't use them. The order of precedence is like this, similar to 1+2*3 is the same as 1+(2*3).



来源:https://stackoverflow.com/questions/3382678/jpa-2-0-criteria-and-grouping-of-predicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!