我有一个实体,比如说员工
@Entity
public class Employee {
@Id
private Long id;
private String name;
....
}
字段 name 被标记为唯一。
为了保存员工的列表,我使用了JpaRepository的saveAll()
函数。当数据库中存在具有 name
的 Employee 时,问题就出现了,这意味着违反了唯一约束 (name
)。
我想在这里处理的是我可以忽略或更新这个员工?有一个想法是,在保存任何 Employee 之前,我必须先检查它,例如 findByName()
,但这样做会使工作翻倍,并且当批量很大时非常不方便。< /p>
这是错误日志:
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.....
先谢谢你
答案 0 :(得分:0)
您可以使用 unique = true。
@Entity
public class Employee {
@Id
private Long id;
@Column(nullable = false, unique = true)
private String name;
....
}