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/31 11:54:02 UTC

svn commit: r780397 - in /openjpa/branches/1.3.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/ openjpa...

Author: mtylenda
Date: Sun May 31 09:54:02 2009
New Revision: 780397

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

Modified:
    openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java
    openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestGenerationType.java
    openjpa/branches/1.3.x/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java?rev=780397&r1=780396&r2=780397&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java (original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedStatementManagerImpl.java Sun May 31 09:54:02 2009
@@ -180,10 +180,10 @@
         String[] autoAssignColNames) 
         throws SQLException {
         ResultSet rs = stmnt.getGeneratedKeys();
-        List vals = new ArrayList();
+        List<Object> vals = new ArrayList<Object>();
         while (rs.next()) {
             for (int i = 0; i < autoAssignColNames.length; i++)
-                vals.add(rs.getObject(autoAssignColNames[i]));
+                vals.add(rs.getObject(i + 1));
         }
         rs.close();
         return vals;
@@ -202,7 +202,8 @@
             && row.getPrimaryKey() != null) {
             autoAssignColNames = new String[autoAssign.length];
             for (int i = 0; i < autoAssign.length; i++)
-                autoAssignColNames[i] = autoAssign[i].getName();
+                autoAssignColNames[i] =
+                    _dict.convertSchemaCase(autoAssign[i].getName());
         }
         return autoAssignColNames;
     }

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?rev=780397&r1=780396&r2=780397&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java (original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java Sun May 31 09:54:02 2009
@@ -193,7 +193,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;
@@ -412,7 +412,6 @@
                     // JDBC3-only method, so it might throw a 
                     // AbstractMethodError
                     isJDBC3 = metaData.getJDBCMajorVersion() >= 3;
-                    supportsGetGeneratedKeys = metaData.supportsGetGeneratedKeys();
                 } catch (Throwable t) {
                     // ignore if not JDBC3
                 }
@@ -429,6 +428,17 @@
                         conn.getAutoCommit(), conn.getHoldability(),
                         conn.getTransactionIsolation()}));
             }
+
+            // Auto-detect generated keys retrieval support
+            // unless user specified it.
+            if (supportsGetGeneratedKeys == null) {
+                if (isJDBC3) {
+                    supportsGetGeneratedKeys =
+                        metaData.supportsGetGeneratedKeys();
+                } else {
+                    supportsGetGeneratedKeys = false;
+                }
+            }
         }
         connected = true;
     }
@@ -4007,7 +4017,7 @@
      * Convert the specified schema name to a name that the database will
      * be able to understand.
      */
-    protected String convertSchemaCase(String objectName) {
+    public String convertSchemaCase(String objectName) {
         if (objectName == null)
             return null;
 

Modified: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestGenerationType.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestGenerationType.java?rev=780397&r1=780396&r2=780397&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestGenerationType.java (original)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestGenerationType.java Sun May 31 09:54:02 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,7 +43,13 @@
     extends SingleEMFTestCase {
 
     public void setUp() {
-        setUp(IdentityGenerationType.class);
+        if (getName().endsWith("WithoutGetGeneratedKeys")) {
+            setUp(IdentityGenerationType.class,
+                "openjpa.jdbc.DBDictionary",
+                "supportsGetGeneratedKeys=false");
+        } else {
+            setUp(IdentityGenerationType.class);
+        }
 
         /*
          * If the DBDictionary doesn't support AutoAssign(ment) of column
@@ -100,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/branches/1.3.x/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml?rev=780397&r1=780396&r2=780397&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml (original)
+++ openjpa/branches/1.3.x/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml Sun May 31 09:54:02 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>