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.  [ 4 posts ] 
Author Message
 Post subject: I miss join+subquery
PostPosted: Mon Nov 24, 2008 11:29 am 
Newbie

Joined: Thu Dec 07, 2006 7:23 am
Posts: 8
Hello

I need to select "movies not rated by Joe"; I have 3 tables: users, movies, ratings.

The SQL would look like this:
Code:
SELECT movies.* FROM movies LEFT JOIN (SELECT ratings.movie_id FROM ratings WHERE ratings.user_id = 123) AS userRatings ON movies.id = userRatings.movie_id WHERE userRatings.id IS NULL;

In HQL following query seems to be the solution:
Code:
from Movie as movie where movie.id not in (select rating.movie.id from Rating as rating where rating.user.id = 123)

Ofcourse this ends up with...
Code:
"...WHERE movie0_.id NOT IN (SELECT rating1_.movie_id..."
...in the generated SQL. Somehow I assume the "NOT IN (SELECT" is not exactly the most effective "join". Am I wrong?

I was not able to google-up (or find in Hibernate "testunit" directory) any HQL-example that would feature left join & subselect in one query; otherwise I would rather use something like:
Code:
from Movie as movie left join (from Rating as rating where rating.user.id = 123) where rating.id is null
...but I just can't make that working.

Can anyone help, plz?
Yaroukh


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 24, 2008 2:58 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Assuming that you have mapped a relation from Movie to Rating something like this (which is similar to your last approach) might work:

Code:
from Movie as movie
left join movie.ratings as rating
left join rating.user user with user.id = 123
where user.id is null


The query looks a bit weird and personally I would probably go for the sub-select variant instead, since it is easier to understand. I don't know if one is better than the other...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2008 10:39 am 
Newbie

Joined: Thu Dec 07, 2006 7:23 am
Posts: 8
Thank you, nordborg, your query seems to work as expected. :)
I'm only beginning (again :) with Hibernate and I did not know the "with" clause.
Thanks a lot!
Yaroukh


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 06, 2008 12:33 pm 
Newbie

Joined: Thu Dec 07, 2006 7:23 am
Posts: 8
As for the IN vs. JOIN:
http://www.bennadel.com/blog/940-SQL-Optimization-Case-Study-JOIN-Clause-vs-IN-Clause.htm


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