[英]JPA Criteria API: How to select property in nested collection
I have a class Customer
and CustomerDependant
entities. 我有一个
Customer
和CustomerDependant
类实体。 Customer
has many to many bi-directional relationship with its dependents. Customer
与其家属有多对多的双向关系。 I need to find customers filtering by name and dependent name. 我需要找到按名称和从属名称过滤的客户。
It's done something like this in JPQL: 它在JPQL中做了类似的事情:
select c join fetch c.dependants d from Customer c where c.name like
'foo' and d.name like 'foo'
How I can do the same thing with JPA Criteria Queries? 我如何使用JPA Criteria Queries做同样的事情?
Taken from JPA Specification section 6.5.4 取自JPA规范第6.5.4节
CriteriaQuery<Department> q = cb.createQuery(Department.class);
Root<Department> d = q.from(Department.class);
d.fetch(Department_.employees, JoinType.LEFT);
q.where(cb.equal(d.get(Department_.deptno), 1)).select(d);
This query is equivalent to the following Java Persistence query language query: 此查询等效于以下Java持久性查询语言查询:
SELECT d
FROM Department d LEFT JOIN FETCH d.employees
WHERE d.deptno = 1
This is what I do it without fetch 这是我没有抓取的做法
CriteriaQuery<Department> q = cb.createQuery(Department.class);
Root<Department> dept = q.from(Department.class);
Join<Department,Employee> emp = d.join(Department_.employees);
q.where(cb.equal(emp.get(Employee_.name),"edalorzo"));
Fetch is a type of join, so I guess you could experiment with that too. Fetch是一种连接,所以我猜你也可以尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.