Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

java - Hibernate : strategy=GenerationType value for oracle

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use GenericGenerator class of hibernate.

@Id
@GenericGenerator(name = "increment", strategy = "increment")
@GeneratedValue(generator = "increment")
private int INSTANCE_KEY;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...