I have a table in postgresql with unique constraint and after insert i can get the error duplicate key value violates unique constraint. How best way to skip this error (for example, ON CONFLICT DO NOTHING) using spring data?
If you want to forget the row in case of conflict, just use INSERT ... ON CONFLICT (field) DO NOTHING.
You can catch the DataIntegrityViolationException exception and handle it the way you want e.g.
try {
repository.save(myEntity);
} catch(DataIntegrityViolationException e) {
System.out.println("Entity already exists. Skipping ...");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With