代码:
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
条件中添加谓词的正确方法吗?
答案 0 :(得分:1)
将List<Predicate>
转换为数组Predicate[]
,如下所示:
criteriaQuery.select(criteriaRoot).where(conditionsList.toArray(new Predicate[] {}));