Skip to content

Add batchUpdate method to JDBCTemplate with support for returning generated keys [SPR-1836] #6530

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Issue Links:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: dataIssues in data modules (jdbc, orm, oxm, tx)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions