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: Help with simple lazy loading
PostPosted: Fri Aug 24, 2007 2:19 am 
Newbie

Joined: Fri Aug 24, 2007 1:55 am
Posts: 2
Hi,

I am a NH beginner user. I am trying to learn about lazy loading for collections. I code up a very simple example, the persistent part seems to work correctly. But for some reason, my collection is not lazily loaded. I have read lots of FAQ and articles, but still don't understand why this is happening. I am using NH 1.2.0 and by default lazy loading should be on. As I said, the code is very simple. Any help will be much appreciated!!

Here's the mapping for the "Parent" class:
Code:
<class name="Person">
    <id name="ID" type="Int32">
      <generator class="assigned"/>
    </id>
    <property name="FirstName" type="String" length="20"/>
    <property name="LastName" type="String" length="20"/>

    <set name="AddressSet" lazy="true">
      <key column="PersonID"/>
      <one-to-many class="Address"/>
    </set>
</class>


And here's the mapping for the "Child" set:
Code:
<class name="Address">
    <id name="ID" type="Int32">
      <generator class="assigned"/>
    </id>
    <property name="Addr" type="String" length="30"/>
</class>

I've marked all the corresponding methods virtual and I am using ISet for the actual 'Set'.

In my code, basically all I am doing is:
1. Open the session from the session factory
2. Begin a new transaction
3. Load the Person obj (Person p = session.Load<Person>(0)). And the Person object with ID 0 does exist in the database.

The Person object is loaded properly, except that the AddressSet collection is loaded together as well (even before I reference it).

I turn on debugging tracing and I see the following: (notice the line 'initializing non-lazy collections'... is that mean somehow NH decides my collection is not a 'lazy collection'?)

Again any help will be much appreciated!! I have been struggling with this for 2 days now... Thanks in advance!!


2007-08-23 22:51:13,562 [1] DEBUG NHibernate.Impl.SessionImpl - resolving associations for: [NHTest.Person#0]
2007-08-23 22:51:13,562 [1] DEBUG NHibernate.Impl.SessionImpl - creating collection wrapper:[NHTest.Person.AddressSet#0]
2007-08-23 22:51:13,562 [1] DEBUG NHibernate.Impl.SessionImpl - done materializing entity [NHTest.Person#0]
2007-08-23 22:51:13,562 [1] DEBUG NHibernate.Impl.SessionImpl - initializing non-lazy collections
2007-08-23 22:51:13,562 [1] DEBUG NHibernate.Loader.Loader - done entity load
2007-08-23 22:51:13,578 [1] DEBUG NHibernate.Impl.SessionImpl - initializing collection [NHTest.Person.AddressSet#0]
2007-08-23 22:51:13,578 [1] DEBUG NHibernate.Impl.SessionImpl - checking second-level cache
2007-08-23 22:51:13,578 [1] DEBUG NHibernate.Impl.SessionImpl - collection not cached
2007-08-23 22:51:13,578 [1] DEBUG NHibernate.Loader.Loader - loading collection: [NHTest.Person.AddressSet#0]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 24, 2007 6:08 am 
Regular
Regular

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

Judging by the log excerpt you posted, the Person class is in fact lazily loaded :
Quote:
2007-08-23 22:51:13,562 [1] DEBUG NHibernate.Impl.SessionImpl - initializing non-lazy collections
2007-08-23 22:51:13,562 [1] DEBUG NHibernate.Loader.Loader - done entity load


But right after this line, you see that NHibernate loads the collection. Check your code, right after the Load() you are referencing the collection one way or another. It might not be that obvious, but I'm pretty sure that it's there (maybe in the ToString() method or something ?).
Note that inspecting the object in a debug session (or by setting a Watch for example) will also trigger the load.

Otherwise, post the code of the Person class along with the code you're using to test this.

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 25, 2007 1:55 am 
Newbie

Joined: Fri Aug 24, 2007 1:55 am
Posts: 2
Hi xasp,

I did think about the debugging issue. I am using VS 2005 and I try to be careful not to "expand" the Person object in the debugger. I know if the debugger expand the Person object, NH is going to load the "children" collections.

But I totally forgot about the "Autos" window/panel... My testing code is like the following:

Code:
Person p = session.Load<Person>(0);
foreach (Address addr in p.AddressSet)
{
    // whatever...
}


I set a break point at the "foreach" line, so I am thinking it shouldn't load the collection because I haven't executed the foreach loop yet... but in fact the "Autos" window is already showing the p.AddressSet, that's why NH is loading the collection.

Thank you very much for reminding me! Everything works out as expected now. I am going to try out the "cascade" setting now.

Cheers,
Joe


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.