简体   繁体   English

JPA中使用序列的约束违例异常

[英]Constraint Violation Exception using sequence in JPA

I am using Spring Data in my project and using JPA for mapping between the entity and my table, this is my entity 我在项目中使用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
}

and this is my Repo 这是我的回购

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

when I try to save I get this problem : 当我尝试保存时,出现此问题:

  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

This can happen if you already have records in database with the same values as your SequenceGenerator will generate. 如果数据库中已有与SequenceGenerator生成的值相同的记录,则可能会发生这种情况。

For example if I save 1 record without using this SequenceGenerator ( seq_table_nc ) after that add it in the code this SequenceGenerator ( seq_table_nc ) and try to save I will get the exception that you have because the generator can give me value that is equal to value that I already have in database. 例如,如果我保存1条记录而不使用此SequenceGeneratorseq_table_nc ),然后将其添加到此SequenceGeneratorseq_table_nc )的代码中,然后尝试保存,我将得到一个例外,因为生成器可以给我等于value的值我已经在数据库中了

Generally you should check which value try to save in DB for id . 通常,您应该检查尝试将哪个值保存在DB中以获取id After that you have to see how to update your database SequenceGenerator so it start generate unique values. 之后,您必须查看如何更新数据库SequenceGenerator以便它开始生成唯一值。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM