此方法用于将数据保存在数据库中
public Response saveAll(PurchaseOrderHeaderDto purchaseOrderHeaderDto){
try{
s=sf.openSession();
tx=s.beginTransaction();
PurchaseOrderHeader orderDo=importDao(purchaseOrderHeaderDto);
for(int i=0;i<orderDo.getPurchaseItemList().size(); i++){
orderDo.getPurchaseItemList().get(i).setHeaderDetails(orderDo);
s.save(orderDo.getPurchaseItemList().get(i));
}
response.setStatus(200);
response.setMessage("succesfull");
tx.commit();
}
catch(ConstraintViolationException e){
System.out.println(e.getMessage());
response.setStatus(500);
response.setMessage("unsuccesfull");
tx.rollback();
}
finally{
s.close();
}
return response;
}
I am getting this error
Error during managed flush [org.hibernate.exception.DataException: could not execute statement]
我不知道如何处理此异常(我知道这是由于篇幅所致,但我想处理此异常。欢迎提出任何建议)
答案 0 :(得分:0)
因为您在获取ConstraintViolationException
时试图抓住DataException
。
尝试使用以下代码:
public Response saveAll(PurchaseOrderHeaderDto purchaseOrderHeaderDto) {
try {
s = sf.openSession();
tx = s.beginTransaction();
PurchaseOrderHeader orderDo = importDao(purchaseOrderHeaderDto);
for (int i = 0; i < orderDo.getPurchaseItemList().size(); i++) {
orderDo.getPurchaseItemList().get(i).setHeaderDetails(orderDo);
s.save(orderDo.getPurchaseItemList().get(i));
}
response.setStatus(200);
response.setMessage("succesfull");
tx.commit();
} catch (Exception e) {
System.out.println(e.getMessage());
response.setStatus(500);
response.setMessage("unsuccesfull");
tx.rollback();
} finally {
s.close();
}
return response;
}
这会捕获所有异常,您可能以后要处理,但是应该可以