Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create bogus Predicate in JPA

Is there any way to create bogus Predicate in JPA? Sort of like this:

CriteriaBuilder cb = em.getCriteriaBuilder;
Predicate pred = cb.isTrue(false);

Almost all the methods of CriteriaBuilder take Expression as parameter. I also tried this to no avail:

Expression<Object> path = cb.coalesce.value(null);
Predicate pred = cb.isNotNull(path);

Obviously it throws NPE, however I thought that this might work, because according to API documentation:

A coalesce expression is equivalent to a case expression that returns null if all its arguments evaluate to null, and the value of its first non-null argument otherwise.

like image 949
jFrenetic Avatar asked Sep 05 '11 10:09

jFrenetic


2 Answers

I think a disjunction is what you're looking for. It's false by default, until some real predicate is ored to it.

like image 125
JB Nizet Avatar answered Nov 01 '22 01:11

JB Nizet


dijunction did not work for me in more complicated query in Eclipselink 2.4.

What did was cb.isTrue(cb.literal(false)) which is as precise representation of false as one can get.

like image 30
pdudits Avatar answered Oct 31 '22 23:10

pdudits