次のjpa基準クエリがあります:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Company> cq = cb.createQuery(Company.class);
Root<Company> root = cq.from(Company.class);
cq.select(root);
Subquery<String> sq = cq.subquery(String.class);
Root<Employee> subroot = sq.from(Employee.class);
sq.select(subroot.get(Employee_.lastName));
Predicate typePredicate = cb.equal(subroot.get(Employee_.lastName), "Doe");
Predicate correlatePredicate = cb.equal(root.get(Company_.employees), subroot);
sq.where(cb.and(typePredicate, correlatePredicate));
cq.where(cb.exists(sq));
TypedQuery<Company> typedQuery = em.createQuery(cq);
List<Company> companies = typedQuery.getResultList();
Eclipselink は次の SQL を生成します。
SELECT t0.ID, ... FROM COMPANY t0
WHERE EXISTS (SELECT t1.LASTNAME FROM EMPLOYEES t2, EMPLOYEES t1
WHERE (((t1.LASTNAME = ?) AND (t1.ID = t2.ID))
AND (t2.COMPANY_ID = t0.ID)))
ご覧のとおり、表 EMPLOYEES に不要な結合があります。この結合を取り除くにはどうすればよいですか?