cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r582169 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src: main/java/org/apache/cayenne/access/ test/java/org/apache/cayenne/access/ test/resources/dml/
Date Fri, 05 Oct 2007 09:51:05 GMT
Author: aadamchik
Date: Fri Oct  5 02:51:04 2007
New Revision: 582169

URL: http://svn.apache.org/viewvc?rev=582169&view=rev
Log:
CAY-872 Potential bug deleting uncommited objects from a nested DataContext

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/ChildDiffLoader.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContextWriteTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.NestedDataContextWriteTest.xml

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/ChildDiffLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/ChildDiffLoader.java?rev=582169&r1=582168&r2=582169&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/ChildDiffLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/ChildDiffLoader.java
Fri Oct  5 02:51:04 2007
@@ -19,7 +19,10 @@
 
 package org.apache.cayenne.access;
 
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.ObjectContext;
@@ -139,6 +142,12 @@
     public void arcDeleted(Object nodeId, final Object targetNodeId, Object arcId) {
         final Persistent source = findObject(nodeId);
 
+        // needed as sometime temporary objects are evoked from the context before
+        // changing their relationships
+        if (source == null) {
+            return;
+        }
+
         ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(
                 ((ObjectId) nodeId).getEntityName());
         Property property = descriptor.getProperty(arcId.toString());
@@ -155,7 +164,24 @@
                         && reverseArc.getRelationship().isRuntime();
 
                 Persistent target = findObject(targetNodeId);
-                property.removeTarget(source, target, autoConnectReverse);
+
+                if (target == null) {
+
+                    // this is usually the case when a NEW object was deleted and then its
+                    // relationships were manipulated; so try to locate the object in the
+                    // collection ...
+                    // the performance of this is rather dubious of course...
+                    target = findObjectInCollection(targetNodeId, property
+                            .readProperty(source));
+                }
+
+                if (target == null) {
+                    // ignore?
+                }
+                else {
+                    property.removeTarget(source, target, autoConnectReverse);
+                }
+
                 return false;
             }
 
@@ -174,6 +200,14 @@
         if (object != null) {
             return object;
         }
+        
+        ObjectId id = (ObjectId) nodeId;
+        
+        // this can happen if a NEW object is deleted and after that its relationships are
+        // modified
+        if (id.isTemporary()) {
+            return null;
+        }
 
         // skip context cache lookup, go directly to its channel
         Query query = new ObjectIdQuery((ObjectId) nodeId);
@@ -190,5 +224,19 @@
         }
 
         return (Persistent) objects.get(0);
+    }
+    
+    Persistent findObjectInCollection(Object nodeId, Object toManyHolder) {
+        Collection c = (toManyHolder instanceof Map) ? ((Map) toManyHolder)
+                .values() : (Collection) toManyHolder;
+        Iterator it = c.iterator();
+        while (it.hasNext()) {
+            Persistent o = (Persistent) it.next();
+            if(nodeId.equals(o.getObjectId())) {
+                return o;
+            }
+        }
+        
+        return null;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContextWriteTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContextWriteTest.java?rev=582169&r1=582168&r2=582169&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContextWriteTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/NestedDataContextWriteTest.java
Fri Oct  5 02:51:04 2007
@@ -44,6 +44,27 @@
         return context;
     }
 
+    public void testDeleteNew() throws Exception {
+        deleteTestData();
+        createTestData("testDeleteNew");
+
+        DataContext context = createDataContext();
+        DataContext childContext = context.createChildDataContext();
+
+        Artist a = (Artist) DataObjectUtils
+                .objectForPK(childContext, Artist.class, 33001);
+        Painting p = (Painting) childContext.newObject(Painting.class);
+        p.setPaintingTitle("X");
+        a.addToPaintingArray(p);
+
+        childContext.commitChangesToParent();
+
+        childContext.deleteObject(p);
+        a.removeFromPaintingArray(p);
+        
+        childContext.commitChangesToParent();
+    }
+
     /**
      * A test case for CAY-698 bug.
      */

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.NestedDataContextWriteTest.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.NestedDataContextWriteTest.xml?rev=582169&r1=582168&r2=582169&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.NestedDataContextWriteTest.xml
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.NestedDataContextWriteTest.xml
Fri Oct  5 02:51:04 2007
@@ -118,4 +118,12 @@
 		</constructor-arg>
 	</bean>
 	
+	<bean id="testDeleteNew" class="java.util.ArrayList">
+		<constructor-arg>
+			<list>
+				<ref bean="A1"/>
+			</list>
+		</constructor-arg>
+	</bean>
+	
 </beans>



Mime
View raw message