-->
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.  [ 2 posts ] 
Author Message
 Post subject: changing fetch mode changes List returned by Criteria
PostPosted: Tue Aug 30, 2005 10:46 am 
Newbie

Joined: Thu Apr 28, 2005 8:16 pm
Posts: 9
School:Degree is one:many.

The result of this operation is a list of Schools where each school appears in the list once:

Code:
HibernateUtil.getSession()
.createCriteria(School.class)
.addOrder(Order.asc("name"))
.list()


However, if I set a JOIN fetch mode on the association (to avoid the n + 1 SQL execution problem downstream), then each School appears appears in the list multiple times (the number of occurrences = the number of degrees associated with the school):

Code:
HibernateUtil.getSession()
.createCriteria(School.class)
.setFetchMode("degrees", FetchMode.JOIN)
.addOrder(Order.asc("name"))
.list()


I understand why this is happening, and it is relatively simple to create a Set from the List, but I figured - incorrectly - that Hibernate would automatically deal with the denormalized SQL result set and return a unique list by default. Perhaps something like a criteria.uniqueList method, similar to uniqueResult would, at least in some cases, make changing fetch modes a more brainless operation.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 30, 2005 1:28 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Try

Code:
HibernateUtil.getSession()
.createCriteria(School.class)
.setFetchMode("degrees", FetchMode.JOIN)
.addOrder(Order.asc("name")).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list()


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