Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-1836

Add batchUpdate method to JDBCTemplate with support for returning generated keys


    • Type: New Feature
    • Status: Resolved
    • Priority: Minor
    • Resolution: Won't Fix
    • Affects Version/s: 2.0 M3
    • Fix Version/s: None
    • Component/s: Data
    • Labels:
    • Last commented by a User:


      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.


          Issue Links



              • Assignee:
                thomas.risberg Thomas Risberg
                arthur.branham Arthur Branham
                Last updater:
                Trevor Marshall
              • Votes:
                3 Vote for this issue
                5 Start watching this issue


                • Created:
                  Days since last comment:
                  5 years, 47 weeks, 3 days ago