Reputation: 63
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.
Upvotes: 0
Views: 223
Reputation: 24895
Use the OR
predicate
Note that since it has an overload with varargs, you can pass a Predicate[]
to it.
Upvotes: 1