views:

77

answers:

2

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 ?

+2  A: 

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)

sfussenegger
i'm not using Spring. I use hibernate objects from my ejb v 2.
Attilah
sorry, can't help you with ejb2, but http://www.google.com/search?q=hibernate+"open+session+in+view"+ejb2 should give enough results
sfussenegger
A: 

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.

non sequitor