-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: JPA Criteria : LEFT JOIN with an AND condition
PostPosted: Thu Feb 17, 2011 8:01 am 
Newbie

Joined: Mon Sep 24, 2007 9:06 am
Posts: 2
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


Last edited by campi on Thu Feb 17, 2011 10:12 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: JPA Criteria : LEFT JOIN with an AND condition
PostPosted: Thu Feb 17, 2011 8:38 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

Before Hibernate 3.5.0, its impossible:

http://opensource.atlassian.com/project ... e/HHH-2308


Top
 Profile  
 
 Post subject: Re: JPA Criteria : LEFT JOIN with an AND condition
PostPosted: Thu Feb 17, 2011 9:30 am 
Newbie

Joined: Mon Sep 24, 2007 9:06 am
Posts: 2
overmeulen wrote:
Hi,

Before Hibernate 3.5.0, its impossible:

http://opensource.atlassian.com/project ... e/HHH-2308


I am actually using 3.5.1.Final and thanks for the link it's really interesting. My problem is that I am using JPA2 Criteria Builder and the link is concerning Hbernate Criteria.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.