openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hthom...@apache.org
Subject svn commit: r1084260 - in /openjpa/branches/1.2.x: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/
Date Tue, 22 Mar 2011 17:25:01 GMT
Author: hthomann
Date: Tue Mar 22 17:25:01 2011
New Revision: 1084260

URL: http://svn.apache.org/viewvc?rev=1084260&view=rev
Log:
OPENJPA-1665: Update BrokerImpl to skip check for duplicate ids when they are auto generated
- merged Rick's changes from trunk

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

Modified: openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java?rev=1084260&r1=1084259&r2=1084260&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
(original)
+++ openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/BrokerImpl.java
Tue Mar 22 17:25:01 2011
@@ -2468,7 +2468,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)
@@ -4776,7 +4776,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/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java?rev=1084260&view=auto
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java
(added)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java
Tue Mar 22 17:25:01 2011
@@ -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/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/AutoIncrementEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java?rev=1084260&view=auto
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java
(added)
+++ openjpa/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java
Tue Mar 22 17:25:01 2011
@@ -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/branches/1.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/auto/TestAutoIncrement.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message