开发者

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 o开发者_JAVA技巧f 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜