I\'m trying to do a basic \"OR\" on three fields using a hibernate criteria query.
Example
class Whatever{
string name;
string address;
string pho
//Expression : (c1 AND c2) OR (c3)
Criteria criteria = session.createCriteria(Employee.class);
Criterion c1 = Restrictions.like("name", "%e%");
Criterion c2 = Restrictions.ge("salary", 10000.00);
Criterion c3 = Restrictions.like("name", "%YYY%");
Criterion c4 = Restrictions.or(Restrictions.and(c1, c2), c3);
criteria.add(c4);
//Same thing can be done for (c1 OR c2) AND c3, or any complex expression.
If someone is using CriteriaQuery instead of Criteria, you can put all your expressions in a Predicate
list and put a OR by predicates size like this:
List<Predicate> predicates = new ArrayList<>();
if (...) {
predicates.add(...);
}
criteriaQuery.where(cb.or(predicates.toArray(new Predicate[predicates.size()])));