[hibernate-commits] Hibernate SVN: r14842 - in core/branches/Branch_3_2: test/org/hibernate/test/ops and 1 other directory.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Tue Jul 1 17:49:14 EDT 2008
Author: steve.ebersole at jboss.com
Date: 2008-07-01 17:49:14 -0400 (Tue, 01 Jul 2008)
New Revision: 14842
Modified:
core/branches/Branch_3_2/src/org/hibernate/type/CollectionType.java
core/branches/Branch_3_2/test/org/hibernate/test/ops/MergeTest.java
Log:
HHH-3294 : persistent collection element replacing on merge
Modified: core/branches/Branch_3_2/src/org/hibernate/type/CollectionType.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/type/CollectionType.java 2008-07-01 21:48:39 UTC (rev 14841)
+++ core/branches/Branch_3_2/src/org/hibernate/type/CollectionType.java 2008-07-01 21:49:14 UTC (rev 14842)
@@ -550,12 +550,16 @@
//for arrays, replaceElements() may return a different reference, since
//the array length might not match
result = replaceElements( original, result, owner, copyCache, session );
-
- if (original==target) {
- //get the elements back into the target
+
+ if ( original == target ) {
+ // get the elements back into the target making sure to handle dirty flag
+ boolean wasClean = PersistentCollection.class.isInstance( target ) && !( ( PersistentCollection ) target ).isDirty();
//TODO: this is a little inefficient, don't need to do a whole
// deep replaceElements() call
replaceElements( result, target, owner, copyCache, session );
+ if ( wasClean ) {
+ ( ( PersistentCollection ) target ).clearDirty();
+ }
result = target;
}
Modified: core/branches/Branch_3_2/test/org/hibernate/test/ops/MergeTest.java
===================================================================
--- core/branches/Branch_3_2/test/org/hibernate/test/ops/MergeTest.java 2008-07-01 21:48:39 UTC (rev 14841)
+++ core/branches/Branch_3_2/test/org/hibernate/test/ops/MergeTest.java 2008-07-01 21:49:14 UTC (rev 14842)
@@ -283,6 +283,55 @@
// cleanup();
}
+ public void testNoExtraUpdatesOnPersistentMergeVersionedWithCollection() throws Exception {
+ Session s = openSession();
+ s.beginTransaction();
+ VersionedEntity parent = new VersionedEntity( "parent", "parent" );
+ VersionedEntity child = new VersionedEntity( "child", "child" );
+ parent.getChildren().add( child );
+ child.setParent( parent );
+ s.persist( parent );
+ s.getTransaction().commit();
+ s.close();
+
+ clearCounts();
+
+ // parent is now detached, but we have made no changes. so attempt to merge it
+ // into this new session; this should cause no updates...
+ s = openSession();
+ s.beginTransaction();
+ // load parent so that merge will follow entityIsPersistent path
+ VersionedEntity persistentParent = ( VersionedEntity ) s.get( VersionedEntity.class, parent.getId() );
+ // load children
+ VersionedEntity persistentChild = ( VersionedEntity ) persistentParent.getChildren().iterator().next();
+ VersionedEntity mergedParent = ( VersionedEntity ) s.merge( persistentParent ); // <-- This merge leads to failure
+ s.getTransaction().commit();
+ s.close();
+
+ assertUpdateCount( 0 );
+ assertInsertCount( 0 );
+ assertEquals( "unexpected parent version increment", parent.getVersion(), mergedParent.getVersion() );
+ VersionedEntity mergedChild = ( VersionedEntity ) mergedParent.getChildren().iterator().next();
+ assertEquals( "unexpected child version increment", child.getVersion(), mergedChild.getVersion() );
+
+ ///////////////////////////////////////////////////////////////////////
+ // as a control measure, now update the node once it is loaded and
+ // make sure we get an update as a result...
+ s = openSession();
+ s.beginTransaction();
+ persistentParent = ( VersionedEntity ) s.get( VersionedEntity.class, parent.getId() );
+ persistentParent.setName( "new name" );
+ persistentParent.getChildren().add( new VersionedEntity( "child2", "new child" ) );
+ persistentParent = ( VersionedEntity ) s.merge( persistentParent );
+ s.getTransaction().commit();
+ s.close();
+ assertUpdateCount( 1 );
+ assertInsertCount( 1 );
+ ///////////////////////////////////////////////////////////////////////
+
+ // cleanup();
+ }
+
public void testPersistThenMergeInSameTxnWithVersion() {
Session s = openSession();
Transaction tx = s.beginTransaction();
More information about the hibernate-commits
mailing list