Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Criteria API: how to express literal true and literal false?

How can I express literal true and literal false in JPA's Criteria API?

I'm looking for something like Predicate alwaysTrue = CriterialBuilder.DefaultLiterals.TRUE (analogous to java.lang.Boolean.TRUE).

like image 333
Abdull Avatar asked Feb 03 '13 17:02

Abdull


2 Answers

To get an always true Predicate instance, use criteriaBuilder.and().
To get an always false Predicate instance, use criteriaBuilder.or().

like image 98
JB Nizet Avatar answered Oct 06 '22 00:10

JB Nizet


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(); 
like image 38
Patrick Garner Avatar answered Oct 05 '22 23:10

Patrick Garner