Blaze Persistence skip null parameters in where expression

282 Views Asked by At

is there a way to skip null parameters in where expression? Let's suppose I have the following code:

Boolean deleted = null;
var criteriaBuilder = cbf.create(em, MyClass.class)
                 .where("deleted").eq(deleted);

I would want to skip the evaluation of "deleted" when the deleted variable is null. Is there a way to accomplish this?

Thanks euks

1

There are 1 best solutions below

2
Christian Beikov On BEST ANSWER

This is usually done by conditionally adding the predicate like this:

var criteriaBuilder = cbf.create(em, MyClass.class);
if (deleted != null) {
    criteriaBuilder.where("deleted").eq(deleted);
}