我试图使用QueryDSL提供的batch-Insert功能批量插入一些记录。当我只使用execute()时,同样的代码可以工作,但必须单独编写一个select查询来获取最后插入的记录。
databaseAccessor.runInNewTransaction(databaseConnection -> {
SQLInsertClause batchInsert = databaseConnection.insert(TEST);
for (int i = 0; i < 5; i++) {
batchInsert.set(TEST.NAME, "label" + i).set(TEST.PROJECT_ID, 1).addBatch();
}
return batchInsert.executeWithKeys(TEST.ID);
}, OnRollback.NOOP);
这段代码在MySQL和PostgreSQL中可以使用,但在ORACLE和SQL Server中却失败了,有什么办法可以做到这一点,并且还能得到插入记录的id。
我得到的异常如下。
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: ****该语句必须在获得任何结果之前执行****。