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.  [ 1 post ] 
Author Message
 Post subject: merge more than once causes OptimisticLockException
PostPosted: Wed Jul 01, 2009 9:48 am 
Newbie

Joined: Thu May 21, 2009 6:25 pm
Posts: 2
Hi,

I am using Hibernate 3.2 with JBoss 4.2.3.

In our application we have to merge a detached entity multiple times. The entity is created and persisted in a business module; and in later stages it is modified and the update is persisted in various business modules. Every business module has a new transaction scope and so the entity becomes detached after the first module where it is persisted. So we are trying to merge the detached entity in every module it is modified. The merge works fine the first time and I understand that the merge returns a new managed entity. But when we try to merge the detached entity again, it throws an OptimisticLockException because the version field of the detached entity is still zero "0". I understand the cause for this problem: When the detached entity is merged for the first time, the version field gets incremented when the transaction commits, but the version value is not reflected in the detached entity. Is there any solution to make this work? Am I doing something wrong?

One workaround I could think of is, have separate update methods for every update, which finds the entity using em.find() method and updates the modified fields using respective setter methods on the returned managed entity. But I feel that is killing the effectiveness of the merge functionality. While using merge, we just don't need to worry about which fields need to be updated, rather just call the merge on it!

I am not really looking for any suggestion to change our business logic. One suggestion could be, why persist the entity first and then keep merging in every module; rather, why not persist the entity at the end after all the updates are done. But this is how our business logic should work - persist the entity in the module which creates it and later do merge on every update.

Your help will be highly appreciated.

Thanks
Vignesh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.