对于上下文,我正在开发一个项目,我希望从oracle数据库中自动生成oracle数据库中的主要ID。我正在使用Java Server Faces和Hibernate将项目添加到数据库中。
所以这是相关的代码部分:
创建序列语句
CREATE SEQUENCE "BIM***********"."SEQUENCE1" MINVALUE 1 MAXVALUE
9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20
NOORDER NOCYCLE ;
创建表语句
CREATE TABLE "BIMB2013WMMEE"."CLIENT"
( "CLIENT_ID" VARCHAR2(20 BYTE) NOT NULL ENABLE,
"CLIENT_CODE" VARCHAR2(20 BYTE) NOT NULL ENABLE,
"CLIENT_NAME" VARCHAR2(20 BYTE) NOT NULL ENABLE,
CONSTRAINT "CLIENT_PK" PRIMARY KEY ("CLIENT_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS" ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS" ;
CREATE OR REPLACE TRIGGER "BIMB2013WMMEE"."BEFORE"
before insert on "BIMB2013WMMEE"."CLIENT"
for each row
begin
if inserting then
if :NEW."CLIENT_ID" is null then
select SEQUENCE1.nextval into :NEW."CLIENT_ID" from dual;
end if;
end if;
end;
/
ALTER TRIGGER "BIMB2013WMMEE"."BEFORE" ENABLE;
这是违规的Java代码(据我所知)
@Entity
@Table (name="client")
public class Client {
//Fields
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column(name="CLIENT_ID")
private String id;
以下是来自Eclipse的错误消息:
Hibernate: select hibernate_sequence.nextval from dual
Jun 06, 2016 9:19:06 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper
logExceptions
WARN: SQL Error: 2289, SQLState: 42000
Jun 06, 2016 9:19:06 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper
logExceptions
ERROR: ORA-02289: sequence does not exist
Jun 06, 2016 9:19:06 AM
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:oracle:thin:@endeavour.us.manh.com:1523/pso11r2f]
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist
答案 0 :(得分:0)
在实体上使用@SequenceGenerator
注释,并使用序列生成器名称在generator
注释中指定@GeneratedValue
属性,如下所示:
@Entity
@Table (name="client")
@SequenceGenerator(name="sequence1",sequenceName="BIM***********.SEQUENCE1",
allocationSize=1, initialValue=1)
public class Client {
//Fields
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence1")
@Column(name="CLIENT_ID")
private String id;
答案 1 :(得分:0)
尝试使用@SequenceGenerator + @GeneratedValue注释。
@Id
@SequenceGenerator(name="sequence1",sequenceName="BIM***********.SEQUENCE1")
@GeneratedValue(generator="sequence1")
@Column(name="CLIENT_ID")
private String id;