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();
Observe que nas versões mais recentes do Postgres você precisa usar
connection.prepareStatement(sql.toString(),
new String[] { "idcompte" /* name of your id column */ })
ao invés de
connection.prepareStatement(sql.toString(),
Statement.RETURN_GENERATED_KEYS);