-->
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: can't figure out how to fetch associated Set
PostPosted: Wed May 03, 2006 4:39 pm 
Newbie

Joined: Thu Dec 11, 2003 3:27 pm
Posts: 12
I thought the following code would return a list containing one Person, with that Person havig his set of addresses populated:

Code:
session.createCriteria(PersonImpl.class).setFetchMode("addresses", FetchMode.JOIN).add(Expression.eq("socialSecurityNumber", theSocialSecurityNumber)).list();


Instead I get as many Persons as there are addresses for that person. That is, instead of getting one Person holding a set of three addresses, I get a list containing three Persons, each with the same identifying information (e.g., id, socialSecurityNumber).

How can I fix the above query to get my desired result?

In case it helps, the definition of the addresses set is:

Code:
      <set name ="addresses"
                inverse = "true"
                 cascade="none"
                 lazy="true">
                <key column ="PERSONID"/>
                <one-to-many class="com.eduction.datamodel.AddressImpl"/>         
      </set>


I'm using hibernate version 3.0.4.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 4:52 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Using direct session.load( PersonImpl.class, serializableId ) will give you a Person object from which you can retrieve set of addresses. I assume socialSecurityNumber to be serializableId i.e. primaryKey for PersonTable. If not you can use HQL version of the query also.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 10:11 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
The "problem" is due to the fact that your addresses are most likely being loaded up with an outer join so for each address you are getting a unique Person/Address combination.

Note that this is what you would get if you executed the raw SQL yourself, Hibernate is simply passing the full result set back to you.

If you call criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) then it will give you a single instance of your root objects when you execute the query.


Top
 Profile  
 
 Post subject: That's the solution
PostPosted: Thu May 04, 2006 2:12 pm 
Newbie

Joined: Thu Dec 11, 2003 3:27 pm
Posts: 12
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) is what I was looking for. It loads Addresses without requiring an extra database hit, and prunes extra copies of the root record.


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.