I currently have an application running with nHibernate however I am
experiencing problems with lazily loaded collections.

The issue I have is as follows:

1.  I have an entity called "Item".  I load several item objects in
from the database, it has a lazily loaded property called "ExamTypes"
on an item and it is a one to many associate (one item to multiple
exam types).

2.  I have an entity called "Form", which has a one to many property
to "FormDetails" which then has a one to many associated to "Items".
All propertys are lazily loaded.

The problem is in one procedure I load say 3 Item objects.  This seems
to get stored in first level cache (I believe), I do not touch the
lazily loaded ExamTypes property.

Then later on I load a "Form" object, which is associated to 3
"FormDetail" objects and each one of these "FormDetail" objects is
associated to the same 3 "Item" objects I loaded previously.

Now if I access my "Items" object like this:
Form.FormDetail.Item.ExamType I will get a
LazyInitializationException.  This seems to be due to the fact that it
is not actually going to the database for these 3 "Item" entitys and
are just using the cached versions from the first time I loaded them
in a completely seperate procedure.

I've solved this for now by Initializing the 3 item records ExamTypes
property the first time they are loaded.

This increases program complexity though, because I'll have to always
remember when a particular entity is loaded first and ensure all
propertys are loaded by initializing the lazy propertys.  So not only
do I have to know what all the lazy propertys are I have to remember
to initialize them.  An alternative is to Evict them from the cache,
but then I loose the nice caching feature.

How do people handle this?  Why can't the session stay around with the
original 3 items records I loaded previously so that when I do
Form.FormDetail.Item.ExamType it won't throw the
LazyInitializationException??

Here is a snippet of the log:

2010-10-18 08:15:32,969 [4] DEBUG NHibernate.AdoNet.ConnectionManager
[(null)] - aggressively releasing database connection
2010-10-18 08:15:32,969 [4] DEBUG
NHibernate.Connection.ConnectionProvider [(null)] - Closing connection
2010-10-18 08:15:32,969 [4] DEBUG NHibernate.Loader.Loader [(null)] -
total objects hydrated: 0
2010-10-18 08:15:32,969 [4] DEBUG
NHibernate.Engine.StatefulPersistenceContext [(null)] - initializing
non-lazy collections
2010-10-18 08:15:32,969 [4] DEBUG NHibernate.AdoNet.ConnectionManager
[(null)] - after autocommit
2010-10-18 08:15:32,969 [4] DEBUG NHibernate.Impl.SessionImpl [(null)]
- transaction completion
2010-10-18 08:15:32,969 [4] DEBUG NHibernate.AdoNet.ConnectionManager
[(null)] - aggressively releasing database connection
2010-10-18 08:15:32,985 [4] ERROR
NHibernate.LazyInitializationException [(null)] -
Initializing[ItemBankCoreNH.BusinessObjects.Item#RAD]-failed to lazily
initialize a collection of role:
ItemBankCoreNH.BusinessObjects.Item.ExamTypes, no session or session
was closed
NHibernate.LazyInitializationException:
Initializing[ItemBankCoreNH.BusinessObjects.Item#RAD]-failed to lazily
initialize a collection of role:
ItemBankCoreNH.BusinessObjects.Item.ExamTypes, no session or session
was closed
2010-10-18 08:15:33,250 [2] DEBUG NHibernate.Impl.SessionImpl [(null)]
- closing session

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to