-->
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.  [ 12 posts ] 
Author Message
 Post subject: How to make an object detached without session closing?
PostPosted: Wed Sep 06, 2006 9:22 am 
Newbie

Joined: Tue Aug 29, 2006 4:29 am
Posts: 6
Location: St. Petersburg
Is there an another way to detach an object from the session then just closing this session?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 10:51 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
evict()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 11:00 am 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
Image

The above picture is from the "Hibernate in Action" book. It shows how an object can change from one state to another. I shows that a persistent object can move to a detached state by using evict(), close(), or clear().


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 11:11 am 
Newbie

Joined: Tue Aug 29, 2006 4:29 am
Posts: 6
Location: St. Petersburg
Thaks guys,

Nevertheless note that evict method should be used if we are sure that all child objects are marked as "evict" as well. Otherwise we'll get some of them still attached to the session.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 11:26 am 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
Don't forget to rate.

_________________
Michael Masters


Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 7:46 am 
Newbie

Joined: Tue Aug 29, 2006 4:29 am
Posts: 6
Location: St. Petersburg
Have tried the evict method, it didn't help.
Even after Session.clear() I get the exception "Illegal attempt to associate a collection with two open sessions".

I'm trying to copy an object from one database into identical one. I can get an object from session A, iterate it for fully initialization (it is a lazy object) but can't detach it from session A to replicate it into session B. I get the mentioned exception.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 9:50 am 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
Is the first session closed when you introduce the object to the second session? If that's the case, then you may want to see if 2nd level cache is turned on. If the 2nd level cache is on, then you may want to turn it off.

_________________
Michael Masters


Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 9:59 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The second level cache is transparent and has nothing to do with the stuff you guys are discussing here.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 10:08 am 
Newbie

Joined: Tue Aug 29, 2006 4:29 am
Posts: 6
Location: St. Petersburg
So, as far as I understood the Session.evict(obj) and Session.close() stuff won't deatach an object from this session, will it? Do these methods just clear the cash but not deatch objects themselves?

There are many object which are to be iterated and replicated into sessionB, this way I can't close/open session A everytime. Is that really I can't detache objects from session not closing it??


Top
 Profile  
 
 Post subject: use Hibernate.initialize() also
PostPosted: Thu Sep 07, 2006 12:16 pm 
Newbie

Joined: Mon Jan 30, 2006 11:28 am
Posts: 13
Location: Mumbai India
Hi,
Let says u have object b (either came thru query or newly created).
Do following steps to detach the b
Code:
Hibernate.initialize(b);
Code:
sessionA.evict(b);

...
Now b is detached from the sessionA.
Code:
newSession.update(b);
//attach to new session
- Chandra Singh Meena

_________________
-Chandra Singh (Anmsoft)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 12:59 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
mickey2006 wrote:
So, as far as I understood the Session.evict(obj) and Session.close() stuff won't deatach an object from this session, will it? Do these methods just clear the cash but not deatch objects themselves?

There are many object which are to be iterated and replicated into sessionB, this way I can't close/open session A everytime. Is that really I can't detache objects from session not closing it??


Closing the session will detach the object from the session. If changes are made to the object out of the session then they will not be persisted to the database. This is different than if the object was inside of the session. If changes to the object are made inside of a session, then those changes will be persisted to the database when the session is closed/flushed.

For more information:
http://www.hibernate.org/hib_docs/v3/re ... e-overview

_________________
Michael Masters


Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 1:01 pm 
Regular
Regular

Joined: Mon Jun 13, 2005 12:38 pm
Posts: 56
Location: Austin, TX
christian wrote:
The second level cache is transparent and has nothing to do with the stuff you guys are discussing here.


Thanks for clearing this up. I wasn't sure if the 2nd level cache would play a role here.

_________________
Michael Masters


Don't forget to rate.


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