-->
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.  [ 5 posts ] 
Author Message
 Post subject: Foreign keys and lazy loading
PostPosted: Thu Feb 22, 2007 7:52 am 
Newbie

Joined: Thu Feb 22, 2007 7:26 am
Posts: 3
Hi!

I've used Hibernate 2.0 and had a problem with hibernate not being able to lazy load an object's parent objects. For us this lead to a performance problem in a batch-job. Each time the job fetched an object deep down in the entity hierarcy, a large number of statements was issued against the database - since hibernate was loading all the parents, and grand-parent objects. See my example:

Got this hierarchy with one-to-many relationships between the three entities:
ObjectType1
-ObjectType2
--ObjectType3

When fetching an instance of ObjectType3 from the database, it would load the referenced (FK) ObjectType2 and ObjectType1, as well.

The solution was to move to hibernate 3.0 where we could specify that hibernate should lazy-load objecttype2 and objecttype1.

Now I am about to use NHibernate, and as far as I can understand from the documentation, NHibernate 1.0 implments only the features found in Hibernate 2.x (with a few exceptions where it actually implements features found in Hibernate 3.x).

My question is:
Am I correct about the feature not supported in NHibernate 1.0? And If so, the feature will be found in NHibernate 1.2 version?

Can anyone confirm this?


-Jan Ivar


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 10:06 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
This looks to me like some misunderstanding on your part. What you describe has been possible since Hibernate 2 and is possible in NHibernate as well. You have to map the entity class with lazy="true".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 11:45 am 
Newbie

Joined: Thu Feb 22, 2007 7:26 am
Posts: 3
Hi!

When I look at the DTD of Hibernate 2.0 (http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd) it says that the element "many-to-one" does not contain any attribute called lazy. Our experience with Hibernate 2.0 was that this made hibernate load the object referenced by this one-to-many relation in all cases. Configuring entity class with lazy="true" did not change that fact in our case. We may have done somthing else wrong, of course...

In Hibernate 3.0 the DTD (http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd) introduces a new attribute "lazy" on this element in the mapping file, which again makes Hibernate able to lazy load these types of associations (if the relation is modelled as "not null" in the ER-model - or ) using proxy objects. If the relation is nullable, then Hibernate loads the referenced object anyway.

From chapter 19.1.3 in Hibernate 3.0 documentation:
"By default, Hibernate3 generates proxies (at startup) for all persistent classes and uses them to enable lazy
fetching of many-to-one and one-to-one associations."

Of course I can have misunderstood something, but I am pretty sure this is how Hibernate 2.0 and 3.0 is working for this particular feature.

So, if the implementation of NHibernate 1.x confirmes to the 2.0 DTD, then NHibernate 1.0 and 1.2 will not support the feature I am requesting...(?) until NHibernate 3.0 is released...



-Jan Ivar


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 12:21 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Well yes, in Hibernate 2 this was handled by a class-level lazy attribute, not by any individual property. I don't really understand why configuring hte entity class with lazy didn't change the behavior for you, probably it was overridden by some other setting (such as outer-join="true").

NHibernate 1.2 is closer to Hibernate 3 in this area in fact. There is a lazy attribute on many-to-one, and all entities are lazy (proxied) by default, but to repeat, this should have worked fine for you in Hibernate 2 (and thus in NH 1.0) as well.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 12:28 pm 
Newbie

Joined: Thu Feb 22, 2007 7:26 am
Posts: 3
Hi!

Ok - I have to do more testing, then - and see if I can make it work like you say. Thanks for all your help :-).

- Jan Ivar


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