简体   繁体   中英

Using JPA2 criteria API without Metamodel on a List property

How can I formulate the following JPA2 criteria query without using the metamodel classes:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
Root<Employee> emp = cq.from(Employee.class);
cq.where(cb.isEmpty(emp.get(Employee_.projects)));
cq.select(emp);

I would like to use:

cq.where(cb.isEmpty(emp.get("projects")));

But I cant figure out how to convert the Path to an Expression, which is needed by cb.isEmpty...

Thanks.

Try this:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Employee.class);
Root emp = cq.from(Employee.class);
cq.where(cb.isEmpty(emp.get("projects")));
cq.select(emp);

Or, using a Path variable:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Employee.class);
Root emp = cq.from(Employee.class);
 projects = emp.get("projects"));
cq.where(cb.isEmpty(projects);
cq.select(emp);

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM