-->
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.  [ 2 posts ] 
Author Message
 Post subject: merge or saveOrUpdate?
PostPosted: Fri Aug 03, 2007 8:49 am 
Newbie

Joined: Tue Jul 24, 2007 10:45 am
Posts: 9
Hi,

I was having a hard time to make hibernate update one object on my application. The process ended up in the following exception being thrown:
Code:
...a different object with the same identifier value was already associated with the session


After looking for answers at this forum, I changed my update method to call getHibernateTemplate().merge(object) instead of getHibernateTemplate().saveOrUpdate(object) and now everything works as expected. No exceptions and the update process is executed.

It works and that's ok, but I'm not sure if this is just a workaround to avoid further problems or it is an acceptable solution. Is there someone here who can shed some light on this?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 04, 2007 1:08 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
I'm going to use these terms, so let me list them first.

transient: never persistent, not associated with any Session
persistent: associated with a unique Session
detached: previously persistent, not associated with any Session

I'm guessing the object you are trying to update is a "persistent" object, i.e. you open a hibernate session, load the object, make changes to it, and call saveOrUpdate before closing the session.

The save operation will try to make a transient object persistent. Since your object is persistent, it won't use this. It will attempt the update instead.

This is a quote from Hibernate's javadoc for update.

"Update the persistent instance with the identifier of the given detached instance. If there is a persistent instance with the same identifier, an exception is thrown."

And that is why you see the exception.

Merge, on the other hand works with both detached and persistent objects.

Really if you are dealing with persistent objects. You don't have to explicitly call saveOrUpdate. When the session closes, your object will be automatically updated.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


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