0

I'd like to add a filtercondition to my criteria query with: cb.and(filtercondition or filterCondition)

so that it's something like:

select * from table t where t.name = 'test' AND (t.place = 'test' OR 'test2');

I've tried the following:

public static Predicate getPredicateForTwoExpressionsAsOrCondition(String filterValue, CriteriaBuilder cb, Expression<String> firstExpression, Expression<String> secondExpression, Predicate filterCondition) {
        Predicate or = cb.or(filterCondition, cb.like(cb.lower(secondExpression), "%" + filterValue.toLowerCase() + "%"));
        return cb.and(or, cb.like(cb.lower(firstExpression), "%" + filterValue.toLowerCase() + "%"));
}

but now it seems as if he's only taking the firstExpression into account.

note that the above method builds on previous filterConditions.. That's why I'm passing it as a parameter.

Using JPA 2.0 and kind of new with the criteria builder.

GregD
  • 1,884
  • 2
  • 28
  • 55

1 Answers1

0

Found it by looking at CriteriaBuilder.and & CriteriaBuilder.or, how-to?

fix:

Predicate or = cb.or(cb.like(cb.lower(secondExpression), "%" + filterValue.toLowerCase() + "%"), cb.like(cb.lower(firstExpression), "%" + filterValue.toLowerCase() + "%"));
return cb.and(filterCondition, or);
Community
  • 1
  • 1
GregD
  • 1,884
  • 2
  • 28
  • 55