Author: gbadner
Date: 2008-04-29 19:02:13 -0400 (Tue, 29 Apr 2008)
New Revision: 14610
Modified:
core/trunk/testsuite/src/test/java/org/hibernate/test/ops/MergeTest.java
Log:
added tests for merging initialized and uninitialized managed collections
Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/ops/MergeTest.java
===================================================================
--- core/trunk/testsuite/src/test/java/org/hibernate/test/ops/MergeTest.java 2008-04-29
02:06:27 UTC (rev 14609)
+++ core/trunk/testsuite/src/test/java/org/hibernate/test/ops/MergeTest.java 2008-04-29
23:02:13 UTC (rev 14610)
@@ -4,16 +4,17 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Set;
import junit.framework.Test;
import org.hibernate.Hibernate;
+import org.hibernate.NonUniqueObjectException;
import org.hibernate.Session;
+import org.hibernate.StaleObjectStateException;
import org.hibernate.Transaction;
-import org.hibernate.NonUniqueObjectException;
-import org.hibernate.StaleObjectStateException;
+import org.hibernate.criterion.Projections;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
-import org.hibernate.criterion.Projections;
/**
* @author Gavin King
@@ -561,6 +562,95 @@
cleanup();
}
+ public void testMergeManagedUninitializedCollection() {
+
+ Session s = openSession();
+ Transaction tx = s.beginTransaction();
+ NumberedNode root = new NumberedNode( "root" );
+ root.addChild( new NumberedNode( "child" ) );
+ s.persist(root);
+ tx.commit();
+ s.close();
+
+ clearCounts();
+
+ NumberedNode newRoot = new NumberedNode( "root" );
+ newRoot.setId( root.getId() );
+
+ s = openSession();
+ tx = s.beginTransaction();
+ root = ( NumberedNode ) s.get( NumberedNode.class, root.getId() );
+ Set managedChildren = root.getChildren();
+ assertFalse( Hibernate.isInitialized( managedChildren ) );
+ newRoot.setChildren( managedChildren );
+ assertSame( root, s.merge( newRoot ) );
+ assertSame( managedChildren, root.getChildren() );
+ assertFalse( Hibernate.isInitialized( managedChildren ) );
+ tx.commit();
+
+ assertInsertCount(0);
+ assertUpdateCount(0);
+ assertDeleteCount(0);
+
+ tx = s.beginTransaction();
+ assertEquals(
+ s.createCriteria(NumberedNode.class)
+ .setProjection( Projections.rowCount() )
+ .uniqueResult(),
+ new Integer(2)
+ );
+ tx.commit();
+
+ s.close();
+
+ cleanup();
+ }
+
+ public void testMergeManagedInitializedCollection() {
+
+ Session s = openSession();
+ Transaction tx = s.beginTransaction();
+ NumberedNode root = new NumberedNode( "root" );
+ root.addChild( new NumberedNode( "child" ) );
+ s.persist(root);
+ tx.commit();
+ s.close();
+
+ clearCounts();
+
+ NumberedNode newRoot = new NumberedNode( "root" );
+ newRoot.setId( root.getId() );
+
+ s = openSession();
+ tx = s.beginTransaction();
+ root = ( NumberedNode ) s.get( NumberedNode.class, root.getId() );
+ Set managedChildren = root.getChildren();
+ Hibernate.initialize( managedChildren );
+ assertTrue( Hibernate.isInitialized( managedChildren ) );
+ newRoot.setChildren( managedChildren );
+ assertSame( root, s.merge( newRoot ) );
+ assertSame( managedChildren, root.getChildren() );
+ assertTrue( Hibernate.isInitialized( managedChildren ) );
+ tx.commit();
+
+ assertInsertCount(0);
+ assertUpdateCount(0);
+ assertDeleteCount(0);
+
+ tx = s.beginTransaction();
+ assertEquals(
+ s.createCriteria(NumberedNode.class)
+ .setProjection( Projections.rowCount() )
+ .uniqueResult(),
+ new Integer(2)
+ );
+ tx.commit();
+
+ s.close();
+
+ cleanup();
+ }
+
public void testRecursiveMergeTransient() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Show replies by date