-->
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.  [ 7 posts ] 
Author Message
 Post subject: Bad behavior reading collections
PostPosted: Tue May 23, 2006 9:10 am 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Let's say I have a simple parent/child relationship in 2 tables. Imagine the parent table has 4 rows and the child table has dozens of rows. When I use nhibernate to read the object graph, it issues 5 select statements - 1 to read the parent rows and then 1 each to read the children for each of the parents!

This is inefficient... there should only be 2 select statements and 2 round trips to the database. One to read the parent rows, and one to read all children of all parents (using a simple "in" clause). Then loop through the children, assigning them to the proper parent.

So if I have hundreds of parent rows, nhibernate is going to sit there making hundreds of round trips to the database. I don't understand this, I doesn't really give me a good feeling about what happens under the covers when I use this O/R solution.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 2:34 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Nobody has any comment on this, I guess it is no big deal that nhibernate thrashes your database?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 3:42 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
grennis wrote:
I guess it is no big deal that nhibernate thrashes your database?

This kind of remarks isn't going to encourage anybody to help you.

NHibernate allows you to load parents and children more efficiently. Read the documentation on batch fetching and querying.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 6:10 pm 
Newbie

Joined: Fri Dec 09, 2005 2:22 pm
Posts: 16
I have the same problem, trying to use batch or fetch didnt help.

Could you give us some hints for the basic example above ? (I do not support the above aggressiveness either :-))


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 6:50 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You can map the Children collection of Parent with batch-size="4" for example. Or you can use a query like "from Parent p join fetch p.Children where p in (:someIds)". If you have trouble with it, post your code, mappings, and possibly also logs.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 25, 2006 8:56 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
Thank you for the help. Your answer was helpful and did address my concern.

After examining the log4net output, I was wondering, with all due respect, why not concat a string with an IN clause of the parent ID's? It seems this would be much more speedy than using a hundred named parameters all combined with OR statements.

But, I am still just a beginner so I'm not sure if there is a reason for this (i.e., the dialect?)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 26, 2006 6:53 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Multicolumn primary keys would have to be handled using "... OR ... OR ..." style anyway, that's why IN is not used. And concatenating SQL is bad in general, so parameters are used. But I didn't write the code myself, so these all are just my educated guesses.


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