0

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

ktcl
  • 365
  • 6
  • 23
  • Does this answer your question? [How to ignore unique violation during insert list of objects which contain set of object](https://stackoverflow.com/questions/12468698/how-to-ignore-unique-violation-during-insert-list-of-objects-which-contain-set-o) – VICTOR Feb 23 '21 at 08:17
  • I see there is a duplicate as mentioned by @VICTOR. But why the `name` is part of your primary key and not only the `id`, is there a specific reason? Actually, the way you have it defined, `name` isn't part of any key on your Entity, then why it violates some constraints? Can you share a stack trace? – Aris Feb 23 '21 at 08:22
  • @Aristotle because of the business, `name` must be unique, and this is error log: org.springframework.dao.DataIntegrityViolationException: could not execute batch; SQL......at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(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..... – ktcl Feb 23 '21 at 08:28
  • Ok so now makes sense what exactly you want. But again I don't think that the violation is from the `name` field. Maybe you can provide the whole trace so we can analyze it. – Aris Feb 23 '21 at 08:34
  • I use below query to create the table: CREATE TABLE EMPLOYEE ( "ID" NUMBER(10) NOT NULL ENABLE, "NAME" VARCHAR2(200 BYTE) NOT NULL ENABLE UNIQUE, "FULL_NAME" VARCHAR2(200 BYTE) NOT NULL ENABLE UNIQUE, CONSTRAINT "EMPLOYEE_PK" PRIMARY KEY ("ID")); so as you can see, `name` should be unique – ktcl Feb 23 '21 at 08:39

1 Answers1

0

you can use unique = true.

@Entity
public class Employee {
    @Id
    private Long id;

    @Column(nullable = false, unique = true)
    private String name;
    ....
}