Я использую Spring JDBC. Является простым способом получить последний вставленный идентификатор с использованием Spring Framework или мне нужно использовать некоторые трюки JDBC?Spring JDBC - Последнее вставленное id
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();
Спасибо. Я сделал это следующим образом: 'jdbcTemplate.queryForLong (" select currval ('main_seq') ");' – marioosh