我有一个班级Customer
和CustomerDependant
实体。Customer
与其家属有多对多的双向关系。我需要找到按名称和从属名称过滤的客户。
它在 JPQL 中做了这样的事情:
select c join fetch c.dependants d from Customer c where c.name like
'foo' and d.name like 'foo'
如何使用 JPA 标准查询做同样的事情?
我有一个班级Customer
和CustomerDependant
实体。Customer
与其家属有多对多的双向关系。我需要找到按名称和从属名称过滤的客户。
它在 JPQL 中做了这样的事情:
select c join fetch c.dependants d from Customer c where c.name like
'foo' and d.name like 'foo'
如何使用 JPA 标准查询做同样的事情?
取自 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);
此查询等效于以下 Java Persistence 查询语言查询:
SELECT d
FROM Department d LEFT JOIN FETCH d.employees
WHERE d.deptno = 1
这就是我在没有 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 是一种连接,所以我想你也可以尝试一下。