Index: main/org/exolab/castor/castor.properties =================================================================== RCS file: /home/projects/castor/scm/castor/src/main/org/exolab/castor/castor.properties,v retrieving revision 1.22 diff -u -r1.22 castor.properties --- main/org/exolab/castor/castor.properties 1 Feb 2006 13:42:46 -0000 1.22 +++ main/org/exolab/castor/castor.properties 6 Feb 2006 08:58:24 -0000 @@ -207,3 +207,9 @@ # requested for the first time? Defaults to false. # org.castor.transactionmanager.InitializeAtRegistration=false + +# +# Instructs Castor JDO to use the JDBC 3.0-specific features to obtain +# the generated value of an identity column; default to false +# +org.castor.jdo.use.jdbc30=false Index: main/org/exolab/castor/jdo/engine/SQLEngine.java =================================================================== RCS file: /home/projects/castor/scm/castor/src/main/org/exolab/castor/jdo/engine/SQLEngine.java,v retrieving revision 1.44 diff -u -r1.44 SQLEngine.java --- main/org/exolab/castor/jdo/engine/SQLEngine.java 21 Jan 2006 11:05:16 -0000 1.44 +++ main/org/exolab/castor/jdo/engine/SQLEngine.java 6 Feb 2006 08:58:25 -0000 @@ -24,6 +24,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -59,7 +60,10 @@ import org.exolab.castor.persist.spi.PersistenceFactory; import org.exolab.castor.persist.spi.PersistenceQuery; import org.exolab.castor.persist.spi.QueryExpression; +import org.exolab.castor.util.Configuration; +import org.exolab.castor.util.LocalConfiguration; import org.exolab.castor.util.Messages; +import org.exolab.castor.util.Configuration.Property; /** * The SQL engine performs persistence of one object type against one @@ -77,6 +81,9 @@ * @version $Revision: 1.44 $ $Date: 2006/01/21 11:05:16 $ */ public final class SQLEngine implements Persistence { + + private static final String PROPERTY_USE_JDBC30 = "org.castor.jdo.use.jdbc30"; + /** The Jakarta * Commons Logging instance used for all logging. */ private static final Log LOG = LogFactory.getLog( SQLEngine.class ); @@ -431,7 +438,7 @@ } /** - * Use the specified keygenerator to gengerate a key for this + * Use the specified keygenerator to generate a key for this * row of object. * * Result key will be in java type. @@ -495,6 +502,9 @@ throw new PersistenceException(Messages.format("persist.noIdentity", _clsDesc.getJavaClass().getName())); } + boolean useJDBC30 = + Boolean.valueOf(LocalConfiguration.getInstance().getProperty(Property.PROPERTY_USE_JDBC30, "false")).booleanValue(); + try { // Must create record in the parent table first. // All other dependents are created afterwards. @@ -512,7 +522,14 @@ if ((_keyGen != null) && (_keyGen.getStyle() == KeyGenerator.DURING_INSERT)) { stmt = ((Connection) conn).prepareCall(_sqlCreate); } else { - stmt = ((Connection) conn).prepareStatement(_sqlCreate); + + if (useJDBC30) { + // amend code to use reflection; otherwise we introduce a compile + // time dependency on the getGeneratedKeys() method + stmt = ((Connection) conn).prepareStatement(_sqlCreate, PreparedStatement.RETURN_GENERATED_KEYS); + } else { + stmt = ((Connection) conn).prepareStatement(_sqlCreate); + } } if (LOG.isDebugEnabled()) { @@ -587,14 +604,34 @@ if (LOG.isDebugEnabled()) { LOG.debug(Messages.format("jdo.creating", _clsDesc.getJavaClass().getName(), stmt.toString())); } - stmt.executeUpdate(); + + stmt.executeUpdate(); + + if (useJDBC30 && identity == null) { + // amend code to use reflection; otherwise we introduce a compile + // time dependency on the getGeneratedKeys() method + ResultSet keySet = stmt.getGeneratedKeys(); + int i = 1; + List keys = new LinkedList(); + while (keySet.next()) { + keys.add(keySet.getObject(i)); + i++; + } + if (keys.size() > 1) { + identity = keys.toArray(); + } else { + identity = keys.iterator().next(); + } + } } stmt.close(); // Generate key after INSERT if ((_keyGen != null) && (_keyGen.getStyle() == KeyGenerator.AFTER_INSERT)) { - identity = generateKey(database, conn, stmt); + if (!useJDBC30) { + identity = generateKey(database, conn, stmt); + } } return identity; Index: main/org/exolab/castor/util/Configuration.java =================================================================== RCS file: /home/projects/castor/scm/castor/src/main/org/exolab/castor/util/Configuration.java,v retrieving revision 1.10 diff -u -r1.10 Configuration.java --- main/org/exolab/castor/util/Configuration.java 16 Jan 2006 20:22:54 -0000 1.10 +++ main/org/exolab/castor/util/Configuration.java 6 Feb 2006 08:58:27 -0000 @@ -250,7 +250,16 @@ static final String DEFAULT_SERIALIZER_FACTORY = "org.exolab.castor.xml.XercesXMLSerializerFactory"; - + /** + * Property specifying whether JDBC 3.0-specific features should be used, + * such ase.g. the use of Statement.getGeneratedKeys() + *
+         * org.castor.jdo.use.jdbc30
+         * 
+ * @since 1.0M3 + */ + public static final String PROPERTY_USE_JDBC30 = "org.castor.jdo.use.jdbc30"; + } //-- class: Property Index: tests/tests.xml =================================================================== RCS file: /home/projects/castor/scm/castor/src/tests/tests.xml,v retrieving revision 1.34 diff -u -r1.34 tests.xml --- tests/tests.xml 1 Feb 2006 13:42:46 -0000 1.34 +++ tests/tests.xml 6 Feb 2006 08:58:28 -0000 @@ -800,7 +800,7 @@ - + Index: tests/jdo/pointbase.sql =================================================================== RCS file: /home/projects/castor/scm/castor/src/tests/jdo/pointbase.sql,v retrieving revision 1.1 diff -u -r1.1 pointbase.sql --- tests/jdo/pointbase.sql 1 Feb 2006 13:42:45 -0000 1.1 +++ tests/jdo/pointbase.sql 6 Feb 2006 08:58:29 -0000 @@ -1,5 +1,3 @@ -connect 'jdbc:derby://127.0.0.1/tests;create=true;user=test;password=test'; - -- tc0x TESTS drop table tc0x_sample; @@ -337,20 +335,19 @@ create unique index tc2x_uuid_ext_pk on tc2x_uuid_ext ( id ); -drop table tc2x_identity; - +drop table tc2x_identity; create table tc2x_identity ( - id integer not null primary key generated always as identity, + id integer identity not null primary key , attr varchar(200) not null ); drop table tc2x_identity_ext; - create table tc2x_identity_ext ( - id integer not null primary key generated always as identity, + id integer identity not null primary key, ext varchar(200) not null ); +drop index tc2x_identity_ext.tc2x_ident_ext_pk; create unique index tc2x_ident_ext_pk on tc2x_identity_ext ( id ); drop table tc2x_seqtable;