Some serious threadomancy here, but I found this thread, maybe you will too..
I've created JIRA issue HHH-3735 about this, but for the moment, I have a workaround. I did pretty much what beppegg tried in the first post, but insted of using plain-old IdentifierGeneratingInsert inside the prepareIdentifierGeneratingInsert method of my AbstractReturningDelegate subclass, I used my own subclass. It ignores all the columns etc., and instead returns the sql-insert string, unmodified. And because I can't get at the sql-insert element in the class mapping, I simply duplicated it in the generator element and made my custom StoredProcedureGenerator implement Configurable.
The rest of the code in my AbstractReturningDelegate subclass is fairly straightforward JDBC:
Code:
@Override
protected PreparedStatement prepare(String sql, SessionImplementor session) throws SQLException
{
CallableStatement stmt = session.getBatcher().prepareCallableStatement(sql);
stmt.registerOutParameter(outputParamIdx,
idType.sqlTypes(session.getFactory())[0]);
return stmt;
}
@Override
protected Serializable executeAndExtract(PreparedStatement stmt) throws SQLException
{
stmt.execute();
CallableStatement callable = (CallableStatement) stmt;
Object id = callable.getObject(outputParamIdx);
return (Serializable) id;
}
I suppose that I could have tried a bit harder to figure out the type of the id in executeAndExtract, but that code is simple, and higher layers of hibernate code do it already, so I'm happy with this.