Author: steve.ebersole(a)jboss.com
Date: 2006-11-06 17:13:39 -0500 (Mon, 06 Nov 2006)
New Revision: 10740
Modified:
trunk/Hibernate3/src/org/hibernate/type/CollectionType.java
trunk/Hibernate3/test/org/hibernate/test/ops/MergeTest.java
trunk/Hibernate3/test/org/hibernate/test/ops/OptLockEntity.hbm.xml
trunk/Hibernate3/test/org/hibernate/test/ops/VersionedEntity.java
Log:
HHH-1401 : merge operation casuing unecessary updates
Modified: trunk/Hibernate3/src/org/hibernate/type/CollectionType.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/type/CollectionType.java 2006-11-06 22:00:41 UTC
(rev 10739)
+++ trunk/Hibernate3/src/org/hibernate/type/CollectionType.java 2006-11-06 22:13:39 UTC
(rev 10740)
@@ -397,15 +397,25 @@
}
/**
- * Replace the elements of a collection with the elements of another collection
+ * Replace the elements of a collection with the elements of another collection.
+ *
+ * @param original The 'source' of the replacement elements (where we copy
from)
+ * @param target The target of the replacement elements (where we copy to)
+ * @param owner The owner of the collection being merged
+ * @param copyCache The map of elements already replaced.
+ * @param session The session from which the merge event originated.
+ * @return The merged collection.
*/
- public Object replaceElements(Object original, Object target, Object owner, Map
copyCache,
- SessionImplementor session) throws HibernateException {
-
+ public Object replaceElements(
+ Object original,
+ Object target,
+ Object owner,
+ Map copyCache,
+ SessionImplementor session) {
// TODO: does not work for EntityMode.DOM4J yet!
-
- java.util.Collection result = (java.util.Collection) target;
-
+ java.util.Collection result = ( java.util.Collection ) target;
+ final boolean isPC = ( result instanceof PersistentCollection );
+ final boolean wasOriginalDirty = ( original instanceof PersistentCollection &&
( ( PersistentCollection ) original ).isDirty() );
result.clear();
// copy elements into newly empty target collection
@@ -414,9 +424,12 @@
while ( iter.hasNext() ) {
result.add( elemType.replace( iter.next(), null, session, owner, copyCache ) );
}
-
+
+ if ( result instanceof PersistentCollection && !wasOriginalDirty ) {
+ ( ( PersistentCollection ) result ).clearDirty();
+ }
+
return result;
-
}
/**
@@ -439,24 +452,25 @@
*
* @param anticipatedSize The anticipated size of the instaniated collection
* after we are done populating it.
+ * @return A newly instantiated collection to be wrapped.
*/
public abstract Object instantiate(int anticipatedSize);
+ /**
+ * {@inheritDoc}
+ */
public Object replace(
- final Object original,
+ final Object original,
final Object target,
- final SessionImplementor session,
- final Object owner,
- final Map copyCache)
- throws HibernateException {
-
+ final SessionImplementor session,
+ final Object owner,
+ final Map copyCache) throws HibernateException {
if ( original == null ) {
return null;
}
if ( !Hibernate.isInitialized( original ) ) {
return target;
}
- //if ( original == target ) return target; // can't do this, since need to merge
element references
// for a null target, or a target which is the same as the original, we
// need to put the merged elements in a new collection
@@ -465,7 +479,7 @@
//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
//TODO: this is a little inefficient, don't need to do a whole
@@ -473,9 +487,8 @@
replaceElements( result, target, owner, copyCache, session );
result = target;
}
-
+
return result;
-
}
/**
Modified: trunk/Hibernate3/test/org/hibernate/test/ops/MergeTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ops/MergeTest.java 2006-11-06 22:00:41 UTC
(rev 10739)
+++ trunk/Hibernate3/test/org/hibernate/test/ops/MergeTest.java 2006-11-06 22:13:39 UTC
(rev 10740)
@@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Date;
+import java.util.Iterator;
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -25,6 +26,167 @@
super(str);
}
+ public void testNoExtraUpdatesOnMerge() throws Exception {
+ Session s = openSession();
+ s.beginTransaction();
+ Node node = new Node( "test" );
+ s.persist( node );
+ s.getTransaction().commit();
+ s.close();
+
+ clearCounts();
+
+ // node 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();
+ node = ( Node ) s.merge( node );
+ s.getTransaction().commit();
+ s.close();
+
+ assertUpdateCount( 0 );
+ assertInsertCount( 0 );
+
+ ///////////////////////////////////////////////////////////////////////
+ // as a control measure, now update the node while it is detached and
+ // make sure we get an update as a result...
+ node.setDescription( "new description" );
+ s = openSession();
+ s.beginTransaction();
+ node = ( Node ) s.merge( node );
+ s.getTransaction().commit();
+ s.close();
+ assertUpdateCount( 1 );
+ assertInsertCount( 0 );
+ ///////////////////////////////////////////////////////////////////////
+
+ cleanup();
+ }
+
+ public void testNoExtraUpdatesOnMergeWithCollection() throws Exception {
+ Session s = openSession();
+ s.beginTransaction();
+ Node parent = new Node( "parent" );
+ Node child = new Node( "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();
+ parent = ( Node ) s.merge( parent );
+ s.getTransaction().commit();
+ s.close();
+
+ assertUpdateCount( 0 );
+ assertInsertCount( 0 );
+
+ ///////////////////////////////////////////////////////////////////////
+ // as a control measure, now update the node while it is detached and
+ // make sure we get an update as a result...
+ ( ( Node ) parent.getChildren().iterator().next() ).setDescription( "child's
new description" );
+ parent.getChildren().add( new Node( "second child" ) );
+ s = openSession();
+ s.beginTransaction();
+ parent = ( Node ) s.merge( parent );
+ s.getTransaction().commit();
+ s.close();
+ assertUpdateCount( 1 );
+ assertInsertCount( 1 );
+ ///////////////////////////////////////////////////////////////////////
+
+ cleanup();
+ }
+
+ public void testNoExtraUpdatesOnMergeVersioned() throws Exception {
+ Session s = openSession();
+ s.beginTransaction();
+ VersionedEntity entity = new VersionedEntity( "entity", "entity"
);
+ s.persist( entity );
+ s.getTransaction().commit();
+ s.close();
+
+ clearCounts();
+
+ // entity 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();
+ VersionedEntity mergedEntity = ( VersionedEntity ) s.merge( entity );
+ s.getTransaction().commit();
+ s.close();
+
+ assertUpdateCount( 0 );
+ assertInsertCount( 0 );
+ assertEquals( "unexpected version increment", entity.getVersion(),
mergedEntity.getVersion() );
+
+
+ ///////////////////////////////////////////////////////////////////////
+ // as a control measure, now update the node while it is detached and
+ // make sure we get an update as a result...
+ entity.setName( "new name" );
+ s = openSession();
+ s.beginTransaction();
+ entity = ( VersionedEntity ) s.merge( entity );
+ s.getTransaction().commit();
+ s.close();
+ assertUpdateCount( 1 );
+ assertInsertCount( 0 );
+ ///////////////////////////////////////////////////////////////////////
+
+ cleanup();
+ }
+
+ public void testNoExtraUpdatesOnMergeVersionedWithCollection() 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();
+ VersionedEntity mergedParent = ( VersionedEntity ) s.merge( parent );
+ 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 while it is detached and
+ // make sure we get an update as a result...
+ mergedParent.setName( "new name" );
+ mergedParent.getChildren().add( new VersionedEntity( "child2", "new
child" ) );
+ s = openSession();
+ s.beginTransaction();
+ parent = ( VersionedEntity ) s.merge( mergedParent );
+ s.getTransaction().commit();
+ s.close();
+ assertUpdateCount( 1 );
+ assertInsertCount( 1 );
+ ///////////////////////////////////////////////////////////////////////
+
+ cleanup();
+ }
+
public void testPersistThenMergeInSameTxnWithVersion() {
Session s = openSession();
Transaction tx = s.beginTransaction();
@@ -367,16 +529,37 @@
private void clearCounts() {
getSessions().getStatistics().clear();
}
-
- private void assertInsertCount(int count) {
+
+ private void assertInsertCount(int expected) {
int inserts = (int) getSessions().getStatistics().getEntityInsertCount();
- assertEquals(count, inserts);
+ assertEquals( "unexpected insert count", expected, inserts );
}
-
- private void assertUpdateCount(int count) {
+
+ private void assertUpdateCount(int expected) {
int updates = (int) getSessions().getStatistics().getEntityUpdateCount();
- assertEquals(count, updates);
+ assertEquals( "unexpected update counts", expected, updates );
}
+
+ private void cleanup() {
+ Session s = openSession();
+ s.beginTransaction();
+ s.createQuery("delete from NumberedNode where parent is not
null").executeUpdate();
+ s.createQuery("delete from NumberedNode").executeUpdate();
+ s.createQuery("delete from Node where parent is not null").executeUpdate();
+ s.createQuery("delete from Node").executeUpdate();
+ s.createQuery("delete from VersionedEntity where parent is not
null").executeUpdate();
+ s.createQuery("delete from VersionedEntity").executeUpdate();
+ s.createQuery("delete from TimestampedEntity").executeUpdate();
+
+ Iterator itr = s.createQuery( "from Employer" ).list().iterator();
+ while ( itr.hasNext() ) {
+ final Employer employer = ( Employer ) itr.next();
+ s.delete( employer );
+ }
+
+ s.getTransaction().commit();
+ s.close();
+ }
protected void configure(Configuration cfg) {
cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
Modified: trunk/Hibernate3/test/org/hibernate/test/ops/OptLockEntity.hbm.xml
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ops/OptLockEntity.hbm.xml 2006-11-06 22:00:41
UTC (rev 10739)
+++ trunk/Hibernate3/test/org/hibernate/test/ops/OptLockEntity.hbm.xml 2006-11-06 22:13:39
UTC (rev 10740)
@@ -15,6 +15,13 @@
</id>
<version name="version" column="VERS"
type="long" />
<property name="name" column="NAME"
type="string" />
+ <many-to-one name="parent" class="VersionedEntity"/>
+ <set name="children"
+ inverse="true"
+ cascade="persist,merge,save-update,evict">
+ <key column="parent"/>
+ <one-to-many class="VersionedEntity"/>
+ </set>
</class>
<class name="TimestampedEntity" table="T_ENTITY">
Modified: trunk/Hibernate3/test/org/hibernate/test/ops/VersionedEntity.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/ops/VersionedEntity.java 2006-11-06 22:00:41
UTC (rev 10739)
+++ trunk/Hibernate3/test/org/hibernate/test/ops/VersionedEntity.java 2006-11-06 22:13:39
UTC (rev 10740)
@@ -1,7 +1,10 @@
package org.hibernate.test.ops;
+import java.util.Set;
+import java.util.HashSet;
+
/**
- * todo: describe VersionedEntity
+ * VersionedEntity
*
* @author Steve Ebersole
*/
@@ -10,6 +13,9 @@
private String name;
private long version;
+ private VersionedEntity parent;
+ private Set children = new HashSet();
+
public VersionedEntity() {
}
@@ -41,4 +47,20 @@
public void setVersion(long version) {
this.version = version;
}
+
+ public VersionedEntity getParent() {
+ return parent;
+ }
+
+ public void setParent(VersionedEntity parent) {
+ this.parent = parent;
+ }
+
+ public Set getChildren() {
+ return children;
+ }
+
+ public void setChildren(Set children) {
+ this.children = children;
+ }
}