JPA Criteria API: how to express literal true and literal false?
CriteriaBuilder#conjunction() returns a TRUE predicate.
CriteriaBuilder#disjunction() returns a FALSE predicate.
/**
* Create a conjunction (with zero conjuncts).
* A conjunction with zero conjuncts is true.
*
* @return and predicate
*/
Predicate conjunction();
/**
* Create a disjunction (with zero disjuncts).
* A disjunction with zero disjuncts is false.
*
* @return or predicate
*/
Predicate disjunction();
To get an always true Predicate
instance, use criteriaBuilder.and()
.
To get an always false Predicate
instance, use criteriaBuilder.or()
.
Although the other answers solve the problem, I think they don't show the intention of the code, which should be "an always true predicate".
Personally I think the approach below is more clear, although a bit longer.
Always true
criteriaBuilder.isTrue(criteriaBuilder.literal(true));
Always false
criteriaBuilder.isFalse(criteriaBuilder.literal(true));