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: Should I always be lazy ?
PostPosted: Thu Aug 16, 2007 6:58 pm 
Newbie

Joined: Fri Jul 20, 2007 7:45 pm
Posts: 12
Location: Amgen Corp
Hi,

My mom told me I should never be lazy, but the NHibernate manual says I should nearly always be lazy!

With help from this forum I just persisted my first Object containing a collection implemented as a bidirectional mapping.

So I populated the two tables in the database that underpin the object with lots of fresh data.

I just went to load the Object from the database, and got the Lazy exception because the one-to-many is marked lazy and I closed my session prior to touching the collection from my application.

So being lazy myself, I simply set lazy to false and everything worked.

The NHibernate manual indicates that "it is intended that lazy initialization be used for almost all collections." The manual instructs you on how to handle tricky situations like web-apps and n-tiered apps.

What's so bad about not being lazy if your know at build time that your code is going to touch most of a collection?

Cheers,

_________________
Fred Stann
Solutions Architect


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 16, 2007 7:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Hi Fred, whether you want something to be lazy or not is very situational. Like you said, if you are going to touch those collections nearly every single time, you might as well load them every time. For example, one rarely needs a ShoppingCart without the ShoppingCartItems. On the other hand, you don't always need the CustomerComments on a Product every time; so you would likely want that to be lazy.

There are obviously other factors that affect the "laziness decision"; one is the sizes of the collections. In any case, at the risk of twisting the documentation's authors' words, I think what the doc meant to say is that generally, most collections should be lazy.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 2:53 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

I would like to elaborate on that: karlchu is right when he says that the laziness of a collection/association is very situational.

But will you always be sure of the future requirements of your application ? Or have you at all times a good view on what your application is doing with your object ?

What I'm getting at here is that I do not think it is a good idea to set lazy="false" in your mapping files because you cannot override it in code. This way, you're not closing any doors, you'll have a little more coding to do to fetch the objects you want. Using an ICriteria query (or HQL) you can perfectly fetch your objects with their preloaded collections in code, and at the same time rest assured that, if needed, you still can load your objects without their collections.

The problem I had with lazy="false" is that it's easy to forget you ever set those collections/objects to lazy="false". After a while, you'll get performance problems on seemingly simple queries and only after seeing the SQL NHibernate emits, you'll understand that it's because you turned off lazy loading. And when your application is in production, it is not all too easy to turn on lazy loading on a frequently used object/collection because you always have the risk of getting lazy loading exceptions in places that worked fine until then.

_________________
Please rate this post if it helped.

X.


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.