-->
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: @OptimisticLock(excluded=true) doesn't work for detached obj
PostPosted: Thu Aug 30, 2007 12:48 pm 
Newbie

Joined: Mon Nov 20, 2006 12:41 pm
Posts: 4
Hibernate version:3.2.3.ga
Hibernate Annotation version:3.3.0.GA

I'm facing a strange behaviour. I have an versionned entity and one attribute annotated as @OptimisticLock(excluded=true). I'm only changing the attribute wich is excluded from the optimstic lock. Then I try to update it. If I made the change on a persistant instance the version is not incremented at the flush() but if I made the change on a detached object (an object which was in a Session but was evicted from it) and call the update method on a new Session the version is incremented but it shouldn't.

I didn't test it with xml mapping but I would like to know if it's the correct behaviour or a bug?

My entity has several one-to-many relationships and the attribute which is annotated as @OptimisticLock(excluded=true) is also @Embbeded

Code:
@Entity
public class Car {
   ...
   @Id
   public Long getId() {
   ...

   @Version
   public Long getVersion() {
   ...

   @Embedded
   @OptimisticLock(excluded=true)
   public Engine getEngine() {
   ...

   ...
}



This case doesn't increment the version
Code:
// open session and begin transaction
Car car = session.get(Car.class, id);
car.setEngine(new Engine());
// flush the session


This case increment the version and it should't do it
Code:
// open session and begin transaction
Car car = session.get(Car.class, id);
car.setEngine(new Engine());
...
session.evict(car);
...
session.update(car);
// flush the session


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 5:28 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is the correct behavior since we don't know the old object state, so we assume everything has changed.
One way to avoid that is to use merge

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 5:04 am 
Newbie

Joined: Mon Nov 20, 2006 12:41 pm
Posts: 4
Thanks for your previous answer. It didn't fully solve my problem. I'm going to explain my case.

I'm in an EJB and my clients doesn't provide me the full object graph so I have to load the data from the db, change them according to the partial object I received and update them. According to the paragraph 11.3.1 in the documentation , I have to check the version manually. My question is : Can Hibernate do automatically the version checking in this case because the partial object I receive is not trivial to check?

Code:
public void update(Car car){
// open session and begin transaction
Car carDb = session.get(Car.class, car.getId());

// map all the changes I need
carDb.setName(car.getName()); // a String attribut
carDb.setEngine(car.setEngine()); // a complex Object
carDb.setVersion(car.getVersion());
// -> I want to keep all others attributs and relationship as in the db

// flush the session
}

I would like that Hibernate fire the version checking at the flush but it doesn't.

I've also tried a dirty solution as a session.evict(carDb); session.update(carDb); or a session.evict(carDb);session.merge(carDb); before the flush but it didn't do what I want.

Is there any solution or any pattern to follow in this case in order to "say" to Hibernate to do the version checking?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 5:20 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
not really, because you are not supposed to change the version manually as your code does.

_________________
Emmanuel


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.