You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by st...@apache.org on 2015/03/16 22:45:43 UTC

svn commit: r1667136 - in /openjpa/trunk: ./ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java

Author: struberg
Date: Mon Mar 16 21:45:43 2015
New Revision: 1667136

URL: http://svn.apache.org/r1667136
Log:
OPENJPA-2335 only handle key columns very restrictive

There is no reason to forbid updates to other Columns like OrderColumn, etc
Ported over from 2.3.x

Modified:
    openjpa/trunk/   (props changed)
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java

Propchange: openjpa/trunk/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Mar 16 21:45:43 2015
@@ -3,4 +3,4 @@
 /openjpa/branches/2.1.x:1415379,1415398,1485010,1513249,1517838,1530146,1533218,1533280,1539188,1569528,1575444,1591536,1636464,1655218,1662610
 /openjpa/branches/2.2.1.x:1415367,1415413,1415425,1504719,1508186,1530347,1533222,1539193,1651808
 /openjpa/branches/2.2.x:1384400,1415459-1415460,1415469,1485013,1530364,1533223,1580898,1580939,1591681,1631786,1666312
-/openjpa/branches/2.3.x:1533462,1535560,1564121
+/openjpa/branches/2.3.x:1533462,1535560,1540277,1564121

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java?rev=1667136&r1=1667135&r2=1667136&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java Mon Mar 16 21:45:43 2015
@@ -331,43 +331,50 @@ public class PrimaryRow
         boolean overrideDefault)
         throws SQLException {
         // make sure we're not setting two different values
-    	// unless the given column is an implicit relationship and value
-    	// changes from logical default to non-default
+        // unless the given column is an implicit relationship and value
+        // changes from logical default to non-default
         Object prev = getSet(col);
         if (prev != null) {
             if (prev == NULL)
                 prev = null;
             if (!rowValueEquals(prev, val)) {
-            	if (isDefaultValue(prev) || allowsUpdate(col, prev, val)) {
-            		super.setObject(col, val, metaType, overrideDefault);
-            	} else if (!isDefaultValue(prev)) {
-            		throw new InvalidStateException(_loc.get("diff-values",
-            				new Object[]{ col.getFullDBIdentifier().getName(),
-                            (prev == null) ? null : prev.getClass(), prev,
-                            (val == null) ? null : val.getClass(), val })).
-            				setFatal(true);
-            	} else {
-            	    // since not allow to update and the new value is 0 or null,
-            	    // just return.
-            	    return;
-            	}
+                if (isDefaultValue(prev) || allowsUpdate(col, prev, val)) {
+                    super.setObject(col, val, metaType, overrideDefault);
+                    return;
+                } else if (!isDefaultValue(val)) {
+                    throw new InvalidStateException(_loc.get("diff-values",
+                            new Object[]{ col.getFullDBIdentifier().getName(),
+                                    (prev == null) ? null : prev.getClass(), prev,
+                                    (val == null) ? null : val.getClass(), val })).
+                            setFatal(true);
+                } else {
+                    // since not allow to update and the new value is 0 or null,
+                    // just return.
+                    return;
+                }
             }
         }
         super.setObject(col, val, metaType, overrideDefault);
     }
     
     /**
-     * Allow the given column value to be updated only if old or current value
-     * is a default value or was not set and the column is not a primary key.
+     * Allow the given key column value to be updated if the old value is a default value
+     * or the new value is default.
+     * For primary keys we even disallow setting the current value to default
      */
     boolean allowsUpdate(Column col, Object old, Object cur) {
-    	return ((!col.isPrimaryKey() && col.isImplicitRelation()) ||
-    	   col.isUni1MFK()) && (isDefaultValue(old));
+        if (col.isPrimaryKey() && isDefaultValue(old) && !isDefaultValue(cur)) {
+            // for primary keys we disallow re-setting it to default
+            return false;
+        }
+
+        return !(col.isPrimaryKey() || col.isRelationId() || col.isImplicitRelation() || col.isUni1MFK())
+               || isDefaultValue(old) || isDefaultValue(cur);
     }
     
     boolean isDefaultValue(Object val) {
-    	return val == null || val == NULL
-    	    || (val instanceof Number && ((Number)val).longValue() == 0);
+        return val == null || val == NULL
+                || (val instanceof Number && ((Number)val).longValue() == 0);
     }
 
     /**

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java?rev=1667136&r1=1667135&r2=1667136&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java Mon Mar 16 21:45:43 2015
@@ -21,6 +21,7 @@ package org.apache.openjpa.persistence;
 import javax.persistence.EntityManager;
 import javax.persistence.spi.LoadState;
 
+import junit.framework.Assert;
 import org.apache.openjpa.persistence.entity.EntityA;
 import org.apache.openjpa.persistence.entity.EntityB;
 import org.apache.openjpa.persistence.entity.EntityC;
@@ -55,4 +56,44 @@ public class TestOpenJPA2330 extends Sin
 
         em.close();
     }
+
+    public void testOpenJPA2335() {
+        EntityManager em = emf.createEntityManager();
+
+        em.getTransaction().begin();
+        EntityA a = new EntityA();
+
+        EntityB b1 = new EntityB(a);
+        b1.setName("b1");
+
+        EntityB b2 = new EntityB(a);
+        b2.setName("b2");
+
+        EntityB b3 = new EntityB(a);
+        b3.setName("b3");
+
+        EntityB b4 = new EntityB(a);
+        b4.setName("b4");
+
+        a.getBs().add(b1);
+        a.getBs().add(b2);
+        a.getBs().add(b3);
+        a.getBs().add(b4);
+
+        em.persist(a);
+
+        em.getTransaction().commit();
+        em.close();
+
+        // now read all back in
+        em = emf.createEntityManager();
+        em.getTransaction().begin();
+        EntityA a2 = em.find(EntityA.class, a.getId());
+        Assert.assertNotNull(a2);
+        Assert.assertNotNull(a2.getBs());
+        Assert.assertEquals(4, a2.getBs().size());
+
+        em.getTransaction().commit();
+        em.close();
+    }
 }