Merge pull request #393 from tdziurko/Envers-bugfix-HHH-7682

HHH-7682: Envers bugfix
This commit is contained in:
Adam Warski 2012-10-11 03:36:38 -07:00
commit 1555ba9969
2 changed files with 54 additions and 1 deletions

View File

@ -27,6 +27,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.hibernate.action.AfterTransactionCompletionProcess; import org.hibernate.action.AfterTransactionCompletionProcess;
import org.hibernate.action.BeforeTransactionCompletionProcess;
import org.hibernate.engine.SessionImplementor; import org.hibernate.engine.SessionImplementor;
import org.hibernate.envers.revisioninfo.RevisionInfoGenerator; import org.hibernate.envers.revisioninfo.RevisionInfoGenerator;
@ -55,7 +56,13 @@ public class AuditProcessManager {
auditProcess = new AuditProcess(revisionInfoGenerator, session); auditProcess = new AuditProcess(revisionInfoGenerator, session);
auditProcesses.put(transaction, auditProcess); auditProcesses.put(transaction, auditProcess);
session.getActionQueue().registerProcess(auditProcess); session.getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() {
public void doBeforeTransactionCompletion(SessionImplementor session) {
if(auditProcesses.get(transaction) != null) {
auditProcesses.get(transaction).doBeforeTransactionCompletion(session);
}
}
});
session.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() { session.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() {
public void doAfterTransactionCompletion(boolean success, SessionImplementor session) { public void doAfterTransactionCompletion(boolean success, SessionImplementor session) {

View File

@ -0,0 +1,46 @@
package org.hibernate.envers.test.integration.basic;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.entities.IntTestEntity;
import org.testng.annotations.Test;
import javax.persistence.EntityManager;
import java.util.List;
import static org.testng.Assert.assertEquals;
/**
* @author Tomasz Dziurko (tdziurko at gmail dot com)
*/
public class TransactionRollbackBehaviour extends AbstractEntityTest {
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(IntTestEntity.class);
}
@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<Number> revisionsForSavedClass = getAuditReader().getRevisions(IntTestEntity.class, ite2Id);
assertEquals(revisionsForSavedClass.size(), 1, "There should be one revision for inserted entity");
List<Number> revisionsForRolledbackClass = getAuditReader().getRevisions(IntTestEntity.class, rollbackedIteId);
assertEquals(revisionsForRolledbackClass.size(), 0, "There should be no revisions for insert that was rolled back");
}
}