Hibernate Jpa-主键(序列)上的约束违反异常 [英] Hibernate Jpa - constraint violation exception on Primary Key(Sequence)

查看:132
本文介绍了Hibernate Jpa-主键(序列)上的约束违反异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用了Hibernate JPA.我有一个具有主键(序列)的表.服务将记录插入到该表中.

I use Hibernate JPA in my application. I have a Table that has a Primary key(Sequence). Service inserts records to that table.

版本:Oracle 12c

Version: Oracle 12c

方言:org.hibernate.dialect.Oracle10gDialect

Dialect: org.hibernate.dialect.Oracle10gDialect

问题:

在负载测试过程中,我们遇到问题(在SEQUENCE键上违反唯一约束).

We face problem(Unique constraint violation on SEQUENCE Key) during the load testing.

问题:

  1. 此问题并非一直存在.但仅在负载测试期间.有人可以检查并帮助使用线程安全生成器吗?

  1. This issue is not occurring all the time. But only during load test. Can someone please check and help to use thread safe generator?

是DB端序列定义问题还是Java端?

Is it DB side sequence definition issue or at Java side?

数据库序列:

CREATE SEQUENCE MY_SEQ    
START WITH 1
INCREMENT BY 1
NOMINVALUE
NOMAXVALUE
CACHE 30
NOORDER;

CREATE TABLE MY_TABLE    (  
    MY_PRIMARY_KEY INT default MY_SEQ.nextval NOT NULL,
    VALUE_COL VARCHAR2(10) NULL       
);

实体:

public class MyTableEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "MY_PRIMARY_KEY")
    @GenericGenerator(
        name = "mySequenceGenerator",
        strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
        parameters = {
                @Parameter(name = "sequence_name", value = "SEQUENCE MY_SEQ"),
                @Parameter(name = "increment_size", value = "1")
        }
    )
    @GeneratedValue(generator = "mySequenceGenerator")
    private long myPrimaryKey;

    @Column(name = "VALUE")
    private String value;

}

推荐答案

Oracle 10方言

对于 Oracle10gDialect ,请使用此配置

@Id
@Column(name = "MY_PRIMARY_KEY")
@GeneratedValue(strategy=GenerationType.AUTO)
Long myPrimaryKey;

Hibernate创建一个表和一个序列:

Hibernate creates a table and a sequence:

create table MY_TABLE (
MY_PRIMARY_KEY number(19,0) not null, 
VALUE varchar2(255 char), 
primary key (MY_PRIMARY_KEY))

create sequence hibernate_sequence 

首先存储它,然后获得新的序列ID,然后将其传递到INSERT语句中

While storing it first gets the new sequence ID and than passes it in the INSERT statement

select hibernate_sequence.nextval from dual
insert into MY_TABLE (VALUE, MY_PRIMARY_KEY) values (?, ?)

Oracle 12方言

如果您使用本机支持Oracle 12 .html#GUID-F9CE0CC3-13AE-4744-A43C-EAC7A71AAAB6"rel =" nofollow noreferrer> IDENTITY column 最好升级到 Oracle12cDialect (请注意,这需要Hibernate 5.3 )

Oracle 12 Dialect

If you use Oracle 12 that natively supports IDENTITY column it is prefered to upgrade to Oracle12cDialect (note that this requires Hibernate 5.3)

strategy设置为GenerationType.IDENTITY

@Id
@Column(name = "MY_PRIMARY_KEY", updatable = false, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
Long myPrimaryKey;

将创建下表-重要的部分是generated as identity,它提供了独特的风格. 请注意,不需要创建显式sequence,而是在内部对其进行管理.

The following table is created - the important part is generated as identity which provides the unique velues. Note that no explicite sequence is required to be created, it is managed internally .

create table MY_TABLE (
MY_PRIMARY_KEY number(19,0) generated as identity, 
VALUE varchar2(255 char), 
primary key (MY_PRIMARY_KEY))

虽然存储未在INSERT中传递任何ID ,但该ID由Oracle分配并返回到会话

While storing no ID is passed in the INSERT, it is assigned by Oracle and returned to the session

insert into MY_TABLE (VALUE) values (?) RETURNING MY_PRIMARY_KEY INTO ? 

请注意,与Oracle 10相比,您节省了一次往返数据库的时间.

Note that in contrary to the Oracle 10 you save one round trip to the database.

这篇关于Hibernate Jpa-主键(序列)上的约束违反异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆