From c80a8c442a91b880a24ea32a79e48da3c4a205a8 Mon Sep 17 00:00:00 2001 From: Arcadiy Ivanov Date: Sat, 27 Sep 2014 02:19:01 -0400 Subject: [PATCH] Fix for HHH-9419 into master See https://github.com/hibernate/hibernate-orm/pull/815 --- .../internal/EntityVerifyVersionProcess.java | 5 ++++ .../jpa/test/lock/QueryLockingTest.java | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityVerifyVersionProcess.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityVerifyVersionProcess.java index 6c1d694195..5e43d5c813 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityVerifyVersionProcess.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityVerifyVersionProcess.java @@ -55,6 +55,11 @@ public class EntityVerifyVersionProcess implements BeforeTransactionCompletionPr public void doBeforeTransactionCompletion(SessionImplementor session) { final EntityPersister persister = entry.getPersister(); + if ( !entry.isExistsInDatabase() ) { + // HHH-9419: We cannot check for a version of an entry we ourselves deleted + return; + } + final Object latestVersion = persister.getCurrentVersion( entry.getId(), session ); if ( !entry.getVersion().equals( latestVersion ) ) { throw new OptimisticLockException( diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/lock/QueryLockingTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/lock/QueryLockingTest.java index 0b60aa6d5e..91d62fb081 100755 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/lock/QueryLockingTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/lock/QueryLockingTest.java @@ -293,6 +293,31 @@ public class QueryLockingTest extends BaseEntityManagerFunctionalTestCase { em.close(); } + @Test + @TestForIssue(jiraKey = "HHH-9419") + public void testNoVersionCheckAfterRemove() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + Lockable lock = new Lockable( "name" ); + em.persist( lock ); + em.getTransaction().commit(); + em.close(); + Integer initial = lock.getVersion(); + assertNotNull( initial ); + + em = getOrCreateEntityManager(); + em.getTransaction().begin(); + Lockable reread = em.createQuery( "from Lockable", Lockable.class ) + .setLockMode( LockModeType.OPTIMISTIC ) + .getSingleResult(); + assertEquals( initial, reread.getVersion() ); + assertTrue( em.unwrap( SessionImpl.class ).getActionQueue().hasBeforeTransactionActions() ); + em.remove( reread ); + em.getTransaction().commit(); + em.close(); + assertEquals( initial, reread.getVersion() ); + } + @Test public void testOptimisticSpecific() { EntityManager em = getOrCreateEntityManager();