This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Lazy=true not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Lazy=true not working" Watch "Lazy=true not working" New topic

Lazy=true not working

Jaikiran Pai

Joined: Jul 20, 2005
Posts: 9962

We are using Hibernate2.1.3 as a MBean in JBoss-3.2.3. We are running into issues related to lazy loading. We have set lazy=true on the collections of a parent objects. But, even then, when the parent object is loaded, the collection is also getting populated with the child objects. Following is a sample hbm mapping:

When we are loading the com.dbdata.core.hibernateobject.Channel (Parent) object, even the collection "channelInstances" is getting populated with instances of com.dbdata.core.hibernateobject.ChannelInstance.

We did a bit of investigation into this and tried to obtain a Thread dump from one of the setters of the child object, to find out the flow from where this setter of the child is getting called. Following is the output from the Thread.dumpStack() invoked from one of the setters of the child object:

It appears that while loading the parent(through Loader.loadEntity) Hibernate invokes the doQueryAndInitializeNonLazyCollections. We had a look at this code and it looks like it intializes the collection irrespective of whether it is lazy=true or lazy=false. Also, the javadoc of this method does not say anything about the lazy attribute:

* Execute an SQL query and attempt to instantiate instances of the class mapped by the given
* persister from each row of the <tt>ResultSet</tt>. If an object is supplied, will attempt to
* initialize that object. If a collection is supplied, attempt to initialize that collection.

Are we missing something here? The lazy=true is a very fundamental feature and it appears that we are surely missing something in our configuration. Any help is appreciated.

Thank you.

[My Blog] [JavaRanch Journal]
Sebastian Hennebrueder
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
How do you load the channel, query (could you post it) or a session.get/load

Regards Sebastian

Things get always more complicated as expected.<br />Author of eBook Hibernate 3 Developer Guide by example<br />Tutorials about Hibernate, EJB, Struts, JSF <a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: Lazy=true not working
Similar Threads
Many-To-Many problem in Hibernate 3.0 beta 1
one to many mapping not using correct key?
could not initialize a collection+ [Microsoft][ODBC SQL Server Driver]Invalid Descrip
Could not read the data
WrongClassException on joined-subclass