Description
Arthur Branham opened SPR-1836 and commented
I realize that generated keys are not returned in a batchUpdate even if multiple insert statements are executed for a table with an autoincrement column. Currently in JDBCTemplate the only way to get a generated key is to execute statements one at a time using the following method signature:
public in update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder)
I have implemented a batchUpdate method with the following method signature:
public int[] batchUpdate(final String sql,final BatchPreparedStatementSetter pss,final KeyHolder generatedKeyHolder) throws DataAccessException;
This method is a little more performant and convenient because all statements are executed by one PreparedStatement. Parameter values for each statement are set via the BatchPreparedStatementSetter. After each batch is executed, any generated keys are collected and aggregated in the KeyHolder. Updated row counts are returned for each batch statement. The returned keys can then be used to fetch in bulk, the rows that were created via a batchQuery as described in http://opensource.atlassian.com/projects/spring/browse/SPR-1835
I will attach an implementation and sample usage.
Affects: 2.0 M3
Attachments:
- JDBCTemplateBatchUpdateMethodImpl.txt (1.82 kB)
- JDCBTemplateBatchUpdateWithGeneratedKeys.txt (2.11 kB)
Issue Links:
- Add batchQuery methods to JDBCTemplate [SPR-1835] #6529 Add batchQuery methods to JDBCTemplate
Referenced from: commits spring-projects/spring-data-relational@21490b8, spring-projects/spring-data-relational@a390cf7, spring-projects/spring-data-relational@3efb2a4
3 votes, 5 watchers