CriteriaBuilder boolean comparison

criteria, criteria-api, jpa

Solution

I suppose you implied using `CriteriaBuilder`'s `equal` method. In this case yes, you can use it as follows:

builder.equal(expression, flag);

And this is equivalent to:

if (flag) {
  builder.isTrue(expression);
} else {
  builder.isFalse(expression);
}

But be aware that if you use `Hibernate` as `JPA` provider the former implementation will throw NPE in case `expression==null` is true while the latter one won't.

Problem

I'm currently doing like this. ``` final CriteriaBuilder builder = ...; final boolean flag = ...; if (flag) { builder.isTrue(expression); } else { builder.isFalse(expression); } ``` Can I use it like this? ``` builder.equals(expression, flag); ``` Is this try won't have any problem? Say null for expression or something.

Original source