Is there anyway to get the generated keys when using Spring JDBC batchUpdate?

后端 未结 2 1872
借酒劲吻你
借酒劲吻你 2021-01-17 10:02

I am using JDBC and want to batch insert, but I need the generated keys for the next round of inserts - is there anyway to accomplish this?

MapSqlParameterSour

相关标签:
2条回答
  • 2021-01-17 10:31

    Use db sequences to grab the next primary key value and use it in the insert statement.

    0 讨论(0)
  • 2021-01-17 10:34

    Spring framework folks attempted a solution to this problem. But they abandoned the attempt when it became apparent that there is no way to guarantee that the solution will work with all JDBC Drivers. This is because the JDBC spec doesn't guarantee that the generated keys will be made available after a batch update. JDBC drivers are free to implement this feature as they see fit. In some cases the underlying database might not return the generated keys making it impossible for the driver to support this feature.

    So even if you are directly working with JDBC, you will need to check whether your database and JDBC driver makes the generated keys available.

    I remember I was able to achieve this with MySQL 5.0 JDBC driver with some effort but never integrated the solution in our production application as we had to support older versions of MySQL as well.

    0 讨论(0)
提交回复
热议问题