diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/work/ModWorkUnit.java b/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/work/ModWorkUnit.java index 15eb6e7621..5b2454b0fa 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/work/ModWorkUnit.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/synchronization/work/ModWorkUnit.java @@ -87,6 +87,7 @@ public class ModWorkUnit extends AbstractAuditWorkUnit implements AuditWorkUnit } public AuditWorkUnit merge(CollectionChangeWorkUnit second) { + second.mergeCollectionModifiedData(data); return this; } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedMergeTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedMergeTest.java index 1f1f608f96..6971ce9da4 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedMergeTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/modifiedflags/HasChangedMergeTest.java @@ -19,8 +19,11 @@ import static org.junit.Assert.assertEquals; * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ public class HasChangedMergeTest extends AbstractModifiedFlagsEntityTest { - private Integer parentId = null; - private Integer childId = null; + private Integer parent1Id = null; + private Integer child1Id = null; + + private Integer parent2Id = null; + private Integer child2Id = null; @Override protected Class[] getAnnotatedClasses() { @@ -34,22 +37,38 @@ public class HasChangedMergeTest extends AbstractModifiedFlagsEntityTest { // Revision 1 - data preparation em.getTransaction().begin(); - ListRefEdEntity parent = new ListRefEdEntity( 1, "initial data" ); - parent.setReffering( new ArrayList() ); // Empty collection is not the same as null reference. - em.persist( parent ); + ListRefEdEntity parent1 = new ListRefEdEntity( 1, "initial data" ); + parent1.setReffering( new ArrayList() ); // Empty collection is not the same as null reference. + ListRefEdEntity parent2 = new ListRefEdEntity( 2, "initial data" ); + parent2.setReffering( new ArrayList() ); + em.persist( parent1 ); + em.persist( parent2 ); em.getTransaction().commit(); // Revision 2 - inserting new child entity and updating parent em.getTransaction().begin(); - parent = em.find( ListRefEdEntity.class, parent.getId() ); - ListRefIngEntity child = new ListRefIngEntity( 1, "initial data", parent ); - em.persist( child ); - parent.setData( "updated data" ); - parent = em.merge( parent ); + parent1 = em.find( ListRefEdEntity.class, parent1.getId() ); + ListRefIngEntity child1 = new ListRefIngEntity( 1, "initial data", parent1 ); + em.persist( child1 ); + parent1.setData( "updated data" ); + parent1 = em.merge( parent1 ); em.getTransaction().commit(); - parentId = parent.getId(); - childId = child.getId(); + // Revision 3 - updating parent, flushing and adding new child + em.getTransaction().begin(); + parent2 = em.find( ListRefEdEntity.class, parent2.getId() ); + parent2.setData( "updated data" ); + parent2 = em.merge( parent2 ); + em.flush(); + ListRefIngEntity child2 = new ListRefIngEntity( 2, "initial data", parent2 ); + em.persist( child2 ); + em.getTransaction().commit(); + + parent1Id = parent1.getId(); + child1Id = child1.getId(); + + parent2Id = parent2.getId(); + child2Id = child2.getId(); em.close(); } @@ -57,16 +76,32 @@ public class HasChangedMergeTest extends AbstractModifiedFlagsEntityTest { @Test @TestForIssue(jiraKey = "HHH-7948") public void testOneToManyInsertChildUpdateParent() { - List list = queryForPropertyHasChanged( ListRefEdEntity.class, parentId, "data" ); + List list = queryForPropertyHasChanged( ListRefEdEntity.class, parent1Id, "data" ); assertEquals( 2, list.size() ); assertEquals( makeList( 1, 2 ), extractRevisionNumbers( list ) ); - list = queryForPropertyHasChanged( ListRefEdEntity.class, parentId, "reffering" ); + list = queryForPropertyHasChanged( ListRefEdEntity.class, parent1Id, "reffering" ); assertEquals( 2, list.size() ); assertEquals( makeList( 1, 2 ), extractRevisionNumbers( list ) ); - list = queryForPropertyHasChanged( ListRefIngEntity.class, childId, "reference" ); + list = queryForPropertyHasChanged( ListRefIngEntity.class, child1Id, "reference" ); assertEquals( 1, list.size() ); assertEquals( makeList( 2 ), extractRevisionNumbers( list ) ); } + + @Test + @TestForIssue(jiraKey = "HHH-7948") + public void testOneToManyUpdateParentInsertChild() { + List list = queryForPropertyHasChanged( ListRefEdEntity.class, parent2Id, "data" ); + assertEquals( 2, list.size() ); + assertEquals( makeList( 1, 3 ), extractRevisionNumbers( list ) ); + + list = queryForPropertyHasChanged( ListRefEdEntity.class, parent2Id, "reffering" ); + assertEquals( 2, list.size() ); + assertEquals( makeList( 1, 3 ), extractRevisionNumbers( list ) ); + + list = queryForPropertyHasChanged( ListRefIngEntity.class, child2Id, "reference" ); + assertEquals( 1, list.size() ); + assertEquals( makeList( 3 ), extractRevisionNumbers( list ) ); + } }