-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL: Joins with condition and bracket
PostPosted: Wed Apr 16, 2008 8:14 am 
Newbie

Joined: Wed Apr 16, 2008 7:18 am
Posts: 3
Hibernate version: 3.2.6.ga

Hi,
my problem is to write a HQL-query, which finally translates into the following SQL-query:

Code:
    select
        p.name as name,
        cv.mail as mail
    from
        person p
    left outer join (
           contact c
       inner join
           contactvalue cv
               on (c.id=cv.contact_id and
                   cv.type='email')
    ) on p.id=c.person_id

The HQL I have (which gets not the correct result!) is:

Code:
   select p.name, cv.mail
   from Person p
        left join p.contact c
        inner join c.contactvalue cv with (cv.type='email')

Hibernate show me an error, if I add a bracket:

Code:
   select p.name, cv.mail
   from Person p
        left join (p.contact c
        inner join c.contactvalue cv with (cv.type='email'))

The relation between Person and Contact is 1..*
Has somebody an idea, how it could work?
I have testet many different HQLs, but nothing works.
It seems, that Hibernate does not support brackets in joins in any form.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 9:12 am 
Newbie

Joined: Tue Oct 09, 2007 3:09 am
Posts: 6
As far as I known, "joins" in HSQL is somethink another than you expected. What about such HSQL:
Code:
select p.name, cv.mail
from Person p, ContactValue cv
where p.contact.id = cv.contact.id and cv.type = 'email';


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 10:11 am 
Newbie

Joined: Wed Apr 16, 2008 7:18 am
Posts: 3
Thanks for reply, but the Problem is, that not all persons have a contact and your join lists only persons, who have one.
That is the reason, why I have to use an left join.
Futhermore, the condition cv.type='email' has to be a join-condition.
It won't work, if it is within the WHERE-part.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 10:23 am 
Newbie

Joined: Tue Oct 09, 2007 3:09 am
Posts: 6
Hm.. may be this will select correclty:
Code:
select p.name, cv.mail
from Person p, ContactValue cv
where (p.contact.id = cv.contact.id OR p.contact.id IS NULL) and cv.type = 'email';


But I am not sure, in common I tried also using of Hibernate joins, but got lot of problems, that's why I suggested just to use capabilities of WHERE.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 11:29 am 
Newbie

Joined: Wed Apr 16, 2008 7:18 am
Posts: 3
I tried to put the condition into the Where-part, but that is not the same: Not all persons are selected, because the contact.id can exist and the cv.type can be something else than 'email'.
In this case, I still want to select the person but with an empty e-mail (NULL).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 23, 2008 11:45 pm 
Newbie

Joined: Wed Jul 23, 2008 11:39 pm
Posts: 18
You have to create a "one to many" join from Person to contact using the person.id

and then use the following :

select p.name as name, cv.mail as mail
from person p left join p.contacts c
where (c.person_id is null) or (c.person_id is not null and c.cv.type = 'email')
(Assume that you have a many-to-one property from contact to contactvalue, actually your condition cv.type='email' shouldn't exists in the left join condition but in a where clause)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.