Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CriteriaBuilder boolean comparison

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.

like image 832
Jin Kwon Avatar asked Aug 24 '12 06:08

Jin Kwon


1 Answers

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.

like image 134
Oleksandr Bondarenko Avatar answered Sep 24 '22 08:09

Oleksandr Bondarenko