本文介绍了JPA 2.0标准和谓词分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 示例(不是实际的代码片断,但是想法应该清楚) : CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); Predicate predicate1 = criteriaBuilder.conjunction(); Predicate predicate2 = criteriaBuilder.conjunction(); //这些是布尔表达式,具有公共根 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问题?解决方案根据SQL规范,这两个语句表示同样的事情。语句是否包含()或不包含,因此Hibernate不使用它们。优先顺序与此类似,类似于1 + 2 * 3与1+(2 * 3)相同。 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 Rootpredicate1.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 expression4Am I doing something awfully wrong or is it Hibernate issue? 解决方案 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). 这篇关于JPA 2.0标准和谓词分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-31 01:14