Hi all,
I have a question regarding JPA criteria.
Here is my JPA query :
Code:
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
Root<Email> emailRoot = criteriaQuery.from(Email.class);
criteriaQuery.distinct(true);
Predicate globalCondition = criteriaBuilder.equal(emailRoot.get(Email_.type), EmailType.in);
Predicate responseMandatoryCondition = criteriaBuilder.equal(emailRoot.get(Email_.responseMandatory), true);
Predicate typeCondition = criteriaBuilder.notEqual(emailRoot.join(Email_.emailsOut, JoinType.LEFT).get(Email_.responseType),ResponseType.response);
globalCondition = criteriaBuilder.and(globalCondition, responseMandatoryCondition);
globalCondition = criteriaBuilder.and(globalCondition, typeCondition);
em.createQuery(mainCriteria.where(globalCondition)).getSingleResult();
Here is the result of this query :
Code:
SELECT DISTINCT email0_.ID AS col_0_0_
FROM EMAIL email0_
LEFT OUTER JOIN email emailso1_ ON email0_.ID = emailso1_.ID_EMAIL_IN
WHERE email0_.TYPE = 'in'
AND email0_.RESPONSE_MANDATORY = true
AND emailso1_.RESPONSE_TYPE <> 'response'
LIMIT 0 , 30
And here is the request I needed :
Code:
SELECT DISTINCT email0_.ID AS col_0_0_
FROM EMAIL email0_
LEFT OUTER JOIN email emailso1_ ON email0_.ID = emailso1_.ID_EMAIL_IN AND emailso1_.RESPONSE_TYPE <> 'response'
WHERE email0_.TYPE = 'in'
AND email0_.RESPONSE_MANDATORY = true
LIMIT 0 , 30
As you can see I need to check if the RESPONSE_TYPE is equals to 'response' on the same line that the LEFT OUTER JOIN(condition checked before the WHERE). Does anybody know how to write such an JPA criteria query ?
Thanks ,
Louis