diff --git a/envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java b/envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java index 3cbfc70d4a..9beeb9e1e5 100644 --- a/envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java +++ b/envers/src/main/java/org/hibernate/envers/event/AuditEventListener.java @@ -257,7 +257,9 @@ public class AuditEventListener implements PostInsertEventListener, PostUpdateEv private void onCollectionAction(AbstractCollectionEvent event, PersistentCollection newColl, Serializable oldColl, CollectionEntry collectionEntry) { String entityName = event.getAffectedOwnerEntityName(); - + if (! verCfg.getGlobalCfg().isGenerateRevisionsForCollections()) { + return; + } if (verCfg.getEntCfg().isVersioned(entityName)) { AuditProcess auditProcess = verCfg.getSyncManager().get(event.getSession()); diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangeNoRevisionTest.java b/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangeNoRevisionTest.java new file mode 100644 index 0000000000..129406e164 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangeNoRevisionTest.java @@ -0,0 +1,69 @@ +package org.hibernate.envers.test.integration.collection.norevision; + +import org.hibernate.MappingException; +import org.hibernate.Transaction; +import org.hibernate.criterion.Restrictions; +import org.hibernate.envers.test.AbstractSessionTest; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Optional; +import org.testng.annotations.Test; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class CollectionChangeNoRevisionTest extends AbstractSessionTest { + + protected static final int EXPECTED_PERSON_REVISION_COUNT = 1; + protected static final String CREATE_REVISION_ON_COLLECTION_CHANGE = "false"; + protected Integer personId; + + @Override + protected void initMappings() throws MappingException, URISyntaxException { + config.addAnnotatedClass(Person.class); + config.addAnnotatedClass(Name.class); + config.setProperty("org.hibernate.envers.revision_on_collection_change", getCollectionChangeValue()); + } + + protected String getCollectionChangeValue() { + return CREATE_REVISION_ON_COLLECTION_CHANGE; + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + newSessionFactory(); + Person p = new Person(); + Name n = new Name(); + n.setName("name1"); + p.getNames().add(n); + getSession().getTransaction().begin(); + getSession().saveOrUpdate(p); + getSession().getTransaction().commit(); + personId = p.getId(); + getSession().getTransaction().begin(); + n.setName("Changed name"); + getSession().saveOrUpdate(p); + getSession().getTransaction().commit(); + getSession().getTransaction().begin(); + Name n2 = new Name(); + n2.setName("name2"); + p.getNames().add(n2); + getSession().getTransaction().commit(); + } + + @Test + public void testPersonRevisionCount() { + int sizePerson = getAuditReader().getRevisions(Person.class, personId).size(); + assert sizePerson == getExpectedPersonRevisionCount(); + } + + protected int getExpectedPersonRevisionCount() { + return EXPECTED_PERSON_REVISION_COUNT; + } + + +} diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangeRevisionTest.java b/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangeRevisionTest.java new file mode 100644 index 0000000000..d48fec6a8a --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/CollectionChangeRevisionTest.java @@ -0,0 +1,27 @@ +package org.hibernate.envers.test.integration.collection.norevision; + +import org.hibernate.MappingException; +import org.testng.annotations.BeforeMethod; + +import java.net.URISyntaxException; + +public class CollectionChangeRevisionTest extends CollectionChangeNoRevisionTest { + + protected static final int PERSON_COUNT_NEW_REVISION_ON_COLLECTION = 2; + protected static final String NEW_REVISION_ON_COLLECTION = "true"; + + @Override + protected void initMappings() throws MappingException, URISyntaxException { + super.initMappings(); + } + + @Override + protected int getExpectedPersonRevisionCount() { + return PERSON_COUNT_NEW_REVISION_ON_COLLECTION; + } + + @Override + protected String getCollectionChangeValue() { + return NEW_REVISION_ON_COLLECTION; + } +} diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/Name.java b/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/Name.java new file mode 100644 index 0000000000..0c03fe9643 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/Name.java @@ -0,0 +1,43 @@ +package org.hibernate.envers.test.integration.collection.norevision; + +import org.hibernate.envers.Audited; + +import javax.persistence.*; +import java.io.Serializable; + +@Audited +@Entity +public class Name implements Serializable { + @Id + @GeneratedValue + private Integer id; + + private String name; + @ManyToOne + @JoinColumn(name = "person_id", insertable = false, updatable = false) + private Person person; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Person getPerson() { + return person; + } + + public void setPerson(Person person) { + this.person = person; + } +} diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/Person.java b/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/Person.java new file mode 100644 index 0000000000..512b0a89e1 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/collection/norevision/Person.java @@ -0,0 +1,40 @@ +package org.hibernate.envers.test.integration.collection.norevision; + +import org.hibernate.envers.AuditMappedBy; +import org.hibernate.envers.Audited; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +@Audited +@Entity +public class Person implements Serializable { + @Id @GeneratedValue + private Integer id; + @AuditMappedBy(mappedBy = "person") + @OneToMany(cascade = CascadeType.ALL) + @JoinColumn(name = "person_id") + private Set names; + + public Person() { + names = new HashSet(); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Set getNames() { + return names; + } + + public void setNames(Set names) { + this.names = names; + } +} diff --git a/envers/src/test/resources/testng.xml b/envers/src/test/resources/testng.xml index da3f6768f6..905743a18a 100644 --- a/envers/src/test/resources/testng.xml +++ b/envers/src/test/resources/testng.xml @@ -6,6 +6,7 @@ +