-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Understanding lazy loading in collections
PostPosted: Mon Dec 04, 2006 6:16 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 5:15 am
Posts: 21
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 2:16 pm 
Newbie

Joined: Tue Sep 19, 2006 4:13 am
Posts: 18
By the exception it is very clear that due to some reason Hibernate identifies the session to be closed and hence when you try to access the collection outside the session it says - Lazyinitiallization exception, - so at that time you only have a proxy.

Anywayz, Could you send your code or some Test case to exhibit this.


Top
 Profile  
 
 Post subject: It is a diffrent session instance for each thread.
PostPosted: Mon Dec 04, 2006 10:47 pm 
Beginner
Beginner

Joined: Thu Nov 02, 2006 2:23 pm
Posts: 33
I think that the problem in the first try is that the same BookCollection arrive at the same time (this is a webservice , so different threds, different session), so when they check if BookCollection is already exists they get false and they both trying to save it at the same time, so only one success (duplicate). i dont know how to solve it.

In the second try the BookCollection is already exists, it get it from the session and do saveOrUpadte to buyer , but failed because StaleStateException , i have no idea why, and how to debug it.

Thank you


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.