【发布时间】:2021-08-08 10:16:08
【问题描述】:
Spring 数据 JPA 的新手。我在 oracle 表中创建了序列。我正在使用JPARepository saveall() 方法来保存数据。插入工作没有任何问题,但是当我尝试更新表中的任何现有行时,它总是尝试插入抛出 唯一约束错误,因为我为该表创建了唯一索引。
实体类
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator1")
@SequenceGenerator(sequenceName = "emp_seq", name = "generator1",allocationSize = 1)
@Column(name = "USER_ID")
private Long id;
保存方法调用
public void persistEmployees(List<Employee> employees) {
employeeRepo.savaAll(employees);
}
存储库类
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> { }
如何告诉 JPA 在插入之前查找现有行?非常感谢任何帮助!!!
【问题讨论】:
-
请张贴代码你好吗尝试更新 - 这将是问题...
-
@MarmiteBomber 我已经添加了我调用保存的方法。这只是我正在使用的 saveAll JpaRepository 方法