You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by cu...@apache.org on 2010/05/12 21:11:15 UTC

svn commit: r943638 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/

Author: curtisr7
Date: Wed May 12 19:11:14 2010
New Revision: 943638

URL: http://svn.apache.org/viewvc?rev=943638&view=rev
Log:
OPENJPA-1665: Update BrokerImpl to skip check for duplicate ids when they are auto generated.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java   (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=943638&r1=943637&r2=943638&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java Wed May 12 19:11:14 2010
@@ -2581,7 +2581,7 @@ public class BrokerImpl
             }
 
             // make sure we don't already have the instance cached
-            checkForDuplicateId(id, obj);
+            checkForDuplicateId(id, obj, meta);
 
             // if had embedded sm, null it
             if (sm != null)
@@ -4952,7 +4952,11 @@ public class BrokerImpl
     /** 
      * This method makes sure we don't already have the instance cached
      */
-    protected void checkForDuplicateId(Object id, Object obj) {
+    protected void checkForDuplicateId(Object id, Object obj, ClassMetaData meta) {
+        FieldMetaData[] pks = meta.getPrimaryKeyFields();
+        if (pks != null && pks.length == 1 && pks[0].getValueStrategy() == ValueStrategies.AUTOASSIGN) {
+            return;
+        }
         StateManagerImpl other = getStateManagerImplById(id, false);
         if (other != null && !other.isDeleted() && !other.isNew())
             throw new ObjectExistsException(_loc.get("cache-exists",

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java?rev=943638&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java Wed May 12 19:11:14 2010
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.jdbc.auto;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+@Entity
+public class AutoIncrementEntity {
+    @Id
+    @GeneratedValue(strategy=GenerationType.IDENTITY)
+    public int id;
+
+    public String somethingElse;
+    
+    public int getId(){
+        return id;
+    }
+    
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java?rev=943638&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java Wed May 12 19:11:14 2010
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.jdbc.auto;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.persistence.test.SingleEMTestCase;
+
+public class TestAutoIncrement extends SingleEMTestCase {
+    public void setUp() {
+        super.setUp(DROP_TABLES, AutoIncrementEntity.class);
+        if (!((JDBCConfiguration) emf.getConfiguration()).getDBDictionaryInstance().supportsAutoAssign) {
+            return;
+        }
+        createZeroIdEntity();
+    }
+
+    public void test() {
+        em.getTransaction().begin();
+        AutoIncrementEntity e1 = em.find(AutoIncrementEntity.class, 0);
+        assertNotNull(e1);
+        AutoIncrementEntity e2 = new AutoIncrementEntity();
+        assertEquals(0, e2.getId());
+        em.persist(e2);
+        em.getTransaction().commit();
+        assertNotEquals(0, e2.getId());
+    }
+    
+    /**
+     * A private worker method that will synthesize an Entity which has an auto generated id that starts at zero.
+     */
+    private void createZeroIdEntity() {
+        em.getTransaction().begin();
+        AutoIncrementEntity aie = new AutoIncrementEntity();
+        em.persist(aie);
+        em.flush();
+        // If the created Entity has a non-zero id, update the Entity to have a zero id.
+        if (aie.getId() != 0) {
+            em.createQuery("UPDATE AutoIncrementEntity a SET a.id = 0 WHERE a.id = :id")
+                .setParameter("id", aie.getId()).executeUpdate();
+        }
+        em.getTransaction().commit();
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java
------------------------------------------------------------------------------
    svn:eol-style = native