我正在使用帶有hibernate的jpa我想在數據庫中插入100條記錄,假設我在第50條記錄插入中得到異常JDBC批量更新,我需要處理異常,並且需要保留其餘記錄到DB。如何在發生異常時繼續批量插入
代碼:
private List<TempCustomers> tempCustomer =new ArrayList<TempCustomers>();
public String migrateCustomers() {
TempCustomers temp = null;
for(DoTempCustomers tempCustomers:doTempCustomers){
try {
temp=new TempCustomers();
BeanUtils.copyProperties(temp, tempCustomers);
tempCustomer.add(temp);
entityManager.persist(temp);
}catch (Exception e) {
tempCustomer.add(temp);
entityManager.persist(temp);
log.info("Exception ..."+e);
return "null";
}
}
return "null";
}
謝謝你的迴應,假設從列表中讀取100條記錄,然後我將堅持使用entityManager.persist(temp)..在該列表中一條記錄不正確,因此通過JDBC批處理異常休眠。時間擴展99條記錄我想存儲數據庫,我該怎麼辦呢? – nag
是的..拉斯,我需要捕捉異常和擴展持久。如何做到這一點? – nag
千萬不要這樣做'log.info(「Exception ...」+ e);'...它吞下堆棧跟蹤。相反,使用'log.info(「Exception ...」,e);'作爲參數的例外 – artbristol