-->
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 HQL left joins and getting multiple records back
PostPosted: Mon Apr 28, 2008 12:03 am 
Beginner
Beginner

Joined: Sun Oct 14, 2007 7:29 pm
Posts: 23
Hello,

Using Hibernate 3.2 and trying to form HQL left joins to load collection fields along with the target object. My queries are working fine using the where clause, ordering, etc, until I start using the "left join" command and then I start getting multple entries of the same object back in my query results list.

Am I doing something wrong?

This HQL query returns one Usrprofile object as I would expect:
Code:
from Usrprofiles as usrprof where (usrprof.username='johndoe')


as there is only one "johndoe" in my Usrprofile table. However, when I try to also load one of Usrprofiles properties that is an association to another table/class using the left join, I get 6 records back. In this case, I am trying to get the single Usrprofile object for "johndoe" as well as all "Usrmsgs" associated with this user, using the following HQL:

Code:
select usrprof from Usrprofiles as usrprof left join fetch usrprof.usrmsgses  where (usrprof.username="johndoe" and usrprof.usrmsgses.viewed=false) order by usrprof.usrmsgses.logDate desc


All the six objects from the second HQL line are identical.

Any help or thougths you have on this would be a great help.

Thanks,

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 28, 2008 6:54 am 
Newbie

Joined: Sat Oct 28, 2006 6:16 am
Posts: 17
You are not doing anything wrong however you need to be aware of the following:

Quote:
Note that queries that make use of eager fetching of collections usually return duplicates of the root objects (but with their collections initialized). You can filter these duplicates simply through a Set.


This is from http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#objectstate-querying-executing

I hope that helps


Top
 Profile  
 
 Post subject: Using HQL left joins and getting multiple records back
PostPosted: Mon Apr 28, 2008 9:05 am 
Beginner
Beginner

Joined: Sun Oct 14, 2007 7:29 pm
Posts: 23
Thanks for the pointer simon_t. I did find this text in the reference guide:

Quote:
Note that queries that make use of eager fetching of collections usually return duplicates of the root objects (but with their collections initialized). You can filter these duplicates simply through a Set.


How does one do this filtering through a Set? Are we talking about programmatic filtering that compares objects in the returned Set?

Thanks,

-Matt


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 29, 2008 5:55 am 
Newbie

Joined: Mon Apr 28, 2008 4:14 pm
Posts: 5
Hi,

There is method uniquereasult() on Query object. It will filter out unique objects for you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 29, 2008 11:32 am 
Newbie

Joined: Mon Apr 28, 2008 4:14 pm
Posts: 5
Or if you are using Criterias

then use


setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);


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.