本文介绍了Hibernate:strategy = oracle的GenerationType值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的实体类:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name =APP_INSTANCE)
public class ApplicationEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =INSTANCE_KEY)
private int INSTANCE_KEY;
//其他代码
}
我的表结构:
$ $ $ $ $ $ $ $ $ $ $ $'$ $ $ $ $'$ $ $ $'$ $ $ $'$ $ $ $ $ $ $ $ $''$'$'$'
NAMEVARCHAR2(255),
ENVVARCHAR2(50),
PRIMARY KEY(APP_INSTANCE_KEY)ENABLE
)
$ c $我正在使用Spring 3,Hibernate 4和oracle数据库。在运行我的应用程序iam gettin gbelow错误时:
$
03:23:07,380 INFO [STDOUT] Hibernate:插入到USER.APP_INSTANCE(SYS_ID,NAME,ENV)值(?,?,?)
03:23:08,271信息[标准输出]警告:org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL错误:1400,SQLState:23000
03:23:08,271信息[标准输出]错误:org.hibernate。 engine.jdbc.spi.SqlExceptionHelper - ORA-01400:无法将NULL插入(USER。APP_INSTANCE。INSTANCE_KEY)
03:23:08,271信息[STDOUT]保存中出现异常..
03:23:08,271错误[STDERR] org.hibernate.excepti on.ConstraintViolationException:无法执行语句
03:23:08,271 ERROR [STDERR] at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
03:23:08,271错误[STDERR] at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
03:23:08,271 ERROR [STDERR] at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert( SqlExceptionHelper.java:124)
我也试过 GenerationType.AUTO
但后来我得到了像顺序找不到
。
请帮忙。我知道这个问题与 @GeneratedValue
有关。请让我知道上面的表nad oracle数据库的值。
解决方案
在oracle数据库中创建序列:
CREATE SEQUENCE SEQ_APP_INSTANCE
以1
开始增加1
NOCACHE
NOCYCLE;
并注释id以使用此序列:
@GeneratedValue(generator =seq_item,strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name =seq_item,sequenceName =SEQ_APP_INSTANCE,allocationSize = 10)
@Column(name =INSTANCE_KEY)
private int INSTANCE_KEY;
My entity class :
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="APP_INSTANCE")
public class ApplicationEntity {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="INSTANCE_KEY")
private int INSTANCE_KEY;
//other code
}
My Table Structure :
CREATE TABLE "APP_INSTANCE"
("INSTANCE_KEY" NUMBER NOT NULL ENABLE,
"SYS_ID" VARCHAR2(255) ,
"NAME" VARCHAR2(255) ,
"ENV" VARCHAR2(50) ,
PRIMARY KEY ("APP_INSTANCE_KEY") ENABLE
)
I am using Spring 3, Hibernate 4 and oracle database.While running my application iam gettin gbelow error :
03:23:07,380 INFO [STDOUT] Hibernate: insert into USER.APP_INSTANCE (SYS_ID, NAME, ENV) values (?, ?, ?)
03:23:08,271 INFO [STDOUT] WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1400, SQLState: 23000
03:23:08,271 INFO [STDOUT] ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ORA-01400: cannot insert NULL into ("USER"."APP_INSTANCE"."INSTANCE_KEY")
03:23:08,271 INFO [STDOUT] Exception in saving..
03:23:08,271 ERROR [STDERR] org.hibernate.exception.ConstraintViolationException: could not execute statement
03:23:08,271 ERROR [STDERR] at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
03:23:08,271 ERROR [STDERR] at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
03:23:08,271 ERROR [STDERR] at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:124)
I have also tried GenerationType.AUTO
but then i got somthing like sequence not found
.
Please help.I know that problem is related to @GeneratedValue
. Please let me know the value for above table nad oracle database.
解决方案
Create sequence in oracle databae:
CREATE SEQUENCE SEQ_APP_INSTANCE
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
and annotate id to use this sequence:
@GeneratedValue(generator="seq_item", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name="seq_item",sequenceName="SEQ_APP_INSTANCE", allocationSize=10)
@Column(name="INSTANCE_KEY")
private int INSTANCE_KEY;
这篇关于Hibernate:strategy = oracle的GenerationType值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!