我的实体类:
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
}
我的表结构:
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
)
我正在使用Spring 3,Hibernate 4和oracle database.While运行我的应用程序iam gettin gbelow错误:
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)
我也试过了GenerationType.AUTO
,但后来我得到了sequence not found
这样的东西。
请帮忙。我知道这个问题与@GeneratedValue
有关。请告诉我上面的表oad oracle数据库的值。
答案 0 :(得分:2)
您可以使用GenericGenerator类的休眠。
@Id
@GenericGenerator(name = "increment", strategy = "increment")
@GeneratedValue(generator = "increment")
private int INSTANCE_KEY;
答案 1 :(得分:1)
在oracle数据库中创建序列:并注释id以使用此序列:
CREATE SEQUENCE SEQ_APP_INSTANCE
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
答案 2 :(得分:1)
在INSTANCE_KEY字段中设置此项:
@Id
@Column(name="INSTANCE_KEY")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqInstanceKey")
@SequenceGenerator(name = "seqInstanceKey", sequenceName = "SEQ_INSTANCE_KEY", allocationSize = 1)
private int INSTANCE_KEY;
在您的数据库中添加一个名称为' SEQ_INSTANCE_KEY'的序列。财产增量乘以' 1'