-->
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: updating detached object
PostPosted: Tue May 18, 2004 12:03 am 
Newbie

Joined: Mon May 17, 2004 11:58 pm
Posts: 2
I tried to save an object loaded in previous session
and hibernate throws nonUniqueObject exception

i have cleared the session (session.clear()) before updating


// in the first session
Cat cat = (Cat) firstSession.load(Cat.class, catId);
// in a higher tier of the application
cat.setMate(potentialMate);
// later, in a new session
secondSession.clear();
secondSession.update(cat); // update cat


but if I load a new instance (with the same identifier),
it works


// in the first session
Cat cat = (Cat) firstSession.load(Cat.class, catId);
// in a higher tier of the application
cat.setMate(potentialMate);
// later, in a new session
Cat cat1 = (Cat) firstSession.load(Cat.class, catId);
cat1.setMate(cat.getMate());
secondSession.update(cat1); // update cat

I dont use versioning.. and using hibernate 2.1.3
can anyone give me a hint ?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 18, 2004 2:48 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
In the second case, Hibernate will give you the SAME instance of Cat.

If cat instances are the same, then this myObject.setMate(myObject);

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: updating detached object
PostPosted: Tue May 18, 2004 2:49 am 
Senior
Senior

Joined: Sun Mar 14, 2004 10:16 am
Posts: 129
Location: Ankara
there is nothing wrong here with hibernate as I see.
After you change the mate property of the cate, u need to update cat object or else your changes can not be reflected to the database.

// in the first session
Cat cat = (Cat) firstSession.load(Cat.class, catId);
// in a higher tier of the application
cat.setMate(potentialMate);
session.update(cat)
// later, in a new session
Cat cat1 = (Cat) firstSession.load(Cat.class, catId);
cat1.setMate(cat.getMate());
secondSession.update(cat1); // update cat
[/img]

_________________
-developer


Top
 Profile  
 
 Post subject: problem solved
PostPosted: Tue May 18, 2004 11:02 am 
Newbie

Joined: Mon May 17, 2004 11:58 pm
Posts: 2
Well.. its not the actual case.. i just use the code in the docs to make it simple

turns out that the error due to unlazy initialization
I load the parent object, hibernate automatically load the children
and then I load another instance of the children..
update it.. boom.. error :)


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.