我正在使用带有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