Hi,
I'm using a simple one-to-many relationship between two classes. The relation is mapped as a simple set collection, nothing strange.
In the web I'm using the open session in view filter as suggested in the Hibernate in Action book. The problem is that when you load a class in a view, store it in the session context and then try to access the collection, hibernate sometimes throws an exception and sometimes it just loads the collection without problems.
I'm aware of the initialize() method to inject the current session back into the object, but why sometimes it does work ?
Here is the version that does work. Notice how the DefaultInitializeCollectionEventListener intercepts the call.
Code:
[HIBERNATE] 10:06:31 DEBUG DefaultInitializeCollectionEventListener : 42 - initializing collection [imserso.nsi.modelo.Nota.adjuntos#4]
[HIBERNATE] 10:06:31 DEBUG DefaultInitializeCollectionEventListener : 47 - checking second-level cache
[HIBERNATE] 10:06:31 DEBUG DefaultInitializeCollectionEventListener : 59 - collection not cached
[HIBERNATE] 10:06:31 DEBUG Loader : 1426 - loading collection: [imserso.nsi.modelo.Nota.adjuntos#4]
And this is the version that fails:
Code:
[HIBERNATE] 10:08:48 ERROR LazyInitializationException : 19 - failed to lazily initialize a collection of role: imserso.nsi.modelo.Nota.adjuntos - no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: imserso.nsi.modelo.Nota.adjuntos - no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:191)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:183)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:134)
I'm using Hibernate 3.2. I'm trying to understand why in one case it uses the DefaultInitializeCollectionEventListener and in the other it just throws an exception.
Any comment would be really welcome.
Regards,
Juancho