-->
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.  [ 10 posts ] 
Author Message
 Post subject: how to create HQL with LEFT JOIN ... ON ... (MySQL)
PostPosted: Wed May 31, 2006 8:08 am 
Newbie

Joined: Wed Sep 21, 2005 11:28 am
Posts: 4
Location: Croatia
So my problem is how to transform MySql SQL with LEFT JOIN ON to HSQL ?

I work with MySql version that doesn't support subqueries, SQL looks like

Code:
SELECT jpt_user.* FROM jpt_user
LEFT JOIN jpt_tw_user ON jpt_user.id=jpt_tw_user.user_id
WHERE jpt_tw_user.user_id is NULL


-- select all records from jpt_user where jpt_user id is not in (select user_id from jpt_tw_user)

So, this is just how to make NOT IN subquery if your database doesn't support subqueries.

I need to write HSQL for this situation.

Please help and thanks.

Marko


Last edited by pljuska on Wed May 31, 2006 1:38 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 12:24 pm 
Beginner
Beginner

Joined: Tue May 30, 2006 6:03 am
Posts: 20
Location: London
what version of mysql? (i assume you can't upgrade...)

hql should be able to represent it as you showed it in the first part without the not in. alternatively, using criteria should not cause this either.

you should be able to represent it as

SELECT u.* FROM jpt_user u
LEFT JOIN jpt_tw_user tu ON u.id=tu.user_id and tu.user_id = NULL

your second query is doing a different query - not in will give you different results to = null - which are you using, and which is giving you the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 1:33 pm 
Newbie

Joined: Wed Sep 21, 2005 11:28 am
Posts: 4
Location: Croatia
Sorry, I'm new here ... so maybe I wasn't clear enough :-)

No, I can't upgrade mysql :-(

So, this is query in MySQL

Code:
SELECT jpt_user.* FROM jpt_user
LEFT JOIN jpt_tw_user ON jpt_user.id=jpt_tw_user.user_id
WHERE jpt_tw_user.user_id is NULL


that replaces NOT IN subquery ...

What I have to do is to transform that query to HQL.

I hope it is clearer now :-)

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 1:37 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
Not sure what your class names are but another way is:

assuming that jpt_user is a mapped class and has a property for a class jpt_tw_user with that exact name:
and exists a property id in your class jpt_tw_user
Code:
FROM jpt_user u
LEFT JOIN u.jpt_tw_user tu
and tu.user_id = NULL
WHERE tu.id IS NOT NULL


Take a look at :

HQL

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 1:55 pm 
Newbie

Joined: Wed Sep 21, 2005 11:28 am
Posts: 4
Location: Croatia
hm .. it is backwards :-)

I have two mapped classes JptUser and JptTwUser ... JptTwUser has a property for a class JptUser (many-to-one) ... so JptUser has Set of JptTwUser classes.

I tried something like "give me from JptUser where Set of JptTwUser classes is empty" ... but I didn't make it :-(

Marko.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 2:01 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
pljuska wrote:
hm .. it is backwards :-)

I have two mapped classes JptUser and JptTwUser ... JptTwUser has a property for a class JptUser (many-to-one) ... so JptUser has Set of JptTwUser classes.

I tried something like "give me from JptUser where Set of JptTwUser classes is empty" ... but I didn't make it :-(

Marko.


I think like this assuming you mapped the set??:
I am assuming the set is mapped and has a name like
jptTwUsers

Code:

FROM JptUser ju
LEFT JOIN ju.jptTwUsers jtu
WHERE jtu.id IS NOT NULL



This should work (did not test) and return you a set of JptUser where the property id is not null.....

Ok??

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 31, 2006 2:52 pm 
Newbie

Joined: Wed Sep 21, 2005 11:28 am
Posts: 4
Location: Croatia
jbosseur wrote:
Code:

FROM JptUser ju
LEFT JOIN ju.jptTwUsers jtu
WHERE jtu.id IS NOT NULL



This should work (did not test) and return you a set of JptUser where the property id is not null.....

Ok??


Yes, thanks ... only change is that it should be IS NULL instead of IS NOT NULL ... but you helped me a lot and solved my problem ... thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 3:54 am 
Beginner
Beginner

Joined: Tue May 30, 2006 6:03 am
Posts: 20
Location: London
just out of curiosity, surely this will ignore the left join and create an outer join since there's no join specified between the two fields?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 7:58 am 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
rkhanmoh wrote:
just out of curiosity, surely this will ignore the left join and create an outer join since there's no join specified between the two fields?


No it will do a left join ..... You are not working with SQL but HQL you specify the join in ju.jptTwUsers Hibernate will resolve which fields to join from the mapping . Take a look at Associations and joins

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 8:36 am 
Beginner
Beginner

Joined: Tue May 30, 2006 6:03 am
Posts: 20
Location: London
good point, was in my sql mind :s


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