Hibernate JPA Oracle12C-在Primary Key(Sequence)上的约束违反异常。

问题描述 投票:0回答:1

我正在使用SpringBoot应用程序与Hibernate JPA.Oracle版本 - 12c ,RAC设置与3个节点。我有四个应用服务器。对于下面的表格:

@EntityListeners(AuditingEntityListener.class)
@Data
@Entity
@Table(name="order_payment_collection")
public class OrderPaymentCollection {

@Column
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
Long id;
....
} 

其中我使用AUTO生成策略,因此hibernate已经在DB中创建了一个序列。

  CREATE SEQUENCE  "test"."HIBERNATE_SEQUENCE"  MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1798892 CACHE 20 NOORDER  NOCYCLE  NOPARTITION ;

当两个不同的进程(运行在不同的节点上)发生并发插入时,我得到了以下错误。

could not execute statement; SQL [n/a]; constraint [*.SYS_C005080]; nested exception is 
org.hibernate.exception.ConstraintViolationException: could not execute statement

Which is PrimaryKeyUnique Index - Column ID for this table.

在使用Hibernate时,我们是否需要为每个节点进程使用单独的序列?

我有一个计划来解决这个问题。

@GenericGenerator(name = "seq", strategy = "com.test.utils.KeyGenerator")
@GeneratedValue(generator = "seq")
@Id
Long id;

在KeyGenerator类中,我实现了IdentifierGenerator接口,从自定义DB序列中获取nextVal。

CREATE SEQUENCE  test."PE_TABLES_SEQ"  MINVALUE 10000 MAXVALUE 10000000000000 INCREMENT BY 
1 START WITH 1798893 CACHE 1000 NOORDER  NOCYCLE  NOPARTITION ; 

因此希望得到专家的意见。如果我遗漏了什么,或者有人遇到了这个问题。. 我的解决方案来解决这个问题将是执行或不?

java hibernate jdbc spring-data-jpa ojdbc
1个回答
0
投票

在你的实体中,尝试改变生成策略为身份序列。请看下面: @GeneratedValue(strategy = GenerationType.SEQUENCE).

希望这对你有帮助

© www.soinside.com 2019 - 2024. All rights reserved.