查询生成器中的 JPA 2.0 谓词条件列表
JPA 2.0 predicate condition lists in query builder
代码:
List<Predicate> conditionsList = new ArrayList<Predicate>();
Predicate onStart = criteriaBuilder.greaterThanOrEqualTo(criteriaRoot.get("insertDateTime"), startTradeDate);
Predicate onEnd = criteriaBuilder.lessThanOrEqualTo(criteriaRoot.get("insertDateTime"), endTradeDate);
conditionsList.add(onStart);
conditionsList.add(onEnd);
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<MyClass> criteriaQuery =
criteriaBuilder.createQuery(MyClass.class);
Root<MyClass> criteriaRoot = criteriaQuery.from(MyClass.class);
criteriaQuery.select(criteriaRoot).where(conditionsList);
上面的最后一行没有编译,因为它期望 conditionsList
对象是布尔列表而不是谓词列表。
请告诉我将上述谓词添加到 hibernate
标准的正确方法?
将 List<Predicate>
转换为数组 Predicate[]
,如下所示:
criteriaQuery.select(criteriaRoot).where(conditionsList.toArray(new Predicate[] {}));
代码:
List<Predicate> conditionsList = new ArrayList<Predicate>();
Predicate onStart = criteriaBuilder.greaterThanOrEqualTo(criteriaRoot.get("insertDateTime"), startTradeDate);
Predicate onEnd = criteriaBuilder.lessThanOrEqualTo(criteriaRoot.get("insertDateTime"), endTradeDate);
conditionsList.add(onStart);
conditionsList.add(onEnd);
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<MyClass> criteriaQuery =
criteriaBuilder.createQuery(MyClass.class);
Root<MyClass> criteriaRoot = criteriaQuery.from(MyClass.class);
criteriaQuery.select(criteriaRoot).where(conditionsList);
上面的最后一行没有编译,因为它期望 conditionsList
对象是布尔列表而不是谓词列表。
请告诉我将上述谓词添加到 hibernate
标准的正确方法?
将 List<Predicate>
转换为数组 Predicate[]
,如下所示:
criteriaQuery.select(criteriaRoot).where(conditionsList.toArray(new Predicate[] {}));