LouisS
LouisS

Reputation: 23

How to obtain the generated keys when using the NamedParameterJDBCTemplate in a batchUpdate

I'm trying to understand how to perform a batch insert statement (for speed improvement) and obtain the created primary key values for the newly created records.

I have seen the KeyHolder interface, but there seems to be no function that combines it with a batch update only a single update (which I want to avoid for performance reasons).

Upvotes: 2

Views: 2646

Answers (1)

biiyamn
biiyamn

Reputation: 506

It's possible by extending JdbcTemplate and adding a method which is an exact copy of batchUpdate method and take an extra param of Type KeyHolder, there in PreparedStatementCallback after ps.executeBatch() you should call ResultSet keys = ps.getGeneratedKeys() and extract the generated keys and store theme in KeyHolder but there is no guarantee that ps.getGeneratedKeys() will return value it depends on database

Upvotes: 2

Related Questions