Return-Path: X-Original-To: apmail-openjpa-commits-archive@www.apache.org Delivered-To: apmail-openjpa-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 927A710BE3 for ; Sat, 9 Nov 2013 08:21:16 +0000 (UTC) Received: (qmail 96343 invoked by uid 500); 9 Nov 2013 08:21:16 -0000 Delivered-To: apmail-openjpa-commits-archive@openjpa.apache.org Received: (qmail 96279 invoked by uid 500); 9 Nov 2013 08:21:12 -0000 Mailing-List: contact commits-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@openjpa.apache.org Delivered-To: mailing list commits@openjpa.apache.org Received: (qmail 96272 invoked by uid 99); 9 Nov 2013 08:21:12 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 09 Nov 2013 08:21:12 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 09 Nov 2013 08:21:10 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id C89AB23888E7; Sat, 9 Nov 2013 08:20:50 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1540277 - in /openjpa/branches/2.3.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/TestOpenJPA2330.java Date: Sat, 09 Nov 2013 08:20:50 -0000 To: commits@openjpa.apache.org From: struberg@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131109082050.C89AB23888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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(); } }