-->
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: Advice on updating from partial detached object
PostPosted: Wed Jul 25, 2007 1:08 pm 
Newbie

Joined: Thu May 31, 2007 11:50 pm
Posts: 6
I'm looking for advice on the correct way to update from a partial detached object (a set of data points that include the version from the original object).

Let me start by explaining our requirements, then how we have found to do it currently. Because of the way our application is built, we are passing data up to a form on another server via XML, so we are not sending the entire object but just whatever data points are being used on the form. When the form is submitted, we need to get those data points back into the original object, but if the object has changed since we originally retrieved it to display on the form for editing we need to know.

Hibernate's optimistic locking sounded like it would work for us here, so we've put version columns on our tables however in practice this didn't seem to be working. Since we we don't have the original object we retrieve it from the database and update it with our data points (including the original "version" column). The problem we're running into is our update of the version column seems to be ignored. The only way we've found around that is to do the check ourselves, comparing the old version to the new and throwing OptimisticLockingFailureException if they're different.

We can go this route, however it seems to me we're missing something and there should be another way to do this. I've tried using merge() and attachDirty(), however it seems like those aren't detecting changed versions.

I've been re-reading the sections on transient/detached objects and locking, but haven't found any references to this. Does anyone have any pointers or suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 7:51 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
If you could get hold of a detached instance of the latest version, then update the version number and appropriate data points, hibernate's auto-detect of stale data will work.

i.e.
At the moment I suspect you're doing this:
open session
load object
update version num
update data points
save object
close session

My tests confirm what you see - hibernate ignores updates to the version number in this case. Probably because the object is attached to the session hibernate, rightly in my opinion, assumes it has control of the version field.

Instead, load the object in a separate session to create a detached instance:
open session1
load object
close session1

update version number
update data points

open session2
save object
close session2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 12:11 pm 
Newbie

Joined: Thu May 31, 2007 11:50 pm
Posts: 6
Yep, that's along the lines of what I was thinking as well. However we are using Spring and have the entire request wrapped in a transaction so we're not managing the session manually. I don't guess there's a way to force an object to detach without having to get into managing the sessions? I've been trying to find a way and having much luck.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 4:17 am 
Newbie

Joined: Mon Sep 03, 2007 7:41 am
Posts: 1
I have identical problem with optimistic locking when manually change the version. These are the most relevant threads I found:
http://forum.hibernate.org/viewtopic.php?t=955121
http://forum.hibernate.org/viewtopic.php?t=949462
http://forum.hibernate.org/viewtopic.php?t=955893
http://forum.springframework.org/showthread.php?t=23736
I see two acceptable solutions:
1. Do evict() - does work but has performance cost and not elegant
2. Compare versions in the interceptor (see the first link above) - looks better than evict()

I think it will be great if Hibernate team will post some opinion about this (actual) problem (i didn't find such so far). Thanks in advance.


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.