-->
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.  [ 5 posts ] 
Author Message
 Post subject: Trying to understand the behaviour of Hibernate merge
PostPosted: Thu Oct 01, 2009 11:15 am 
Newbie

Joined: Thu Oct 01, 2009 4:55 am
Posts: 4
Applogies if this question have already been asked. I've searched the Hibernate docs and forum for a while without finding an answer.

I have a Spring - Hibernate application.

When merge(Object o) is called and an error is encountered an exception is immediately thrown, but if I use for example update(Object o) the exception doesn't get thrown until a flush is executed(for example before a commit). In my case an exception related to optimistic locking is thrown(HibernateOptimisticLockingFailureException), this exception is expected because of 2 users updating the same entity. I'm Guessing other types of exceptions will produce the same behavior.

Can someone help me understand why merge is throwing the exception instantly whilst with the update method nothing happens until flushing?

This behaviour forces me to always use a merge when the entity is under version control by hibernate since I want the exception to be thrown at an expected point. Since I use Springs HibernateTransactionManager I do not have explicit control over when the commit of the transaction is performed.


Top
 Profile  
 
 Post subject: Re: Trying to understand the behaviour of Hibernate merge
PostPosted: Thu Oct 01, 2009 1:35 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi RobbieH,

calling merge() takes immediate effect whereas calling update() is postponed until the session is flushed. Maybe, reading Modifying detached objects and Automatic state detection of the Hibernate docs gives you some more insight.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Trying to understand the behaviour of Hibernate merge
PostPosted: Thu Oct 01, 2009 2:58 pm 
Newbie

Joined: Thu Oct 01, 2009 4:55 am
Posts: 4
Thanks for your reply Froestel.

I have previously read those parts of the doc but I don't really see where they explain the difference between the 2 i.e why merge takes immediate effect whilst update don't. But maybe I'm missing something.

merge explained in "Automatic state detection" part:
Quote:
if there is a persistent instance with the same identifier currently associated with the session, copy the state of the given object onto the persistent instance

if there is no persistent instance currently associated with the session, try to load it from the database, or create a new persistent instance
...
...


1. The loading from the database will perform a flush, right?
2. if there is an existing persistent instance the changes in the versioned property will be detected at copy time?
Is this correct? I'm just guessing. Or do you mean that merge is doing an instant flush no matter what?

By the way this is exactly what I'm doing:
Quote:
Usually update() or saveOrUpdate() are used in the following scenario:

the application loads an object in the first session

the object is passed up to the UI tier

some modifications are made to the object

the object is passed back down to the business logic tier

the application persists these modifications by calling update() in a second session


The main reason for asking this queston is that I want to know if merge is the only reasonable option in my case, and if I can trust merge always to throw an exception instantly i.e not wait until a flush. (Actually I would rather not use merge at all in this case)


Top
 Profile  
 
 Post subject: Re: Trying to understand the behaviour of Hibernate merge
PostPosted: Sat Oct 03, 2009 12:02 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi RobbieH,

as I wrote before
Froestel wrote:
calling merge() takes immediate effect


This means anytime you call merge() the action will be executed in that very moment. So it is guaranteed that the exception will be thrown right away. You would have the same effect on
Code:
session.update(obj);
session.flush();
but in this case the call to flush() will be the trigger for your exception. You cannot determine precisely when the exception is thrown with update() since Hibernate might do some statement re-ordering and stuff.

So if you need to have the exception thrown in that very moment when you call the method to be executed then use merge().

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Trying to understand the behaviour of Hibernate merge
PostPosted: Sat Oct 03, 2009 6:41 pm 
Newbie

Joined: Thu Oct 01, 2009 4:55 am
Posts: 4
Ok. Thank you Froestel.
Along with your answer and some further reading I now have a better understanding.


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