-->
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: Join Fetch returns same entity multiple times
PostPosted: Thu Sep 21, 2006 6:37 am 
Newbie

Joined: Thu Sep 21, 2006 6:14 am
Posts: 4
Hello,

I've got two tables Community and Zipcode with a one to many relationsship (one community may have multiple zipcodes).

I'd like to pre-fetch the zipcodes in order to access them after the session is closed. If I modify the mapping file by setting lazy="false" then it works!

But if I specifiy a Join Fetch explicitely in the Criteria or HQL, then the query will return the same Community entity multiple times. The entities are the same. All of them contain the same associated Zipcodes.

Any ideas what I'm doing wrong?

Thanks.




Hibernate version: 3.1.3

Mapping documents:
<hibernate-mapping>
<class name="Community" table="COMMUNITY" schema="XXX">
<id name="communityId" type="java.lang.Long">
<column name="COMMUNITY_ID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="200" not-null="true" />
</property>
<set name="zipcodes" inverse="true">
<key>
<column name="COMMUNITY_ID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="Zipcode" />
</set>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="Zipcode" table="ZIPCODE" schema="XXX">
<id name="zipcodeId" type="java.lang.Long">
<column name="ZIPCODE_ID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<many-to-one name="basPlace" class="Place" fetch="select">
<column name="PLACE_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
<many-to-one name="community" class="Community" fetch="select">
<column name="COMMUNITY_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
<many-to-one name="supplier" class="Supplier" fetch="select">
<column name="SUPPLIER_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
<property name="zipcode" type="java.lang.String">
<column name="ZIPCODE" length="6" not-null="true" />
</property>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
DetachedCriteria criteria = DetachedCriteria.forClass(Community.class);
criteria.setFetchMode("zipcodes", FetchMode.JOIN);
criteria.addOrder(Property.forName("name").asc());
List<Community> communities = getHibernateTemplate().findByCriteria(criteria);


Name and version of the database you are using: Oracle 10g


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 6:45 pm 
Beginner
Beginner

Joined: Tue Aug 08, 2006 11:53 am
Posts: 37
Hi I am having the same problem. if you found the solution can you please share with me!

thank you in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 13, 2006 4:09 am 
Beginner
Beginner

Joined: Fri Oct 28, 2005 7:11 am
Posts: 20
There is not much Hibernate can do. It is one-to-many relation, and you tell use join-fetch when retrieving Community. So it makes a join to Zipcode in one SQL (look on the sql). This works properly, but the use of join-fetch in this case is not correct here.


Top
 Profile  
 
 Post subject: Re: Join Fetch returns same entity multiple times
PostPosted: Mon Dec 11, 2006 5:39 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
qnob wrote:
DetachedCriteria criteria = DetachedCriteria.forClass(Community.class);
criteria.setFetchMode("zipcodes", FetchMode.JOIN);
criteria.addOrder(Property.forName("name").asc());
List<Community> communities = getHibernateTemplate().findByCriteria(criteria);


Just also call setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) on your criteria instance. This should let each instance only once.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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.