【问题标题】:Spring data jpa does not update existing row (Oracle) when using @sequencegenerator使用@sequencegenerator 时,Spring 数据 jpa 不会更新现有行(Oracle)
【发布时间】: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 方法

标签: oracle spring-data-jpa


【解决方案1】:

我的拙见;

  • 在 Update 解决此问题之前,将“存在”条件检查用作所有受约束列的“子查询”。

【讨论】:

  • 感谢您的回答。我没有写任何查询,我正在使用 JpaRepository 的 saveall() 方法
猜你喜欢
  • 2017-02-05
  • 2019-05-02
  • 2020-03-23
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多