openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fa...@apache.org
Subject svn commit: r729180 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/
Date Wed, 24 Dec 2008 00:18:32 GMT
Author: fancy
Date: Tue Dec 23 16:18:32 2008
New Revision: 729180

URL: http://svn.apache.org/viewvc?rev=729180&view=rev
Log:
OPENJPA-557 Primary key sequences broken with postgres schemas
Commit patch provided by Milosz Tylenda

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog3.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog4.java
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog1.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog2.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable2.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable3.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable4.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestMultipleSchemaNames.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java?rev=729180&r1=729179&r2=729180&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PostgresDictionary.java
Tue Dec 23 16:18:32 2008
@@ -113,9 +113,8 @@
         requiresAliasForSubselect = true;
         allowsAliasInBulkClause = false;
 
-        // {2} is the result of getGeneratedKeySequenceName; the
         // single-quote escape will result in SELECT CURVAL('mysequence')
-        lastGeneratedKeyQuery = "SELECT CURRVAL(''{2}'')";
+        lastGeneratedKeyQuery = "SELECT CURRVAL(''{1}_{0}_seq'')";
         supportsAutoAssign = true;
         autoAssignTypeName = "BIGSERIAL";
         nextSequenceQuery = "SELECT NEXTVAL(''{0}'')";

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog1.java?rev=729180&r1=729179&r2=729180&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog1.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog1.java
Tue Dec 23 16:18:32 2008
@@ -21,9 +21,6 @@
 import javax.persistence.*;
 import java.io.*;
 
-/**
- * Extension of Animal class illustrating inheritance.
- */
 @Entity(name = "Dog1")
 @Table(name = "DOGAUTO", schema = "SCHEMA1")
 public class Dog1 implements Serializable

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog2.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog2.java?rev=729180&r1=729179&r2=729180&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog2.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog2.java
Tue Dec 23 16:18:32 2008
@@ -21,9 +21,6 @@
 import javax.persistence.*;
 import java.io.*;
 
-/**
- * Extension of Animal class illustrating inheritance.
- */
 @Entity(name = "Dog2")
 @Table(name = "DOGAUTO", schema = "SCHEMA2")
 public class Dog2 implements Serializable

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog3.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog3.java?rev=729180&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog3.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog3.java
Tue Dec 23 16:18:32 2008
@@ -0,0 +1,51 @@
+/*
+ * 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.generationtype;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity(name = "Dog3")
+@Table(name = "DOGIDENT", schema = "SCHEMA1")
+public class Dog3 {
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private int id;
+
+    private String name;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog4.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog4.java?rev=729180&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog4.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/Dog4.java
Tue Dec 23 16:18:32 2008
@@ -0,0 +1,51 @@
+/*
+ * 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.generationtype;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity(name = "Dog4")
+@Table(name = "DOGIDENT4")
+public class Dog4 {
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private int id;
+
+    private String name;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable.java?rev=729180&r1=729179&r2=729180&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable.java
Tue Dec 23 16:18:32 2008
@@ -22,9 +22,6 @@
 import javax.persistence.*;
 import java.io.*;
 
-/**
- * Extension of Animal class illustrating inheritance.
- */
 @Entity(name = "DogTable")
 @Table(name = "DOGTABLES", schema = "SCHEMA1")
 public class DogTable implements Serializable

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable2.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable2.java?rev=729180&r1=729179&r2=729180&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable2.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable2.java
Tue Dec 23 16:18:32 2008
@@ -21,9 +21,6 @@
 import java.io.*;
 import javax.persistence.*;
 
-/**
- * Extension of Animal class illustrating inheritance.
- */
 @Entity(name = "DogTable2")
 @Table(name = "DOGTABLES", schema = "SCHEMA2")
 public class DogTable2 implements Serializable

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable3.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable3.java?rev=729180&r1=729179&r2=729180&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable3.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable3.java
Tue Dec 23 16:18:32 2008
@@ -21,9 +21,6 @@
 import java.io.*;
 import javax.persistence.*;
 
-/**
- * Extension of Animal class illustrating inheritance.
- */
 @Entity(name = "DogTable3")
 @Table(name = "DOGTABLES", schema = "SCHEMA3")
 public class DogTable3 implements Serializable

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable4.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable4.java?rev=729180&r1=729179&r2=729180&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable4.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/DogTable4.java
Tue Dec 23 16:18:32 2008
@@ -21,9 +21,6 @@
 import java.io.*;
 import javax.persistence.*;
 
-/**
- * Extension of Animal class illustrating inheritance.
- */
 @Entity(name = "DogTable4")
 @Table(name = "DOGTABLES4")
 public class DogTable4 implements Serializable

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestMultipleSchemaNames.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestMultipleSchemaNames.java?rev=729180&r1=729179&r2=729180&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestMultipleSchemaNames.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestMultipleSchemaNames.java
Tue Dec 23 16:18:32 2008
@@ -19,15 +19,31 @@
 package org.apache.openjpa.persistence.generationtype;
 
 import java.util.List;
+
 import javax.persistence.EntityManager;
+import javax.persistence.PersistenceException;
 import javax.persistence.Query;
-import org.apache.openjpa.persistence.*;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.jdbc.sql.PostgresDictionary;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
+import org.apache.openjpa.persistence.OpenJPAPersistence;
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
 
 public class TestMultipleSchemaNames extends SingleEMFTestCase {
+
     public void setUp() {
-        setUp(Dog1.class, Dog2.class, DogTable.class, DogTable2.class,
-                DogTable3.class, DogTable4.class);
+        // Create schemas when database requires this and we are about
+        // to execute the first test.
+        if ("testGeneratedAUTO".equals(getName())) {
+            createSchemas();
+        }
+
+        setUp(Dog1.class, Dog2.class, Dog3.class, Dog4.class,
+            DogTable.class, DogTable2.class, DogTable3.class, DogTable4.class);
 
         EntityManager em = emf.createEntityManager();
         em.getTransaction().begin();
@@ -77,6 +93,22 @@
             em.remove(Obj);
         }
 
+        Query qry7 = em.createQuery("select d from Dog3 d");
+        List result7 = qry7.getResultList();
+
+        for (int index = 0; index < result7.size(); index++) {
+            Dog3 Obj = (Dog3) result7.get(index);
+            em.remove(Obj);
+        }
+
+        Query qry8 = em.createQuery("select d from Dog4 d");
+        List result8 = qry8.getResultList();
+
+        for (int index = 0; index < result8.size(); index++) {
+            Dog4 Obj = (Dog4) result8.get(index);
+            em.remove(Obj);
+        }
+
         Query delschema1 = em
                 .createNativeQuery("delete from schema1.openjpa_sequence_table");
         delschema1.executeUpdate();
@@ -321,4 +353,83 @@
         em.getTransaction().commit();
         em.close();
     }
+    
+    public void testGeneratedIDENTITY() {
+        EntityManager em = emf.createEntityManager();
+        OpenJPAEntityManager kem = OpenJPAPersistence.cast(em);
+
+        // Dog3 is a schema dog.
+        em.getTransaction().begin();
+        Dog3 dog30 = new Dog3();
+        dog30.setName("Dog30");
+        em.persist(dog30);
+        
+        Dog3 dog31 = new Dog3();
+        dog31.setName("Dog31");
+        em.persist(dog31);
+        em.getTransaction().commit();
+
+        // We can't assume generated values start with 1 as
+        // the table might have already existed and had some rows.
+        Dog3 dog30x = em.find(Dog3.class, kem.getObjectId(dog30));
+        Dog3 dog31x = em.find(Dog3.class, kem.getObjectId(dog31));
+        assertTrue((dog30x.getId() + 1 == dog31x.getId()) ||
+            (dog30x.getId() == dog31x.getId() + 1));
+        assertEquals(dog30x.getName(), "Dog30");
+
+        // Dog4 is a non-schema dog.
+        em.getTransaction().begin();
+        Dog4 dog40 = new Dog4();
+        dog40.setName("Dog40");
+        em.persist(dog40);
+        
+        Dog4 dog41 = new Dog4();
+        dog41.setName("Dog41");
+        em.persist(dog41);
+        em.getTransaction().commit();
+
+        Dog4 dog40x = em.find(Dog4.class, kem.getObjectId(dog40));
+        Dog4 dog41x = em.find(Dog4.class, kem.getObjectId(dog41));
+        assertTrue((dog40x.getId() + 1 == dog41x.getId()) ||
+            (dog40x.getId() == dog41x.getId() + 1));
+        assertEquals(dog40x.getName(), "Dog40");
+
+        em.close();
+    }
+    
+    /**
+     * Create necessary schemas if running on PostgreSQL as it does
+     * not create them automatically.
+     * Oracle and MySQL also don't create schemas automatically but
+     * we give up as they treat schemas in special ways.
+     */
+    private void createSchemas() {
+        OpenJPAEntityManagerFactorySPI emf = createEMF();
+        OpenJPAEntityManagerSPI em = emf.createEntityManager();
+        DBDictionary dict = ((JDBCConfiguration) em.getConfiguration())
+            .getDBDictionaryInstance();
+
+        if (!(dict instanceof PostgresDictionary)) {
+            closeEMF(emf);
+            return;
+        }
+        
+        String[] schemas =
+            { "SCHEMA1", "SCHEMA2", "SCHEMA3", "SCHEMA3G", "SCHEMA4G" };
+        for (String schema : schemas) {
+            try {
+                em.getTransaction().begin();
+                Query q = em.createNativeQuery("create schema " + schema);
+                q.executeUpdate();
+                em.getTransaction().commit();
+            } catch (PersistenceException e) {
+                System.err.println("Exception caught while creating schema "
+                    + schema + ". Schema already exists? Message: "
+                    + e.getMessage());
+                em.getTransaction().rollback();
+            }
+        }
+        closeEMF(emf);
+    }
+
 } // end of TestMultipleSchemaNames



Mime
View raw message