JPA 2 + Criteria API

时间:2013-06-24 10:16:29

标签: java jpa criteria criteria-api

Employee (table)
id - int
ctd_id - int
message - char

SELECT a.*
FROM Employee a left outer join 
( select * from Employee where message = 23 ) b
on a.ctd_id = b.ctd_id
where a.message = 22 and b.id is null;

这是我试过的

CriteriaBuilder cb = entityManager.getCriteriaBuilder();

CriteriaQuery<Employee> criteria = cb.createQuery(Employee.class);
Root<Employee> emp = criteria.from(Employee.class); 
CriteriaQuery<Employee> sq = c.select(emp);  

Subquery<Employee> sq2 = criteria.subquery(Employee.class); 
Root<Employee> emp2 = sq2.from(Employee.class); 
Join<Employee,Employee> sqEmp = emp2.join("ctd_id", JoinType.LEFT); 
sq.select(sqemp).where(cb.equal(emp2.get("message"), cb.parameter(String.class, "23"))); 

sq.where(cb.in(path).value(sq2));
TypedQuery<Employee> q = em.createQuery(criteria);
List<Employee> employeess = q.getResultList()

但是,我无法理解我应该如何在带有where子句的子查询中应用连接。

请帮忙。

1 个答案:

答案 0 :(得分:0)

JPA不支持FROM子句中的子查询。

要么使用SQL作为查询,要么重写它不要在from子句中有子查询,它看起来不像你需要它。