cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [cayenne] branch master updated: Flush Action issue: transient setting and then NULL-ing of to-one relationship test case from PR #385
Date Tue, 21 May 2019 15:07:13 GMT
This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git


The following commit(s) were added to refs/heads/master by this push:
     new f5d8648  Flush Action issue: transient setting and then NULL-ing of to-one relationship
 test case from PR #385
f5d8648 is described below

commit f5d86483128a12d5c0d7c760d1f512b3ca5598ee
Author: Nikita Timofeev <stariy95@gmail.com>
AuthorDate: Tue May 21 18:00:03 2019 +0300

    Flush Action issue: transient setting and then NULL-ing of to-one relationship
     test case from PR #385
---
 .../java/org/apache/cayenne/access/flush/ArcTarget.java | 10 ++++++++--
 .../cayenne/access/flush/ArcValuesCreationHandler.java  |  2 +-
 .../apache/cayenne/access/flush/operation/Values.java   |  8 ++------
 .../cayenne/CayenneDataObjectRelationshipsIT.java       | 17 +++++++++++++++++
 4 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/flush/ArcTarget.java b/cayenne-server/src/main/java/org/apache/cayenne/access/flush/ArcTarget.java
index e1c9a7f..483127c 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/flush/ArcTarget.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/flush/ArcTarget.java
@@ -35,15 +35,17 @@ class ArcTarget {
     private final ObjectId sourceId;
     private final ObjectId targetId;
     private final ArcId arcId;
+    private final boolean delete;
 
-    ArcTarget(ObjectId sourceId, ObjectId targetId, ArcId arcId) {
+    ArcTarget(ObjectId sourceId, ObjectId targetId, ArcId arcId, boolean delete) {
         this.sourceId = Objects.requireNonNull(sourceId);
         this.targetId = Objects.requireNonNull(targetId);
         this.arcId = Objects.requireNonNull(arcId);
+        this.delete = delete;
     }
 
     ArcTarget getReversed() {
-        return new ArcTarget(targetId, sourceId, arcId.getReverseId());
+        return new ArcTarget(targetId, sourceId, arcId.getReverseId(), delete);
     }
 
     ArcId getArcId() {
@@ -68,6 +70,9 @@ class ArcTarget {
         }
 
         ArcTarget arcTarget = (ArcTarget) o;
+        if(delete != arcTarget.delete) {
+            return false;
+        }
         if (!sourceId.equals(arcTarget.sourceId)) {
             return false;
         }
@@ -82,6 +87,7 @@ class ArcTarget {
         int result = sourceId.hashCode();
         result = 31 * result + targetId.hashCode();
         result = 31 * result + arcId.hashCode();
+        result = 31 * result + (delete ? 1 : 0);
         return result;
     }
 }
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/flush/ArcValuesCreationHandler.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/flush/ArcValuesCreationHandler.java
index 97d4e26..86520d9 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/flush/ArcValuesCreationHandler.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/flush/ArcValuesCreationHandler.java
@@ -70,7 +70,7 @@ class ArcValuesCreationHandler implements GraphChangeHandler {
         if(snapshotId != null) {
             actualTargetId = snapshotId;
         }
-        ArcTarget arcTarget = new ArcTarget((ObjectId) nodeId, actualTargetId, arcId);
+        ArcTarget arcTarget = new ArcTarget((ObjectId) nodeId, actualTargetId, arcId, !created);
         if(factory.getProcessedArcs().contains(arcTarget.getReversed())) {
             return;
         }
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/flush/operation/Values.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/flush/operation/Values.java
index 2996012..91b8e93 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/flush/operation/Values.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/flush/operation/Values.java
@@ -53,22 +53,18 @@ public class Values {
             snapshot = new HashMap<>();
             updatedAttributes = new ArrayList<>();
         }
-        computeSnapshotValue(attribute.getName(), value);
+        snapshot.put(attribute.getName(), value);
         if(!updatedAttributes.contains(attribute)) {
             updatedAttributes.add(attribute);
         }
     }
 
-    private void computeSnapshotValue(String attribute, Object value) {
-        snapshot.putIfAbsent(attribute, value);
-    }
-
     public void merge(Values other) {
         if(this.snapshot == null) {
             this.snapshot = other.snapshot;
             this.updatedAttributes = other.updatedAttributes;
         } else if(other.snapshot != null) {
-            other.snapshot.forEach(this::computeSnapshotValue);
+            other.snapshot.forEach(snapshot::putIfAbsent);
             other.updatedAttributes.forEach(attr -> {
                 if(!updatedAttributes.contains(attr)) {
                     updatedAttributes.add(attr);
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectRelationshipsIT.java
b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectRelationshipsIT.java
index 9b1793b..74aabf5 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectRelationshipsIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/CayenneDataObjectRelationshipsIT.java
@@ -346,4 +346,21 @@ public class CayenneDataObjectRelationshipsIT extends ServerCase {
         artist.setArtistName("updated artist name"); // this will force the commit to actually
execute some SQL
         context.commitChanges();
     }
+    
+    @Test
+    public void testTransientSetAndNullOfToOneRelationship() throws Exception {
+        createArtistWithPaintingDataSet();
+
+        Artist artist = ObjectSelect.query(Artist.class).selectOne(context);
+
+        Painting object2 = context.newObject(Painting.class);
+        object2.setPaintingTitle("Title");
+        object2.setToArtist(artist);
+        object2.setToArtist(null);
+        context.commitChanges();
+        
+        context.invalidateObjects(object2);
+        assertNull(object2.getToArtist());
+    }
+    
 }


Mime
View raw message