-->
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.  [ 5 posts ] 
Author Message
 Post subject: Using left joins
PostPosted: Thu Apr 26, 2007 6:15 am 
Newbie

Joined: Mon Mar 26, 2007 5:26 am
Posts: 10
Hibernate version:3.2

Hey all

I'm having some difficulties with a HQL statement. I am trying to fetch a collection for a movie with the following query:

Code:
from Movie as m
   inner join fetch m.movieViewStatistic
   inner join fetch m.user
   left join fetch m.comments
   where m.isActive = 1 and
   m.normalizedTitle = ?


The problem is that it will return a list of 56 movies instead of just one unique result. The movie in question has 56 comments so I'm guessing Hibernate returns one Movie for each Comment the movie has. If I leave out the "left join fetch m.comments" I will receive a unique result. The list of the 56 movies all have their comments initialized so it is fetching the collection properly, but why is it returning 56 results?

mapping:

Code:
Movie.hbm.xml
<set name="comments" cascade="all" order-by="created desc">
      <key column="movie_id" />
      <one-to-many class="com.myapp.model.MovieComment" />
</set>

MovieComment.hbm.xml
<many-to-one
      name="movie"
      column="movie_id"
      not-null="true"
   />


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 6:23 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
You don't actually need the join to Comments in that statement. Hibernate will magicaly do it for you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 7:27 am 
Newbie

Joined: Mon Mar 26, 2007 5:26 am
Posts: 10
no actually it doesn't. You'll need the fetch statement for Hibernate to initialize the collection, when left out it will return the comments as a separate collection alongside the movie.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 10:30 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
With lazy-loading (the clue's in the name), I'm pretty sure your collection of comments will be got when you call a movies .getComments() method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 12:44 pm 
Newbie

Joined: Fri Apr 20, 2007 3:46 pm
Posts: 15
It is because you are using left outer join. To fix you problem just add the results to a set and make sure that Movie implements equals and hashCode


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