7

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.

jFrenetic
  • 5,384
  • 5
  • 42
  • 67

2 Answers2

9

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.

pdudits
  • 896
  • 6
  • 9
  • Thanks for sharing your solution. 4 years later, and people still search for that :) Strangely enough, I tested the solution proposed by @JB Nizet on EclipseLink and it worked. But that was long time ago. – jFrenetic Mar 28 '15 at 12:33
9

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

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255