Hibernate JPA Oracle12C-主键(Sequence)上的约束冲突异常



我正在使用带有Hibernate JPA的SpringBoot应用程序。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

哪个是PrimaryKey/Unique Index-此表的列ID。

在使用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 ; 

因此,如果我遗漏了什么或有人遇到了这个问题,请征求专家意见。我解决这个问题的解决方案是有效的还是无效的?

在实体中,尝试将生成策略更改为Identity/sequence。见下文:@GeneratedValue(strategy = GenerationType.SEQUENCE)

希望这能有所帮助!

最新更新