-->
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.  [ 4 posts ] 
Author Message
 Post subject: failed to lazily initialize a collection
PostPosted: Thu Jul 03, 2008 7:35 pm 
Newbie

Joined: Thu Jul 03, 2008 5:13 pm
Posts: 2
Once again a Question relating to the lazy collection exception.

I have a Collection of data which is stored in the database and not needs to get reloaded after loaded once.
I am loading the data via hibernate and holding the resulting Collection in an application bean of an webapplication. Now if iam accessing the items of that collection it causes the following exception:

failed to lazily initialize a collection of role: mypackage.MyClass , no session or session was closed

I dont want to merge each item of the collection to the current session, and i want to avoid copying the loaded objects to none hibernate objects.


MyQuestion:
Is there some possibility to detach once loaded Hibernate Objects from the Session without copying or merge them?


Thanks for any reply


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 10:39 pm 
Newbie

Joined: Wed Jul 02, 2008 8:44 pm
Posts: 14
Location: Zurich, Switzerland
Probably not.
You cannot use lazy loading after closing/detaching from the session you used to originate the lazt collection. So either you use eager fetching to load all elements of the collection at once or try to keep the originating session open at all times, which is probably tricky and I wouldn't recommend anyway as it is an easy way to make a connection leak.

HTH


Top
 Profile  
 
 Post subject: eager fetching
PostPosted: Fri Jul 04, 2008 6:19 am 
Newbie

Joined: Thu Jul 03, 2008 5:13 pm
Posts: 2
So if i use eager fetching for the lazy collection, all fetched objects are loaded at once, and i can access the data later, also if the assigned session is closed already?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 04, 2008 9:38 am 
Newbie

Joined: Wed Jul 02, 2008 8:44 pm
Posts: 14
Location: Zurich, Switzerland
That's correct, the objects are then detached from the session and can be accessed without it.
Only problem I forgot to mention is that the objects are then transient, so if you want to save the changes you make to them later on, you will have to attach them to a session again (refresh them from the database), but that should not be a big problem ^^


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.