cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [1/3] cayenne git commit: CAY-2256 Cannot Save/Insert an Object With null Flattened (complex) toOne Relationship
Date Fri, 10 Mar 2017 07:27:22 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 458fb96b4 -> cf9d83b4a


CAY-2256 Cannot Save/Insert an Object With null Flattened (complex) toOne Relationship


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/007dc276
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/007dc276
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/007dc276

Branch: refs/heads/master
Commit: 007dc276e3ecfa2f5dbee2be7b832decc7e77477
Parents: 448c540
Author: Matt Watson <matt@swarmbox.biz>
Authored: Mon Mar 6 12:03:02 2017 -0800
Committer: Matt Watson <matt@swarmbox.biz>
Committed: Mon Mar 6 12:03:02 2017 -0800

----------------------------------------------------------------------
 .../org/apache/cayenne/CayenneDataObject.java   |  2 +-
 .../cayenne/FlattenedRelationshipsIT.java       | 47 ++++++++++++++++++--
 .../relationships_flattened/FlattenedTest5.java |  7 +++
 .../auto/_FlattenedTest1.java                   | 14 ++++++
 .../auto/_FlattenedTest5.java                   | 38 ++++++++++++++++
 .../resources/relationships-flattened.map.xml   | 26 +++++++++++
 6 files changed, 129 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/007dc276/cayenne-server/src/main/java/org/apache/cayenne/CayenneDataObject.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/CayenneDataObject.java b/cayenne-server/src/main/java/org/apache/cayenne/CayenneDataObject.java
index 401f14b..5e929f3 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/CayenneDataObject.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/CayenneDataObject.java
@@ -667,7 +667,7 @@ public class CayenneDataObject extends PersistentObject implements DataObject,
V
 				continue;
 			}
 
-			if (dbRels.size() > 1 && (relationship.isToMany() || relationship.isReadOnly()))
{
+			if (dbRels.size() > 1 && (!relationship.isToDependentEntity() || relationship.isReadOnly()))
{
 				continue;
 			}
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/007dc276/cayenne-server/src/test/java/org/apache/cayenne/FlattenedRelationshipsIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/FlattenedRelationshipsIT.java
b/cayenne-server/src/test/java/org/apache/cayenne/FlattenedRelationshipsIT.java
index 000e073..7a10d10 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/FlattenedRelationshipsIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/FlattenedRelationshipsIT.java
@@ -23,6 +23,7 @@ import org.apache.cayenne.access.DataContext;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.exp.Expression;
 import org.apache.cayenne.exp.ExpressionFactory;
+import org.apache.cayenne.query.ObjectSelect;
 import org.apache.cayenne.query.SQLTemplate;
 import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.test.jdbc.DBHelper;
@@ -31,19 +32,18 @@ import org.apache.cayenne.testdo.relationships_flattened.FlattenedCircular;
 import org.apache.cayenne.testdo.relationships_flattened.FlattenedTest1;
 import org.apache.cayenne.testdo.relationships_flattened.FlattenedTest2;
 import org.apache.cayenne.testdo.relationships_flattened.FlattenedTest3;
+import org.apache.cayenne.testdo.relationships_flattened.FlattenedTest5;
 import org.apache.cayenne.unit.di.server.CayenneProjects;
 import org.apache.cayenne.unit.di.server.ServerCase;
 import org.apache.cayenne.unit.di.server.UseServerRuntime;
+import org.apache.cayenne.validation.ValidationResult;
 import org.junit.Before;
 import org.junit.Test;
 
 import java.sql.Types;
 import java.util.List;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 /**
  * Test case for objects with flattened relationships.
@@ -291,4 +291,43 @@ public class FlattenedRelationshipsIT extends ServerCase {
         assertTrue(side1s.isEmpty());
     }
 
+    /**
+     * Should be able to save/insert an object with flattened (complex) toOne relationship
+     * @throws Exception
+     */
+    @Test
+    public void testFlattenedComplexToOneRelationship() throws Exception {
+        FlattenedTest1 ft1 = context.newObject(FlattenedTest1.class);
+        ft1.setName("FT1");
+
+        FlattenedTest5 ft5 = context.newObject(FlattenedTest5.class);
+        ft5.setName("FT5");
+        ft5.setToFT1(ft1);
+
+        context.commitChanges();
+
+        FlattenedTest5 ft5Persisted = ObjectSelect.query(FlattenedTest5.class).selectFirst(context);
+        assertEquals(ft1, ft5Persisted.getToFT1());
+    }
+
+    /**
+     * Should be able to save/insert an object with null flattened (complex) toOne relationship
+     * @throws Exception
+     */
+    @Test
+    public void testNullFlattenedComplexToOneRelationship() throws Exception {
+        FlattenedTest5 ft5 = context.newObject(FlattenedTest5.class);
+        ft5.setName("FT5");
+
+        // should be valid for save
+        ValidationResult validationResult = new ValidationResult();
+        ft5.validateForSave(validationResult);
+
+        assertTrue(validationResult.toString(), validationResult.getFailures().isEmpty());
+
+        context.commitChanges();
+
+        assertEquals(1, ObjectSelect.query(FlattenedTest5.class).selectCount(context));
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/007dc276/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/FlattenedTest5.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/FlattenedTest5.java
b/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/FlattenedTest5.java
new file mode 100644
index 0000000..d2ad619
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/FlattenedTest5.java
@@ -0,0 +1,7 @@
+package org.apache.cayenne.testdo.relationships_flattened;
+
+import org.apache.cayenne.testdo.relationships_flattened.auto._FlattenedTest5;
+
+public class FlattenedTest5 extends _FlattenedTest5 {
+
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/007dc276/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/auto/_FlattenedTest1.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/auto/_FlattenedTest1.java
b/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/auto/_FlattenedTest1.java
index 15c551c..a215d50 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/auto/_FlattenedTest1.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/auto/_FlattenedTest1.java
@@ -7,6 +7,7 @@ import org.apache.cayenne.exp.Property;
 import org.apache.cayenne.testdo.relationships_flattened.FlattenedTest2;
 import org.apache.cayenne.testdo.relationships_flattened.FlattenedTest3;
 import org.apache.cayenne.testdo.relationships_flattened.FlattenedTest4;
+import org.apache.cayenne.testdo.relationships_flattened.FlattenedTest5;
 
 /**
  * Class _FlattenedTest1 was generated by Cayenne.
@@ -25,6 +26,7 @@ public abstract class _FlattenedTest1 extends CayenneDataObject {
     public static final Property<List<FlattenedTest3>> FT3ARRAY = Property.create("ft3Array",
List.class);
     public static final Property<List<FlattenedTest3>> FT3OVER_COMPLEX = Property.create("ft3OverComplex",
List.class);
     public static final Property<List<FlattenedTest4>> FT4ARRAY_FOR1 = Property.create("ft4ArrayFor1",
List.class);
+    public static final Property<List<FlattenedTest5>> FT5ARRAY = Property.create("ft5Array",
List.class);
 
     public void setName(String name) {
         writeProperty("name", name);
@@ -69,4 +71,16 @@ public abstract class _FlattenedTest1 extends CayenneDataObject {
     }
 
 
+    public void addToFt5Array(FlattenedTest5 obj) {
+        addToManyTarget("ft5Array", obj, true);
+    }
+    public void removeFromFt5Array(FlattenedTest5 obj) {
+        removeToManyTarget("ft5Array", obj, true);
+    }
+    @SuppressWarnings("unchecked")
+    public List<FlattenedTest5> getFt5Array() {
+        return (List<FlattenedTest5>)readProperty("ft5Array");
+    }
+
+
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/007dc276/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/auto/_FlattenedTest5.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/auto/_FlattenedTest5.java
b/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/auto/_FlattenedTest5.java
new file mode 100644
index 0000000..3233a3d
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/testdo/relationships_flattened/auto/_FlattenedTest5.java
@@ -0,0 +1,38 @@
+package org.apache.cayenne.testdo.relationships_flattened.auto;
+
+import org.apache.cayenne.CayenneDataObject;
+import org.apache.cayenne.exp.Property;
+import org.apache.cayenne.testdo.relationships_flattened.FlattenedTest1;
+
+/**
+ * Class _FlattenedTest5 was generated by Cayenne.
+ * It is probably a good idea to avoid changing this class manually,
+ * since it may be overwritten next time code is regenerated.
+ * If you need to make any customizations, please use subclass.
+ */
+public abstract class _FlattenedTest5 extends CayenneDataObject {
+
+    private static final long serialVersionUID = 1L; 
+
+    public static final String FT5_ID_PK_COLUMN = "FT5_ID";
+
+    public static final Property<String> NAME = Property.create("name", String.class);
+    public static final Property<FlattenedTest1> TO_FT1 = Property.create("toFT1",
FlattenedTest1.class);
+
+    public void setName(String name) {
+        writeProperty("name", name);
+    }
+    public String getName() {
+        return (String)readProperty("name");
+    }
+
+    public void setToFT1(FlattenedTest1 toFT1) {
+        setToOneTarget("toFT1", toFT1, true);
+    }
+
+    public FlattenedTest1 getToFT1() {
+        return (FlattenedTest1)readProperty("toFT1");
+    }
+
+
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/007dc276/cayenne-server/src/test/resources/relationships-flattened.map.xml
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/resources/relationships-flattened.map.xml b/cayenne-server/src/test/resources/relationships-flattened.map.xml
index cc32c8f..d5a0ee5 100644
--- a/cayenne-server/src/test/resources/relationships-flattened.map.xml
+++ b/cayenne-server/src/test/resources/relationships-flattened.map.xml
@@ -10,6 +10,11 @@
 		<db-attribute name="FT3_FK" type="INTEGER"/>
 		<db-attribute name="PK" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
 	</db-entity>
+	<db-entity name="COMPLEX_JOIN2">
+		<db-attribute name="FT1_FK" type="INTEGER" isMandatory="true"/>
+		<db-attribute name="FT5_FK" type="INTEGER" isMandatory="true"/>
+		<db-attribute name="PK" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
+	</db-entity>
 	<db-entity name="FLATTENED_CIRCULAR">
 		<db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
 	</db-entity>
@@ -36,6 +41,10 @@
 		<db-attribute name="FT4_ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
 		<db-attribute name="NAME" type="VARCHAR" isMandatory="true" length="100"/>
 	</db-entity>
+	<db-entity name="FLATTENED_TEST_5">
+		<db-attribute name="FT5_ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
+		<db-attribute name="NAME" type="VARCHAR" isMandatory="true" length="100"/>
+	</db-entity>
 	<obj-entity name="FlattenedCircular" className="org.apache.cayenne.testdo.relationships_flattened.FlattenedCircular"
dbEntityName="FLATTENED_CIRCULAR">
 	</obj-entity>
 	<obj-entity name="FlattenedTest1" className="org.apache.cayenne.testdo.relationships_flattened.FlattenedTest1"
dbEntityName="FLATTENED_TEST_1">
@@ -50,12 +59,21 @@
 	<obj-entity name="FlattenedTest4" className="org.apache.cayenne.testdo.relationships_flattened.FlattenedTest4"
dbEntityName="FLATTENED_TEST_4">
 		<obj-attribute name="name" type="java.lang.String" db-attribute-path="NAME"/>
 	</obj-entity>
+	<obj-entity name="FlattenedTest5" className="org.apache.cayenne.testdo.relationships_flattened.FlattenedTest5"
dbEntityName="FLATTENED_TEST_5">
+		<obj-attribute name="name" type="java.lang.String" db-attribute-path="NAME"/>
+	</obj-entity>
 	<db-relationship name="toFT1" source="COMPLEX_JOIN" target="FLATTENED_TEST_1" toMany="false">
 		<db-attribute-pair source="FT1_FK" target="FT1_ID"/>
 	</db-relationship>
 	<db-relationship name="toFT3" source="COMPLEX_JOIN" target="FLATTENED_TEST_3" toMany="false">
 		<db-attribute-pair source="FT3_FK" target="FT3_ID"/>
 	</db-relationship>
+	<db-relationship name="toFT1" source="COMPLEX_JOIN2" target="FLATTENED_TEST_1" toMany="false">
+		<db-attribute-pair source="FT1_FK" target="FT1_ID"/>
+	</db-relationship>
+	<db-relationship name="toFT5" source="COMPLEX_JOIN2" target="FLATTENED_TEST_5" toMany="false">
+		<db-attribute-pair source="FT5_FK" target="FT5_ID"/>
+	</db-relationship>
 	<db-relationship name="side1s" source="FLATTENED_CIRCULAR" target="FLATTENED_CIRCULAR_JOIN"
toDependentPK="true" toMany="true">
 		<db-attribute-pair source="ID" target="SIDE2_ID"/>
 	</db-relationship>
@@ -71,6 +89,9 @@
 	<db-relationship name="complexJoins" source="FLATTENED_TEST_1" target="COMPLEX_JOIN"
toMany="true">
 		<db-attribute-pair source="FT1_ID" target="FT1_FK"/>
 	</db-relationship>
+	<db-relationship name="complexJoins2" source="FLATTENED_TEST_1" target="COMPLEX_JOIN2"
toMany="true">
+		<db-attribute-pair source="FT1_ID" target="FT1_FK"/>
+	</db-relationship>
 	<db-relationship name="ft2Array" source="FLATTENED_TEST_1" target="FLATTENED_TEST_2"
toMany="true">
 		<db-attribute-pair source="FT1_ID" target="FT1_ID"/>
 	</db-relationship>
@@ -92,16 +113,21 @@
 	<db-relationship name="toFT3" source="FLATTENED_TEST_4" target="FLATTENED_TEST_3" toMany="false">
 		<db-attribute-pair source="FT3_ID" target="FT3_ID"/>
 	</db-relationship>
+	<db-relationship name="complexJoin2" source="FLATTENED_TEST_5" target="COMPLEX_JOIN2"
toMany="false">
+		<db-attribute-pair source="FT5_ID" target="FT5_FK"/>
+	</db-relationship>
 	<obj-relationship name="side1s" source="FlattenedCircular" target="FlattenedCircular"
db-relationship-path="side1s.side1"/>
 	<obj-relationship name="side2s" source="FlattenedCircular" target="FlattenedCircular"
db-relationship-path="side2s.side2"/>
 	<obj-relationship name="ft2Array" source="FlattenedTest1" target="FlattenedTest2" db-relationship-path="ft2Array"/>
 	<obj-relationship name="ft3Array" source="FlattenedTest1" target="FlattenedTest3" db-relationship-path="ft2Array.ft3Array"/>
 	<obj-relationship name="ft3OverComplex" source="FlattenedTest1" target="FlattenedTest3"
db-relationship-path="complexJoins.toFT3"/>
 	<obj-relationship name="ft4ArrayFor1" source="FlattenedTest1" target="FlattenedTest4"
db-relationship-path="ft2Array.ft3Array.ft4Array"/>
+	<obj-relationship name="ft5Array" source="FlattenedTest1" target="FlattenedTest5" deleteRule="Deny"
db-relationship-path="complexJoins2.toFT5"/>
 	<obj-relationship name="ft3Array" source="FlattenedTest2" target="FlattenedTest3" db-relationship-path="ft3Array"/>
 	<obj-relationship name="toFT1" source="FlattenedTest2" target="FlattenedTest1" db-relationship-path="toFT1"/>
 	<obj-relationship name="ft4Array" source="FlattenedTest3" target="FlattenedTest4" db-relationship-path="ft4Array"/>
 	<obj-relationship name="toFT1" source="FlattenedTest3" target="FlattenedTest1" db-relationship-path="toFT2.toFT1"/>
 	<obj-relationship name="toFT2" source="FlattenedTest3" target="FlattenedTest2" db-relationship-path="toFT2"/>
 	<obj-relationship name="toFT3" source="FlattenedTest4" target="FlattenedTest3" db-relationship-path="toFT3"/>
+	<obj-relationship name="toFT1" source="FlattenedTest5" target="FlattenedTest1" deleteRule="Nullify"
db-relationship-path="complexJoin2.toFT1"/>
 </data-map>


Mime
View raw message