0

How do I write the following MySQL query using JPA Criteria?

select * from deptors where customerId in 
   (select customerId from address where zipcode="12345-6789")
and gender="male";

I have read http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Criteria#Subquery and many stackoverflow threads including JPA 2.0, Criteria API, Subqueries, In Expressions. But I am just not getting it. Thanks for helping.

Community
  • 1
  • 1
kasavbere
  • 5,873
  • 14
  • 49
  • 72

1 Answers1

0

Something like:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Deptors> cq = cb.createQuery(Deptors.class);
Root<Deptors> c = cq.from(Deptors.class);

Subquery<Address> subquery = cq.subquery(Address.class);
Root<Address> a = subquery.from(Address.class);

However I would rather recommend building this query with JPQL and/or externalize it as a NamedQuery into your Entity.

Sebastian

seba.wagner
  • 3,800
  • 4
  • 28
  • 52