我在postgresql中有一个带有唯一约束的表,在插入之后,我可以得到错误duplicate key value violates unique constraint
。如何使用spring数据跳过此错误的最好方法(例如,冲突时什么都不做)?
发布于 2020-03-03 20:14:25
如果您想在发生冲突时忘记行,只需使用INSERT ... ON CONFLICT (field) DO NOTHING
即可。
发布于 2020-03-04 01:07:12
您可以捕获DataIntegrityViolationException
异常并以您想要的方式处理它,例如
try {
repository.save(myEntity);
} catch(DataIntegrityViolationException e) {
System.out.println("Entity already exists. Skipping ...");
}
https://stackoverflow.com/questions/60513810
复制相似问题