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]