I have an entity, say Employee
@Entity
public class Employee {
@Id
private Long id;
private String name;
....
}
with field name is marked as unique.
To save a list of Employee, I use saveAll()
function of JpaRepository. The problem comes when there is an Employee with existed name
in database which means unique constraint (name
) has been violated.
What I want to handle here is somehow I can ignore or update this Employee ? There is an idea that is before saving any Employee, I have to check it first, like findByName()
but by doing that, it makes work doubled and really inconvenient when batch size is huge.
This is error log:
org.springframework.dao.DataIntegrityViolationException: could not execute batch;
SQL......at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccess
Exception(HibernateJpaDialect.java:298)......
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute batch......
Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint (SILKGATE_TEST.SYS_C00100705) violated.....
Thank you in advanced