简体   繁体   中英

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

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?

MapSqlParameterSource[] batchArgs ....

DAL.getNamedParameterTemplate().batchUpdate("INSERT INTO...", batchArgs);

Thanks

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.

使用db sequence获取下一个主键值并在insert语句中使用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM