Although the answer provided by johncarl was accepted, it does not look right for me. JavaDocs for CriteriaQuery.where () say:
Modify the query to restrict the query result to match the specified boolean expression. Replaces previously added constraint (s), if any.
As I understand it, each of the following lines (with restrictions) will override the restrictions specified earlier:
criteria.where(builder.like(langJoin.get(Language_.locale), locale)); criteria.where(builder.like(pRoot.get(Person_.name), name)); criteria.where(builder.between(pRoot.get(Person_.time), startDate, endDate));
This means that at the end there will be only the last restriction (between the start and end dates).
I suggest the following modifications to johncarl's answer:
CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Person> criteria = builder.createCriteria(Person.class); Root<Person> pRoot = criteria.from(Person.class); Join<Person, Language> langJoin = criteria.join("language", JoinType.LEFT); Predicate[] restrictions = new Predicate[] { builder.like(langJoin.get(Language_.locale), locale), builder.like(pRoot.get(Person_.name), name), builder.between(pRoot.get(Person_.time), startDate, endDate) }; criteria.where(builder.and(restrictions)); criteria.orderBy(builder.asc(pRoot.get(Person_.name)));
However, this code looks really ugly! Please feel free to edit if it is wrong, and comment on it if you see a better solution! I would be graceful!
source share