Java + Spring + JPA + Hibernate + oracle
我正在使用以下代码批量插入
当我执行以下代码时,我得到响应-2并且没有记录插入表中。有什么帮助吗?
Example code.java
List<ReportItem> reportItems = new ArrayList<ReportItem>();
ReportItem reportItem = new ReportItem();
reportItems.add(reportItem);
reportItem.setReportId(new Long(61));
reportItem.setLicenseWindowEnd("LICENSE_WINDOW_END");
reportItem.setLicenseWindowStart("LICENSE_WINDOW_START");
reportItem.setPackageAssetID("PACKAGE_ASSET_ID");
reportItem.setPackageAssetName("PACKAGE_ASSET_NAME");
reportItem.setPackageProvider("PACKAGE_PROVIDER");
reportItem.setPackageProviderID("PACKAGE_PROVIDER_ID");
reportItem.setPackageUpdateNum("PACKAGE_UPDATE_NUM");
reportItem.setTitle("TITLE");
reportItem.setTitleAssetID("TITLE_ASSET_ID");
reportItem.setTitleBrief("TITLE_BRIEF");
reportItem.setTitleProviderID("TITLE_PROVIDER_ID");
reportItem.setTitleUpdateNum("TITLE_UPDATE_NUM");
reportItem.setTargetPitchDate("TARGET_PITCH_DATE");
reportItem.setAssetCdnStatus("ASSET_CDN_STATUS");
reportItem.setAssetReceived("ASSET_RECEIVED");
reportItem.setAssetReceived("ASSET_AVAILABLE");
int[] counts = reportDAO.insertBatchReportItems(reportItems);
// response return counts[0] is -2
Dao.java
@Override
public int[] insertBatchReportItems(final List<ReportItem> reportItems) {
int[] counts = getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ReportItem reportItem = reportItems.get(i);
ps.setLong(1, reportItem.getReportId());
ps.setString(2, reportItem.getLicenseWindowEnd());
ps.setString(3, reportItem.getLicenseWindowStart());
ps.setString(4, reportItem.getPackageAssetID());
ps.setString(5, reportItem.getPackageAssetName());
ps.setString(6, reportItem.getPackageProvider());
ps.setString(7, reportItem.getPackageProviderID());
ps.setString(8, reportItem.getPackageUpdateNum());
ps.setString(9, reportItem.getTitle());
ps.setString(10, reportItem.getTitleAssetID());
ps.setString(11, reportItem.getTitleBrief());
ps.setString(12, reportItem.getTitleProviderID());
ps.setString(13, reportItem.getTitleUpdateNum());
ps.setString(14, reportItem.getTargetPitchDate());
ps.setString(15, reportItem.getAssetCdnStatus());
ps.setString(16, reportItem.getAssetReceived());
ps.setString(17, reportItem.getAssetAvailable());
}
@Override
public int getBatchSize() {
return reportItems.size();
}
});
return counts;
}
答案 0 :(得分:1)
根据jdbc
Statement.SUCCESS_NO_INFO - 命令已成功处理, 但受影响的行数未知
Statement.SUCCESS_NO_INFO定义为-2,因此您的结果表明一切正常,但您无法获得有关更新列数的信息。
根据oracle docs
对于准备好的声明批处理,无法知道该数字 数据库中受每个单独语句影响的行数 批量。因此,所有数组元素的值都为-2。根据 在JDBC 2.0规范中,值为-2表示该操作 成功但受影响的行数未知。