Created
April 16, 2018 12:55
-
-
Save ThangLeQuoc/a938bc3754d4e3e190d931c3f2263d2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*SELECT e | |
FROM Employee e | |
WHERE e.id IN (SELECT emp.id | |
FROM Project p JOIN p.employees emp | |
WHERE p.name = :project) | |
*/ | |
CriteriaBuilder cb = em.getCriteriaBuilder(); | |
CriteriaQuery<Employee> c = cb.createQuery(Employee.class); | |
Root<Employee> emp = c.from(Employee.class); | |
Subquery<Integer> sq = c.subquery(Integer.class); | |
Root<Project> project = sq.from(Project.class); | |
Join<Project, Employee> sqEmp = project.join(Project_.employees); | |
sq.select(sqEmp.get(Employee_.id)).where( | |
cb.equal(project.get(Project_.name), | |
cb.parameter(String.class, "project"))); | |
c.select(emp).where( | |
cb.in(emp.get(Employee_.id)).value(sq)); | |
TypedQuery<Employee> q = em.createQuery(c); | |
q.setParameter("project", projectName); // projectName is a String | |
List<Employee> employees = q.getResultList(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment