-->
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: Special Case of n+1 Selects Problem (chapter 7.6.1)
PostPosted: Fri Nov 05, 2004 7:11 am 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
If I translate my problem to the example in the book, I'd like to retrieve the 10 most recently created Items together with their bids (in order to display all bids or find out the maximum bid or whatever).

If I implement it like this...

Code:

int numberOfResults = 10;

List results = session.createCriteria(Item.class)
  .addOrder(Order.desc("created"))
  .setMaxResults(numberOfResults)
  .setFetchMode("bids", FetchMode.EAGER)
  .list();



...I've got the problem that only 10 non-distinct result sets are returned. If there are 10 Bids for each Item, I'd only get 1 Item after making the results distinct.

Since I don't have any idea how many Bids each Item has, I have to fetch all items. Or am I wrong?

I would wish that Hibernate would somehow do the magic and make the results distinct for me. What's the point of getting non-distinct results anyway? I could formulate statements like "SELECT i, b FROM Item i, Bids b WHERE b.item = i" if I really want a more join-like result.

Regards,

Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 06, 2004 2:51 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The limit is applied to the eager outer join fetch query, so you don't get the "correct" result from an object graph point of view. It is however correct in SQL and also a reason why non-distinct objects are returned. Both views are valid, but we will change this in Hibernate3 (we'll return distinct objects, but then maxresult/firstresult will be even more difficult to understand). Your last statement is a simple inner join, but don't forget that your fetch query is also a join, left outer.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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.