Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CriteriaBuilder.and & CriteriaBuilder.or, how-to?

I'm trying to change the following HQL to use JPA Criteria:

select distinct d from Department d 
left join fetch d.children c 
where d.parent is null 
and (
    d.name like :term 
    or c.name like :term
    ) 
order by d.name

Department has a Set<Department> of children.

Criteria:

CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Department> c = cb.createQuery(Department.class);
Root<Department> root = c.from(Department.class);
root.fetch("children", JoinType.LEFT);
Path<Department> children = root.join("children", JoinType.LEFT);
c.orderBy(cb.asc(root.get("name")));
c.distinct(true);
c.where(cb.isNull(root.get("parent")));
String param = "%" + "term" + "%";
cb.and(cb.like(root.<String> get("name"), param));
cb.or(cb.like(children.<String> get("name"), param));

TypedQuery<Department> tq = getEntityManager().createQuery(c);
departments = tq.getResultList();

I know it can be a bit terse, however, the HQL returns 24 and Criteria version 28. I think I'm not handling:

and (x = y OR z = y)

correctly. Any pointers will be greatly appreciated. Thanks.

like image 409
kmansoor Avatar asked Aug 05 '13 23:08

kmansoor


People also ask

What is the use of CriteriaBuilder?

Interface CriteriaBuilder. Used to construct criteria queries, compound selections, expressions, predicates, orderings. Note that Predicate is used instead of Expression<Boolean> in this API in order to work around the fact that Java generics are not compatible with varags.

What is CriteriaBuilder in JPA?

CriteriaBuilderJPA interfaceUsed to construct criteria queries, compound selections, expressions, predicates, orderings. See JavaDoc Reference Page... interface serves as the main factory of criteria queries and criteria query elements. It can be obtained either by the EntityManagerFactory. persistence.

What is predicate in Criteria API?

javax.persistence.criteria The type of a simple or compound predicate: a conjunction or disjunction of restrictions. A simple predicate is considered to be a conjunction with a single conjunct. Return the top-level conjuncts or disjuncts of the predicate.

Is Criteria API deprecated?

The Criteria API allows us to build up a criteria query object programmatically, where we can apply different kinds of filtration rules and logical conditions. Since Hibernate 5.2, the Hibernate Criteria API is deprecated, and new development is focused on the JPA Criteria API.


1 Answers

Here's the where clause of your JPQL query:

where d.parent is null 
and (
    d.name like :term 
    or c.name like :term
    ) 

The where clause contains two predicates:

d.parent is null 

and

(d.name like :term 
 or c.name like :term)

The second predicate is an or containing two predicates:

d.name like :term

and

c.name like :term

So you need the same structure in your criteria query:

Predicate orClause = 
    cb.or(cb.like(root.<String>get("name"), param),
          cb.like(children.<String>get("name"), param));

c.where(cb.isNull(root.get("parent")),
        orClause);
like image 165
JB Nizet Avatar answered Oct 29 '22 01:10

JB Nizet