How can I do Criteria Query in JPA2, which is equivalent to this JPQL with and without using metamodel?
SELECT p FROM Employee e JOIN e.phones p WHERE e=:empl
How can I do Criteria Query in JPA2, which is equivalent to this JPQL with and without using metamodel?
SELECT p FROM Employee e JOIN e.phones p WHERE e=:empl
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Phone> criteriaQuery = cb.createQuery(Phone.class);
Root<Employee> employee = criteriaQuery.from(Employee.class);
CollectionJoin<Employee, Phone> phone = employee.join(Employee_.phones);
criteriaQuery.where(cb.equal(employee, empl);
criteriaQuery.select(phone);
TypedQuery<Phone> query = em.createQuery(criteriaQuery);
List<Phone> phones = query.getResultList();
That said, I see no point in replacing a simple, obvious JPQL query with the horrendous, unreadable lines of code above. Criteria queries are useful to dynamically construct queries, but JPQL shines for static queries like the one you have.