Skip to content
Advertisement

BatchSqlUpdate – how to get auto generated keys

I am using spring BatchSqlUpdate to insert a set of rows. How do I get the auto generated keys for all of the rows inserted?

When doing a single insert I get the keys like this –

SqlUpdate sqlUpdate = new SqlUpdate(dataSource, sqlTemplate.toString());
sqlUpdate.setReturnGeneratedKeys(true);
KeyHolder keyHolder = new GeneratedKeyHolder();
sqlUpdate.update(new Object[] {}, keyHolder);
return keyHolder.getKey().longValue();

Thanks!

Advertisement

Answer

There is no provided solution for this using BatchSqlUpdate as far as I know, but you can always

  • query the last key before the insert
  • using this information, query all new keys after the insert
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement