Resolving Postgres "returning auto generated keys no supported"

I discovered Jean-Pol Landrain’s, “TriggerAssignedIdentityGenerator” to address the issue of the Hibernate ID Generator not supporting an Id created by the database via a BEFORE INSERT trigger.

However the TriggerAssignedIdentityGenerator design for Oracle did not work for the older JDBC PostgreSQL driver which doesn’t support the return of auto-generated keys.

The probem was resolved by changes to the ‘prepare’ and ‘executeAndExtract’ methods:

protected PreparedStatement prepare(String insertSQL, SessionImplementor session) throws SQLException {

 return session.getBatcher().prepareStatement(insertSQL + " RETURNING *");}

protected Serializable executeAndExtract(PreparedStatement insert) throws SQLException {

 insert.execute(); // get the new id back ResultSet rs = insert.getResultSet(); if (rs.next()) {  return new String(rs.getString(1)); // assume that 1 is id }

 throw new SQLException("Returned no id in resultset");

}
Advertisements

One thought on “Resolving Postgres "returning auto generated keys no supported"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s