From b7855e944bab1e11abd2c17d804d13c813805e8f Mon Sep 17 00:00:00 2001 From: brmeyer Date: Thu, 25 Oct 2012 10:11:14 -0400 Subject: [PATCH] HHH-7682 Audit records are not rolled back when transaction is rolled back --- .../basic/TransactionRollbackBehaviour.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java new file mode 100644 index 0000000000..b6622af69b --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/basic/TransactionRollbackBehaviour.java @@ -0,0 +1,50 @@ +package org.hibernate.envers.test.integration.basic; + +import java.util.List; + +import javax.persistence.EntityManager; + +import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; +import org.hibernate.envers.test.entities.IntTestEntity; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Tomasz Dziurko (tdziurko at gmail dot com) + */ +public class TransactionRollbackBehaviour extends BaseEnversJPAFunctionalTestCase { + + @Test + public void testAuditRecordsRollback() { + // Given + EntityManager em = getEntityManager(); + em.getTransaction().begin(); + IntTestEntity iteToRollback = new IntTestEntity( 30 ); + em.persist( iteToRollback ); + Integer rollbackedIteId = iteToRollback.getId(); + em.getTransaction().rollback(); + + // When + em.getTransaction().begin(); + IntTestEntity ite2 = new IntTestEntity( 50 ); + em.persist( ite2 ); + Integer ite2Id = ite2.getId(); + em.getTransaction().commit(); + + // Then + List revisionsForSavedClass = getAuditReader() + .getRevisions( IntTestEntity.class, ite2Id ); + Assert.assertEquals( "There should be one revision for inserted entity", + 1, revisionsForSavedClass.size() ); + + List revisionsForRolledbackClass = getAuditReader() + .getRevisions( IntTestEntity.class, rollbackedIteId ); + Assert.assertEquals( "There should be one revision for inserted entity", + 0, revisionsForRolledbackClass.size() ); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { IntTestEntity.class }; + } +}