cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johnth...@apache.org
Subject [cayenne] branch master updated: Optimistic locking - Fix NPE when locking on a null value for a to-one relationship
Date Mon, 13 May 2019 19:13:01 GMT
This is an automated email from the ASF dual-hosted git repository.

johnthuss 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 56d1198  Optimistic locking - Fix NPE when locking on a null value for a to-one relationship
56d1198 is described below

commit 56d11983ede65c1dc9ce27c4a062f9fbe2330573
Author: John Huss <johnthuss@apache.org>
AuthorDate: Mon May 13 11:38:33 2019 -0500

    Optimistic locking - Fix NPE when locking on a null value for a to-one relationship
---
 .../access/flush/OptimisticLockQualifierBuilder.java |  5 ++++-
 .../apache/cayenne/access/OptimisticLockingIT.java   | 20 ++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/flush/OptimisticLockQualifierBuilder.java
b/cayenne-server/src/main/java/org/apache/cayenne/access/flush/OptimisticLockQualifierBuilder.java
index 1aa7b19..fa3bdb3 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/flush/OptimisticLockQualifierBuilder.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/flush/OptimisticLockQualifierBuilder.java
@@ -70,8 +70,11 @@ class OptimisticLockQualifierBuilder implements PropertyVisitor {
             for(DbJoin join : dbRelationship.getJoins()) {
                 DbAttribute source = join.getSource();
                 if(!source.isPrimaryKey()) {
+                    Object valueObjectId = value != null 
+                        ? ObjectIdValueSupplier.getFor(value, join.getTargetName()) 
+                        : null;
                     dbRow.getQualifier()
-                            .addAdditionalQualifier(source, ObjectIdValueSupplier.getFor(value,
join.getTargetName()), true);
+                            .addAdditionalQualifier(source, valueObjectId, true);
                 }
             }
         }
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/OptimisticLockingIT.java
b/cayenne-server/src/test/java/org/apache/cayenne/access/OptimisticLockingIT.java
index f389f34..14b623c 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/access/OptimisticLockingIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/access/OptimisticLockingIT.java
@@ -441,6 +441,26 @@ public class OptimisticLockingIT extends ServerCase {
     }
 
     @Test
+    public void testSuccessLockingOnToOneNull() throws Exception {
+        createLockingOnToOneDataSet();
+
+        List<RelLockingTestEntity> allObjects = new SelectQuery<>(
+                RelLockingTestEntity.class).select(context);
+        assertEquals(1, allObjects.size());
+
+        RelLockingTestEntity object = allObjects.get(0);
+
+        // set to-one relationship to null and save
+        SimpleLockingTestEntity object1 = object.getToSimpleLockingTest();
+        object.setToSimpleLockingTest(null);
+        context.commitChanges();
+
+        // change to-one relationship to non-null and save... should lock on null value
+        object.setToSimpleLockingTest(object1);
+        context.commitChanges();
+    }
+    
+    @Test
     public void testFailLockingOnToOne() throws Exception {
         createLockingOnToOneDataSet();
 


Mime
View raw message