You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mt...@apache.org on 2009/05/30 15:22:56 UTC

svn commit: r780260 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/ openjpa-project/src/doc/manual/

Author: mtylenda
Date: Sat May 30 13:22:56 2009
New Revision: 780260

URL: http://svn.apache.org/viewvc?rev=780260&view=rev
Log:
OPENJPA-736: JDBC 3 way of retrieving generated keys - allow user to disable it, doc and test updates.

Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestGenerationType.java
    openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=780260&r1=780259&r2=780260&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java (original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java Sat May 30 13:22:56 2009
@@ -196,7 +196,7 @@
     public boolean supportsAlterTableWithAddColumn = true;
     public boolean supportsAlterTableWithDropColumn = true;
     public boolean supportsComments = false;
-    public boolean supportsGetGeneratedKeys = false;
+    public Boolean supportsGetGeneratedKeys = null;
     public String reservedWords = null;
     public String systemSchemas = null;
     public String systemTables = null;
@@ -435,8 +435,6 @@
                     // JDBC3-only method, so it might throw a 
                     // AbstractMethodError
                     isJDBC3 = metaData.getJDBCMajorVersion() >= 3;
-                    supportsGetGeneratedKeys =
-                            metaData.supportsGetGeneratedKeys();
                 } catch (Throwable t) {
                     // ignore if not JDBC3
                 }
@@ -457,6 +455,17 @@
             // While we have the metaData, set some values from it
             setSupportsDelimitedIds(metaData);
             setDelimitedCase(metaData);
+
+            // Auto-detect generated keys retrieval support
+            // unless user specified it.
+            if (supportsGetGeneratedKeys == null) {
+                if (isJDBC3) {
+                    supportsGetGeneratedKeys =
+                        metaData.supportsGetGeneratedKeys();
+                } else {
+                    supportsGetGeneratedKeys = false;
+                }
+            }
         }
         connected = true;
     }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestGenerationType.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestGenerationType.java?rev=780260&r1=780259&r2=780260&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestGenerationType.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestGenerationType.java Sat May 30 13:22:56 2009
@@ -19,18 +19,23 @@
 package org.apache.openjpa.persistence.identity;
 
 import java.util.List;
+
 import javax.persistence.EntityManager;
 import javax.persistence.EntityTransaction;
 import javax.persistence.Query;
 
-import junit.framework.TestCase;
 import junit.textui.TestRunner;
+
 import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
 import org.apache.openjpa.persistence.OpenJPAEntityManager;
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
 
 /**
- * Simple test case to test the GenerationType for @Id...
+ * Simple test case to test the GenerationType for
+ * {@link javax.persistence.Id}.
+ * Tests both ways of generated keys retrieval: separate query
+ * and JDBC3 {@link java.sql.Statement#getGeneratedKeys}
+ * if the database supports them.
  *
  * @author Kevin Sutter
  */
@@ -38,14 +43,26 @@
     extends SingleEMFTestCase {
 
     public void setUp() {
-        setUp(IdentityGenerationType.class);
+        if (getName().endsWith("WithoutGetGeneratedKeys")) {
+            setUp(IdentityGenerationType.class,
+                "openjpa.jdbc.DBDictionary",
+                "supportsGetGeneratedKeys=false");
+        } else {
+            setUp(IdentityGenerationType.class);
+        }
     }
 
+    /**
+     * Not all databases support GenerationType.IDENTITY column(s).
+     */
+    private boolean supportsAutoAssign() {
+        return ((JDBCConfiguration) emf.getConfiguration())
+            .getDBDictionaryInstance().supportsAutoAssign;
+    }
+    
     public void testCreateEntityManager() {
-        // Not all databases support GenerationType.IDENTITY column(s)
-        if (!((JDBCConfiguration) emf.getConfiguration()).
-            getDBDictionaryInstance().supportsAutoAssign) {
-			return;
+        if (!supportsAutoAssign()) {
+            return;
         }
         EntityManager em = emf.createEntityManager();
 
@@ -64,10 +81,8 @@
     }
 
     public void testPersist() {
-        // Not all databases support GenerationType.IDENTITY column(s)
-        if (!((JDBCConfiguration) emf.getConfiguration()).
-            getDBDictionaryInstance().supportsAutoAssign) {
-			return;
+        if (!supportsAutoAssign()) {
+            return;
         }
         EntityManager em = emf.createEntityManager();
         em.getTransaction().begin();
@@ -77,10 +92,8 @@
     }
 
     public void testQuery() {
-        // Not all databases support GenerationType.IDENTITY column(s)
-        if (!((JDBCConfiguration) emf.getConfiguration()).
-            getDBDictionaryInstance().supportsAutoAssign) {
-			return;
+        if (!supportsAutoAssign()) {
+            return;
         }
         EntityManager em = emf.createEntityManager();
         em.getTransaction().begin();
@@ -98,6 +111,14 @@
         em.close();
     }
 
+    public void testPersistWithoutGetGeneratedKeys() {
+        testPersist();
+    }
+
+    public void testQueryWithoutGetGeneratedKeys() {
+        testQuery();
+    }
+
     public static void main(String[] args) {
         TestRunner.run(TestGenerationType.class);
     }

Modified: openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml?rev=780260&r1=780259&r2=780260&view=diff
==============================================================================
--- openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml (original)
+++ openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml Sat May 30 13:22:56 2009
@@ -1467,6 +1467,9 @@
 automatically generated key for an auto-increment column. For example, 
 <literal>"SELECT LAST_INSERT_ID()"</literal> for MySQL. This property is set
 automatically in the dictionary, and should not need to be overridden.
+If <literal>SupportsGetGeneratedKeys</literal> is true, the query will not
+be issued but a more efficient JDBC 3.0 mechanism for obtaining generated
+keys will be used instead.
                     </para>
                 </listitem>
                 <listitem id="DBDictionary.LongVarbinaryTypeName">
@@ -2320,6 +2323,30 @@
 keys. Defaults to <literal>true</literal>.
                     </para>
                 </listitem>
+                <listitem id="DBDictionary.SupportsGetGeneratedKeys">
+                    <para>
+                    <indexterm>
+                        <primary>
+                            persistent fields
+                        </primary>
+                        <secondary>
+                            automatic field values
+                        </secondary>
+                        <tertiary>
+                            SupportsGetGeneratedKeys
+                        </tertiary>
+                    </indexterm>
+<literal>SupportsGetGeneratedKeys</literal>: When true, OpenJPA will use
+<methodname>java.sql.Statement.getGeneratedKeys</methodname> method to obtain
+values of auto-increment columns. When false, a query specified by
+<literal>LastGeneratedKeyQuery</literal> will be used for that purpose.
+If not set, the value will be auto-detected by querying the JDBC driver.
+Setting the value to true requires that the JDBC
+driver supports version 3.0 or higher of the JDBC specification
+and supports the <methodname>java.sql.Statement.getGeneratedKeys</methodname>
+method.
+                    </para>
+                </listitem>
                 <listitem id="DBDictionary.SupportsHaving">
                     <para>
                     <indexterm>