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.  [ 3 posts ] 
Author Message
 Post subject: Minimise number of queries to load object graph
PostPosted: Sun Aug 26, 2007 9:11 pm 
Beginner
Beginner

Joined: Sun Apr 09, 2006 9:07 pm
Posts: 24
Hibernate version: 1.2.0 GA

Name and version of the database you are using: MS SQL 2000

I have a class (A) with a collection of children (B), which also has a collection of children (C). I would like to load this entire object graph, ie. all instances of A with all child instance B and all grandchild instances C as quickly as possible. Collections of B and C are mapped as bags and are not lazy-loaded.

Currently I'm loading them using code like this:

Session.CreateQuery("from A").List<A>()

This works fine for A, but it then runs a separate queries to load entities B and C for each instance of A. I believe it should be considerably faster to load all instances of A, all instance of B and all instances of C (3 queries in total) and then put them into the appropriate collections. Is there a way to do this with NHibernate? Alternatively, can I write the code to do this myself, but in such a way that NHibernate knows that all the collections have been loaded and does not try to load them again?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 12:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
The docs on fetching strategy here should have all your answer:
http://www.hibernate.org/hib_docs/nhibe ... e-fetching

If you can live with three queries instead of one, sub-select fetching may be your answer. Read the docs for more details.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 11:35 pm 
Beginner
Beginner

Joined: Sun Apr 09, 2006 9:07 pm
Posts: 24
Thanks very much! fetch="subselect" does the trick. I've found I also had to call Session.Evict() on all instances of A and set cascade="all" for A->B and B->C bags, otherwise NHibernate tries to flush all the loaded entities, which wastes quite a bit of time.


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