HHH-5565 - Fix and test
This commit is contained in:
parent
d51a0d0c78
commit
ffa9cf5768
|
@ -70,6 +70,8 @@ public abstract class BaseEnversCollectionEventListener extends BaseEnversEventL
|
|||
return;
|
||||
}
|
||||
if ( getAuditConfiguration().getEntCfg().isVersioned( entityName ) ) {
|
||||
checkIfTransactionInProgress(event.getSession());
|
||||
|
||||
AuditProcess auditProcess = getAuditConfiguration().getSyncManager().get(event.getSession());
|
||||
|
||||
String ownerEntityName = ((AbstractCollectionPersister) collectionEntry.getLoadedPersister()).getOwnerEntityName();
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.hibernate.envers.configuration.AuditConfiguration;
|
|||
import org.hibernate.envers.entities.RelationDescription;
|
||||
import org.hibernate.envers.entities.RelationType;
|
||||
import org.hibernate.envers.entities.mapper.id.IdMapper;
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
import org.hibernate.envers.synchronization.AuditProcess;
|
||||
import org.hibernate.envers.synchronization.work.CollectionChangeWorkUnit;
|
||||
import org.hibernate.envers.tools.Tools;
|
||||
|
@ -126,4 +127,11 @@ public abstract class BaseEnversEventListener implements EnversListener {
|
|||
toPropertyName, enversConfiguration, id, value));
|
||||
}
|
||||
|
||||
protected void checkIfTransactionInProgress(SessionImplementor session) {
|
||||
if (!session.isTransactionInProgress()) {
|
||||
// Historical data would not be flushed to audit tables if outside of active transaction
|
||||
// (AuditProcess#doBeforeTransactionCompletion(SessionImplementor) not executed).
|
||||
throw new AuditException("Unable to create revision because of non-active transaction");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,6 +45,8 @@ public class EnversPostDeleteEventListenerImpl extends BaseEnversEventListener i
|
|||
String entityName = event.getPersister().getEntityName();
|
||||
|
||||
if ( getAuditConfiguration().getEntCfg().isVersioned( entityName ) ) {
|
||||
checkIfTransactionInProgress(event.getSession());
|
||||
|
||||
AuditProcess auditProcess = getAuditConfiguration().getSyncManager().get( event.getSession() );
|
||||
|
||||
AuditWorkUnit workUnit = new DelWorkUnit(
|
||||
|
|
|
@ -44,6 +44,8 @@ public class EnversPostInsertEventListenerImpl extends BaseEnversEventListener i
|
|||
String entityName = event.getPersister().getEntityName();
|
||||
|
||||
if ( getAuditConfiguration().getEntCfg().isVersioned( entityName ) ) {
|
||||
checkIfTransactionInProgress(event.getSession());
|
||||
|
||||
AuditProcess auditProcess = getAuditConfiguration().getSyncManager().get(event.getSession());
|
||||
|
||||
AuditWorkUnit workUnit = new AddWorkUnit(
|
||||
|
|
|
@ -46,6 +46,8 @@ public class EnversPostUpdateEventListenerImpl extends BaseEnversEventListener i
|
|||
String entityName = event.getPersister().getEntityName();
|
||||
|
||||
if ( getAuditConfiguration().getEntCfg().isVersioned(entityName) ) {
|
||||
checkIfTransactionInProgress(event.getSession());
|
||||
|
||||
AuditProcess auditProcess = getAuditConfiguration().getSyncManager().get(event.getSession());
|
||||
|
||||
final Object[] newDbState = postUpdateDBState( event );
|
||||
|
|
|
@ -0,0 +1,117 @@
|
|||
package org.hibernate.envers.test.integration.basic;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
import org.hibernate.envers.test.BaseEnversFunctionalTestCase;
|
||||
import org.hibernate.envers.test.entities.StrTestEntity;
|
||||
import org.hibernate.envers.test.integration.collection.norevision.Name;
|
||||
import org.hibernate.envers.test.integration.collection.norevision.Person;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-5565")
|
||||
public class OutsideTransactionTest extends BaseEnversFunctionalTestCase {
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[]{StrTestEntity.class, Person.class, Name.class};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration configuration) {
|
||||
configuration.setProperty("org.hibernate.envers.store_data_at_delete", "true");
|
||||
configuration.setProperty("org.hibernate.envers.revision_on_collection_change", "true");
|
||||
}
|
||||
|
||||
@Test(expected = AuditException.class)
|
||||
public void testInsertOutsideActiveTransaction() {
|
||||
Session session = openSession();
|
||||
|
||||
// Illegal insertion of entity outside of active transaction.
|
||||
StrTestEntity entity = new StrTestEntity("data");
|
||||
session.persist(entity);
|
||||
session.flush();
|
||||
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test(expected = AuditException.class)
|
||||
public void testUpdateOutsideActiveTransaction() {
|
||||
Session session = openSession();
|
||||
|
||||
// Revision 1
|
||||
session.getTransaction().begin();
|
||||
StrTestEntity entity = new StrTestEntity("data");
|
||||
session.persist(entity);
|
||||
session.getTransaction().commit();
|
||||
|
||||
// Illegal modification of entity state outside of active transaction.
|
||||
entity.setStr("modified data");
|
||||
session.update(entity);
|
||||
session.flush();
|
||||
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test(expected = AuditException.class)
|
||||
public void testDeleteOutsideActiveTransaction() {
|
||||
Session session = openSession();
|
||||
|
||||
// Revision 1
|
||||
session.getTransaction().begin();
|
||||
StrTestEntity entity = new StrTestEntity("data");
|
||||
session.persist(entity);
|
||||
session.getTransaction().commit();
|
||||
|
||||
// Illegal removal of entity outside of active transaction.
|
||||
session.delete(entity);
|
||||
session.flush();
|
||||
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test(expected = AuditException.class)
|
||||
public void testCollectionUpdateOutsideActiveTransaction() {
|
||||
Session session = openSession();
|
||||
|
||||
// Revision 1
|
||||
session.getTransaction().begin();
|
||||
Person person = new Person();
|
||||
Name name = new Name();
|
||||
name.setName("Name");
|
||||
person.getNames().add(name);
|
||||
session.saveOrUpdate(person);
|
||||
session.getTransaction().commit();
|
||||
|
||||
// Illegal collection update outside of active transaction.
|
||||
person.getNames().remove(name);
|
||||
session.saveOrUpdate(person);
|
||||
session.flush();
|
||||
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test(expected = AuditException.class)
|
||||
public void testCollectionRemovalOutsideActiveTransaction() {
|
||||
Session session = openSession();
|
||||
|
||||
// Revision 1
|
||||
session.getTransaction().begin();
|
||||
Person person = new Person();
|
||||
Name name = new Name();
|
||||
name.setName("Name");
|
||||
person.getNames().add(name);
|
||||
session.saveOrUpdate(person);
|
||||
session.getTransaction().commit();
|
||||
|
||||
// Illegal collection removal outside of active transaction.
|
||||
person.setNames(null);
|
||||
session.saveOrUpdate(person);
|
||||
session.flush();
|
||||
|
||||
session.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue