我尝试保留实体列表:
for (Charge charge : charges) {
try {
Charge savedCharge = saveCharge(charge);
}catch (DataAccessException e){
log.error(e.getMessage());
}
}
public Charge saveCharge(Charge charge){
return chargesRepository.save(charge);
}
有时我会遇到异常(exception):
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: the limitation of uniqueness has been violated
我需要捕获这个异常。我该怎么做?
最佳答案
您将其捕获为任何异常。不过我建议你还是捕获 DataIntegrityViolationException
来自 Spring 框架,因为 Spring 已经将 SQL 异常转换为更具体的异常:
try {
Charge savedCharge = saveCharge(charge);
} catch (DataIntegrityViolationException e) {
log.error(e.getMessage());
// more error handling actions
} // more catch clauses
关于java - 捕获Spring数据异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51839776/