KeyHolder holder = new GeneratedKeyHolder();
getJdbcTemplate().update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection)
throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql.toString(),
Statement.RETURN_GENERATED_KEYS);
ps.setString(1, person.getUsername());
ps.setString(2, person.getPassword());
ps.setString(3, person.getEmail());
ps.setLong(4, person.getRole().getId());
return ps;
}
}, holder);
Long newPersonId = holder.getKey().longValue();
Notez que dans les nouvelles versions de Postgres, vous devez utiliser
connection.prepareStatement(sql.toString(),
new String[] { "idcompte" /* name of your id column */ })
au lieu de
connection.prepareStatement(sql.toString(),
Statement.RETURN_GENERATED_KEYS);