In case anyone else runs into this, I got it to work by writing my own IdentityGenerator that forces the use of the InsertSelectDelegate that is present in the IdentityGenerator class since I didn't want to turn off GetGeneratedKeys for the entire Factory, just this one class that needed to use the sql-insert with a stored procedure:
public class StoredProcedureIdentityGenerator extends AbstractPostInsertGenerator { public InsertGeneratedIdentifierDelegate getInsertGeneratedIdentifierDelegate( final PostInsertIdentityPersister persister, final Dialect dialect, final boolean isGetGeneratedKeysEnabled) throws HibernateException { return new InsertSelectDelegate(persister, dialect); }
/** * Delegate for dealing with IDENTITY columns where the dialect supports returning * the generated IDENTITY value directly from the insert statement. * * Written by Christoph Sturm * $Id: IdentityGenerator.java 14098 2007-10-18 06:17:56Z gbadner $ */ public static class InsertSelectDelegate extends AbstractReturningDelegate implements InsertGeneratedIdentifierDelegate { private final PostInsertIdentityPersister persister; private final Dialect dialect;
public InsertSelectDelegate( final PostInsertIdentityPersister persister, final Dialect dialect) { super(persister); this.persister = persister; this.dialect = dialect; }
public IdentifierGeneratingInsert prepareIdentifierGeneratingInsert() { InsertSelectIdentityInsert insert = new InsertSelectIdentityInsert( dialect ); insert.addIdentityColumn( persister.getRootTableKeyColumnNames()[0] ); return insert; }
protected PreparedStatement prepare(String insertSQL, SessionImplementor session) throws SQLException { return session.getBatcher().prepareStatement( insertSQL, false ); }
public Serializable executeAndExtract(PreparedStatement insert) throws SQLException { if ( !insert.execute() ) { while ( !insert.getMoreResults() && insert.getUpdateCount() != -1 ) { // do nothing until we hit the rsult set containing the generated id } } ResultSet rs = insert.getResultSet(); try { return IdentifierGeneratorFactory.getGeneratedIdentity(rs, persister.getIdentifierType()); } finally { rs.close(); } }
public Serializable determineGeneratedIdentifier( final SessionImplementor session, final Object entity) { throw new AssertionFailure( "insert statement returns generated value" ); } } }
|