0

I am using JPA's(Entity Manager) Criteria query to fetch results from database. I need to put 'OR' clause between the criterias.

My code is as below:

List<Predicate> criteraList = new ArrayList<Predicate>();
if(user.getEid() != null){
    criteraList.add(criteriaUserCountry.like((Expression) userList.get("userCountryPK").get("user").get("eid"), "%" + user.getEid() + "%" ));
}
if(user.getFirstName() != null) {
    criteraList.add(criteriaUserCountry.like((Expression) userList.get("userCountryPK").get("user").get("firstName"), "%" + user.getFirstName() + "%" ));
}
cq.where(criteraList.toArray(new Predicate[]{}));
TypedQuery<UserCountry> query = entityManager.createQuery(cq);

In terms of sql query, the above code inserts 'AND' between different criterias. How can an 'OR' clause be achieved using Entity Manager's criteria.

perissf
  • 15,979
  • 14
  • 80
  • 117
Heena Ghai
  • 63
  • 1
  • 3
  • possible duplicate of [How to generate a predicate array of OR statements](http://stackoverflow.com/questions/18226800/how-to-generate-a-predicate-array-of-or-statements) – perissf Aug 19 '13 at 09:36

1 Answers1

1

Use the OR predicate

Note that since it has an overload with varargs, you can pass a Predicate[] to it.

SJuan76
  • 24,532
  • 6
  • 47
  • 87