Reputation: 17930
I have 2 Hibernate objects :
Dero and Motif.
a Dero has a set of Motif.
I load a Dero object from the DB fine. but when I try to access its set of Motif :
assertEquals(dero.getMotifRefus(),deroFromDB.getMotifRefus());
I get an exception :
org.hibernate.LazyInitializationException: failed to lazily initialize a collection
of role: com.dossier.Derogation.motifRefus, no session or session was closed
at
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
How do I solve this ?
Upvotes: 0
Views: 422
Reputation: 18796
The OSIV (Open Session In View) pattern is just that a pattern so you can definitely look at the source code for the Spring implementation and adapt it to your EntityManager (EMs are known as Sessions in Hibernate and EMFs are known as SessionFactories). Also look at this on the Hibernate site and adapt.
Upvotes: 0
Reputation: 36095
It looks like the session that fetched Dero was closed before the lazy set of Motifs was initialized. There are different ways to solve this, depending on your environment and use case.
It your talking about a Spring powered webapp, OpenSessionInViewFilter
would be for you
<filter>
<filter-name>OpenSessionFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
I'd bet that a similar solution is available for any webapp though.
This article might be helpful: Open Session in View (hibernate.org)
Upvotes: 2