-->
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: EntityManager manipulation of @Version
PostPosted: Wed Jun 10, 2009 9:04 am 
Newbie

Joined: Wed Jun 10, 2009 8:33 am
Posts: 1
Why does a hibernate session ignore changes to an @Version column?

Consider the situation where I load an object from DB and then update elements of it with the intention of persisting those changes--all within a single Hibernate session. If I change the "name" field of my bean and then merge, the change will commit to the DB. However, if I change the version column (annotated @Version) and then merge, the change gets ignored and doesn't throw the StaleObjectException as it should.

This is particularly a problem for me when using spring's OpenEntityManagerInViewFilter when the same Hibernate session is used to pull the data (using Spring's prepare interceptor) from the DB and then update it within the same http request (same hibernate session).

In junit, this test will fail w/ OptimisticLockException (the behavior I would expect)--here the merge uses a different EntityManager:
Code:
@Test
   public void testSupplierCompanyBean() throws Exception {
      EntityManager em = emf.createEntityManager();
      EntityManager em3 = emf.createEntityManager();
      MyBean b = em.find(MyBean.class, 100);
      b.setVersion(b.getVersion() - 1);
      b=em3.merge(b);
   }

whereas this one will pass ( no StaleObject or OptimisticLock exceptions thrown) and instead of a version that is decremented (current -1), the resulting DB would have an incremented version.

Code:
@Test
   public void testSameEntityManagerMerge() throws Exception {
      EntityManager em = emf.createEntityManager();      
      MyBean b = em.find(MyBean.class, 100);
      b.setVersion(b.getVersion() - 1);
      b=em.merge(b);
   }

In the second case the object is still attached to em, however I don't see why hibernate doesn't notice that the version has changed and throw the StaleObjectException.


Top
 Profile  
 
 Post subject: Re: EntityManager manipulation of @Version
PostPosted: Wed Jun 10, 2009 9:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Hi,
as per the spec
"An entity may access the state of its version field or property or export a method for use by the application
to access the version, but must not modify the version value[17]."

Regards,
Anthony

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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.