}
public Object executeStatementAfterInsert(final Database database,
final Connection conn, final Identity identity, final ProposedEntity entity)
throws PersistenceException {
Identity internalIdentity = identity;
SQLEngine extended = _engine.getExtends();
PreparedStatement stmt = null;
try {
// must create record in the parent table first. all other dependents
// are created afterwards. quick and very dirty hack to try to make
// multiple class on the same table work.
if (extended != null) {
ClassDescriptor extDesc = extended.getDescriptor();
if (!new ClassDescriptorJDONature(extDesc).getTableName().equals(_mapTo)) {
internalIdentity = extended.create(database, conn, entity, internalIdentity);
}
}
if ((internalIdentity == null) && _useJDBC30) {
Field field = Statement.class.getField("RETURN_GENERATED_KEYS");
Integer rgk = (Integer) field.get(_statement);
Class[] types = new Class[] {String.class, int.class};
Object[] args = new Object[] {_statement, rgk};
Method method = Connection.class.getMethod("prepareStatement", types);
stmt = (PreparedStatement) method.invoke(conn, args);
// stmt = conn.prepareStatement(_statement, Statement.RETURN_GENERATED_KEYS);
} else {
stmt = conn.prepareStatement(_statement);
}
if (LOG.isTraceEnabled()) {
LOG.trace(Messages.format("jdo.creating", _type, stmt.toString()));
}
// must remember that SQL column index is base one.
int count = 1;
count = bindFields(entity, stmt, count);
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.format("jdo.creating", _type, stmt.toString()));
}
stmt.executeUpdate();
SQLColumnInfo[] ids = _engine.getColumnInfoForIdentities();
if (internalIdentity == null) {
if (_useJDBC30) {
// use key returned by INSERT statement.
Class cls = PreparedStatement.class;
Method method = cls.getMethod("getGeneratedKeys", (Class[]) null);
ResultSet keySet = (ResultSet) method.invoke(stmt, (Object[]) null);
// ResultSet keySet = stmt.getGeneratedKeys();
int i = 1;
int sqlType;
List keys = new ArrayList();
while (keySet.next()) {
sqlType = ids[i - 1].getSqlType();
Object temp;
if (sqlType == java.sql.Types.INTEGER) {
temp = new Integer(keySet.getInt(i));
} else if (sqlType == java.sql.Types.NUMERIC) {
temp = keySet.getBigDecimal(i);
} else {
temp = keySet.getObject(i);
}
keys.add(ids[i - 1].toJava(temp));
i++;
}
internalIdentity = new Identity(keys.toArray());
stmt.close();
} else {
stmt.close();