Spring JDBC を使用しています。Spring Framework を使用して最後に挿入された ID を取得する簡単な方法はありますか、それとも JDBC トリックを使用する必要がありますか?
jdbcTemplate.update("insert into test (name) values(?)", params, types);
// last inserted id ...
以下のようなものを見つけましたが、次のようになります。org.postgresql.util.PSQLException: Returning autogenerated keys is not supported.
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(
Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement("insert into test (name) values(?)", new String[] {"id"});
ps.setString(1, "test");
return ps;
}
}, keyHolder);
lastId = (Long) keyHolder.getKey();