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: Working with detached Collections?
PostPosted: Tue Aug 23, 2005 5:35 pm 
Newbie

Joined: Fri Jun 10, 2005 1:52 pm
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.5

Name and version of the database you are using:
Oracle 9i

I would like to know, how in hibernate, can i reattach a detached Collection?
I get a List from an HQL query. The list is then passed to a differenct class. How can i open a session in this class so that I can iterate into the list and not get session closed error?

TIA.


Top
 Profile  
 
 Post subject: Re: Working with detached Collections?
PostPosted: Wed Aug 24, 2005 2:36 am 
Newbie

Joined: Wed Mar 16, 2005 11:06 am
Posts: 10
puks7 wrote:
I would like to know, how in hibernate, can i reattach a detached Collection?
I get a List from an HQL query. The list is then passed to a differenct class. How can i open a session in this class so that I can iterate into the list and not get session closed error?

TIA.


IMHO you have to reattach every single object in the collection using Session#update() or Session#lock().
This is only true for the collections you get by querying your session (like in your case) because the collection (List) returned by Session#list() is not a persistent object but only a collection of persistent objects.

If you have a persistent object A (that has become deattached) with an associated collection, than this collection can be reattached automatically if A is reattached if you use transitive persistence (have the collection mapped with cascade="update").

Here is the link from the doc
http://www.hibernate.org/hib_docs/v3/re ... e-detached

Regards
Sebastian


Top
 Profile  
 
 Post subject: Re: Working with detached Collections?
PostPosted: Wed Aug 24, 2005 10:03 am 
Newbie

Joined: Fri Jun 10, 2005 1:52 pm
Posts: 9
sebbo wrote:

IMHO you have to reattach every single object in the collection using Session#update() or Session#lock().
This is only true for the collections you get by querying your session (like in your case) because the collection (List) returned by Session#list() is not a persistent object but only a collection of persistent objects.



Yea I had tried this a couple of days back, but then it really gets hard to keep track of which object in which list is associated with which session. (assume you had to do this for a couple of lists and they had common persistent objects).

so for now I ve decided to pass the session via which i obtained the list so sessions could be well organized/maintained. i doesnt sound like the best way to do this, so if any of you can suggest a better way to handle a situation like this, i would be very greatfull :)


Top
 Profile  
 
 Post subject: Re: Working with detached Collections?
PostPosted: Wed Aug 24, 2005 10:24 am 
Newbie

Joined: Wed Mar 16, 2005 11:06 am
Posts: 10
puks7 wrote:
sebbo wrote:

IMHO you have to reattach every single object in the collection using Session#update() or Session#lock().
This is only true for the collections you get by querying your session (like in your case) because the collection (List) returned by Session#list() is not a persistent object but only a collection of persistent objects.



Yea I had tried this a couple of days back, but then it really gets hard to keep track of which object in which list is associated with which session. (assume you had to do this for a couple of lists and they had common persistent objects).

so for now I ve decided to pass the session via which i obtained the list so sessions could be well organized/maintained. i doesnt sound like the best way to do this, so if any of you can suggest a better way to handle a situation like this, i would be very greatfull :)


You don't need to know which session a detached object was originally loaded with. You can reattach it to a new session. That's the trick with reattaching!

Code:
Session s1 = ...; // get session from somewhere and begin transaction
List list = s1.createQuery(...).list();
s1.close(); // also commit transaction here
// all objects in list are detached from s1

// the following could happen somewhere in your code
Session s2 = ...; // get another session and begin another transaction
for(Object o : list) {
    s2.update(o); // o is reattached to s2.
    // there are some other other methods to reattach like lock()
    // and merge. Consult the docu
}

// do some work like accessing lazily fetched collections, changing the
// objects, ...

s2.close(); // also commit transaction here
// all objects in list are detached from s2


If you just want to initialize a lazily fetched collection of a detached object, do the following:
Code:
ObjectWithCollection o = ...;
Session s = ...; // get session from somewhere and begin transaction
s.update(o);
Hibernate.initialize(o.getLazyCollection());
s.close(); // also commit transaction here


Regards
Sebastian


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.