PDA

View Full Version : Why do I get NHibernate.LazyInitializationException?


Tommy_Shen
12-01-2006, 07:35 AM
hi, all
I have the following code:

[Test]
public void TestEditMember()
{
Member member = memberDao.Get(9L);
member.Certifications.Clear(); // caught LazyInitializationException
member.Certifications.Add(certificationDao.Get(1L) );
memberDao.Save(member);
//Certification cert = certificationDao.Get(1L);
//memberDao.AddCertification(9L, cert); it's ok.
}

Member.hbm.xml snippet:

...
<set name="Certifications" table="member_certification" lazy="true" inverse="false">
<key>
<column name="member_id" not-null="true"/>
</key>
<many-to-many class="Certification" column="certification_id"/>
</set>
...

Certification.hbm.xml snippet

....
<set name="Members" table="member_certification" lazy="true" inverse="true">
<key>
<column name="certification_id" not-null="true"/>
</key>
<many-to-many class="Certification" column="member_id"/>
</set>
....


entity relation is the normal many-to-many case.

when I call member.Certifications.Clear, I caught the following exception:

ERROR NHibernate.LazyInitializationException - Failed to lazily initialize a collection - no session
NHibernate.LazyInitializationException: Failed to lazily initialize a collection - no session


I used the TransactionProxyFactoryObject for my transaction management, can anybody help me? thanks.

PS: these codes in java worked well.

Tommy_Shen
12-04-2006, 02:47 AM
does anybody get this exception? I have looked the log file:

2006-12-01 12:57:19,640 [1] DEBUG NHibernate.Impl.SessionImpl [(null)] - initializing non-lazy collections
2006-12-01 12:57:19,640 [1] DEBUG NHibernate.Loader.Loader [(null)] - done entity load
2006-12-01 12:57:19,640 [1] DEBUG Spring.Data.NHibernate.HibernateAccessor [(null)] - Not closing pre-bound Hibernate Session after HibernateTemplate
2006-12-01 12:57:19,640 [1] DEBUG NHibernate.Transaction.AdoTransaction [(null)] - rollback
2006-12-01 12:57:19,640 [1] DEBUG NHibernate.Transaction.AdoTransaction [(null)] - running AdoTransaction.Dispose()
2006-12-01 12:57:19,640 [1] DEBUG NHibernate.Impl.SessionImpl [(null)] - transaction completion
2006-12-01 12:57:19,656 [1] ERROR NHibernate.LazyInitializationException [(null)] - Failed to lazily initialize a collection - no session
NHibernate.LazyInitializationException: Failed to lazily initialize a collection - no session
2006-12-01 12:57:19,656 [1] DEBUG Spring.Data.NHibernate.Support.OpenSessionInViewMo dule [(null)] - Closing single Hibernate Session in OpenSessionInViewFilter
2006-12-01 12:57:19,656 [1] DEBUG Spring.Data.NHibernate.SessionFactoryUtils [(null)] - Closing Hibernate Session
2006-12-01 12:57:19,656 [1] DEBUG NHibernate.Impl.SessionImpl [(null)] - closing session
2006-12-01 12:57:19,656 [1] DEBUG NHibernate.Impl.SessionImpl [(null)] - disconnecting session
2006-12-01 12:57:19,656 [1] DEBUG NHibernate.Connection.ConnectionProvider [(null)] - Closing connection
2006-12-01 12:57:19,656 [1] DEBUG NHibernate.Impl.SessionImpl [(null)] - transaction completion



the log file maybe tells: that OpenSessionInViewModule had being work, it can keep the session be opened until the end of the request in view. but, it seems that it can't work fine with the collections lazy loading.

but, how java can do this?

Mark Pollack
12-04-2006, 04:19 AM
Hi,
Sorry I am quite busy getting the next release out. I'll have to look at this later in the week. My apologies.
Mark

Tommy_Shen
12-11-2006, 10:02 AM
hi mark
has this problem been solved?

jotache
12-28-2006, 10:30 PM
hi mark
has this problem been solved?

HI TOMMY I HAVE SAME PROBLEM, DID YOU FOUND THE RESPONSE ?

Mark Pollack
12-28-2006, 11:53 PM
Hi,

My apologies, I actually forgot about this question. In the unit test code OpenSessionInView module isn't in use, so the tx/session are closed together as the unit of work after the dao method is finished. I'm not sure looking at the log file why there is a NH rollback.

Another thread has a rough impl of a session scope, that would help here, but the real answer in a testing environment is to have a [TransactionalTest] attribute. I realize there are quite a few threads related to these lazy collection issues....so until a more "elegant" solution is found, you can wrap your test code inside an anonymous delegate of TransactionTemplate. (assuming .net 2.0 of course) The ref docs have an example.

Mark