开发者

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开发者_如何学JAVA)));
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.<List<Project>>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);
Path<List<Project>> projects = emp.get("projects"));
cq.where(cb.isEmpty(projects);
cq.select(emp);

Reference

  • javax.persistence.criteria API
    • <Y> Path<Y> Path.get(String):
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜