【问题标题】:Constraint Violation Exception using sequence in JPA在 JPA 中使用序列的约束违反异常
【发布时间】:2018-11-21 11:16:41
【问题描述】:

我在我的项目中使用 Spring Data 并使用 JPA 在实体和我的表之间进行映射, 这是我的实体

public class daoTable {

@Id
@SequenceGenerator(name = "seq_table_nc", sequenceName = "SEQ_TABLE_NC")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_table_nc")
private Long id;
// setter && getter && outher params
}

这是我的回购

public interface daoTable Repository extends JpaRepository<daoTable , Long>, JpaSpecificationExecutor<daoTable >{}

当我尝试保存时,我遇到了这个问题:

  1. org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [GQAO.PK_RT]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
  2. java.sql.SQLIntegrityConstraintViolationException: ORA-00001: violation de contrainte unique

【问题讨论】:

    标签: java oracle hibernate jpa spring-data-jpa


    【解决方案1】:

    如果您的数据库中已有记录与您的SequenceGenerator 将生成的值相同,就会发生这种情况。

    例如,如果我保存 1 条记录而不使用此 SequenceGenerator (seq_table_nc) 之后将其添加到代码中此 SequenceGenerator (seq_table_nc) 并尝试保存我会得到你的异常,因为生成器可以给我的值等于我在数据库中已有的值。

    通常您应该检查哪个值尝试在 DB 中为id 保存。之后您必须了解如何更新您的数据库SequenceGenerator,以便它开始生成唯一值。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 2019-04-29
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 2014-10-08
      • 2013-03-31
      相关资源
      最近更新 更多