Author: struberg
Date: Sat Nov 9 08:20:50 2013
New Revision: 1540277
URL: http://svn.apache.org/r1540277
Log:
OPENJPA-2335 only handle key columns very restrictive
There is no reason to forbid updates to other Columns like OrderColumn, etc
Modified:
openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java
openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java
Modified:
openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java
URL:
http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java?rev=1540277&r1=1540276&r2=1540277&view=diff
==============================================================================
---
openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java
(original)
+++
openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java
Sat Nov 9 08:20:50 2013
@@ -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/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java
URL:
http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java?rev=1540277&r1=1540276&r2=1540277&view=diff
==============================================================================
---
openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java
(original)
+++
openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java
Sat Nov 9 08:20:50 2013
@@ -64,15 +64,38 @@ public class TestOpenJPA2330 extends Sin
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();
}
}