-->
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.  [ 8 posts ] 
Author Message
 Post subject: Force to load collections of related child objects
PostPosted: Mon Jan 08, 2007 10:52 am 
Newbie

Joined: Mon Jan 08, 2007 10:14 am
Posts: 15
Location: Minsk, BY
I have no idea how I can implement this quite simple task using NHibenate 1.2.0.

I have a business entity Order with a collection of child objects OrderItem.

Usually I use Orders without OrderItems, so I set lazy=true for them. But sometimes I need to load a list of Orders with related OrderItems at once with maximum performance.
How can do it without enumerating of all child objects (to enable lazy loading)? Maybe I can force NHibernate to load all child order items during loading of orders (simulate lazy=false behavior)?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 11:15 am 
Newbie

Joined: Mon Jan 08, 2007 10:29 am
Posts: 3
Hi Almas,

you may do this by setting your lazy option to true, or not specifiing it (since in NHibernate 1.2.0 the lazy option defaults to true).

In your code you load your entity Order like you are used to to.
If you get an order for witch you'll need all the childs you can load them by performing a:
NHibernateUtil.Initialize(Order[x].OrderItems);

regards
Stephan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 11:26 am 
Newbie

Joined: Mon Jan 08, 2007 10:14 am
Posts: 15
Location: Minsk, BY
Thank you, Stephan.
But, should I call NHibernateUtil.Initialize for every order in the list?
Does it produce an additional select of items for every order?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 5:59 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Do NOT call:
NHibernateUtil.Initialize(Order[x].OrderItems);

This will create a SELECT N+1 issue, use eager loading, like this:

session
.CreateQuery("select o from Order o left join fetch o.OrderLines where o.Id = :id")
.SetInt32("id", 5)
.UniqueResult<Order>();

This will load the order with all its OrderLines already populated


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 08, 2007 10:58 pm 
Senior
Senior

Joined: Sat Mar 25, 2006 9:16 am
Posts: 150
If you don't want to use HQL, you can still use Nhibernate.Initialize(...).

The select N+1 problem can be avoided by using the "batch-size" tag, or using the fetch="subselect" mode, which is new in the latest beta.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 6:08 am 
Newbie

Joined: Mon Jan 08, 2007 10:14 am
Posts: 15
Location: Minsk, BY
Ayende Rahien wrote:
Do NOT call:
NHibernateUtil.Initialize(Order[x].OrderItems);

This will create a SELECT N+1 issue, use eager loading, like this:

session
.CreateQuery("select o from Order o left join fetch o.OrderLines where o.Id = :id")
.SetInt32("id", 5)
.UniqueResult<Order>();

This will load the order with all its OrderLines already populated


This query produces a set of duplicated orders which number is proportional to the number of its related order lines. Is it strange behaviour by design?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 8:44 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
It is behavior by design. The order objects are actually just duplicate references, not duplicate objects. Put all of the orders into a Set to remove the duplicates.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 9:03 am 
Newbie

Joined: Mon Jan 08, 2007 10:14 am
Posts: 15
Location: Minsk, BY
Thank you, guys.

Are there any references to a documentation or articles that explain that behavior?


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