根据hibernate的文档,有两种方式实现实体对象的主键自动增长。
第一种: 设置ID的增长策略是sequence,同时指定sequence的名字,最好每个表建一个sequence,此种做法就如同MS-SQL,MY-SQL中的自动增长一样,不需要创建触发器,具体的oracle数据库脚本及hibernate配置文件如下:
[1]oracle数据表的创建脚本:
- CREATE TABLE DEPARTMENT (
- ID NUMBER(19 , 0 ) DEFAULT '0' NOT NULL,
- NAME VARCHAR2(255 ) NOT NULL,
- DESCRIPTION CLOB
- );
- ALTER TABLE DEPARTMENT ADD CONSTRAINT PRIMARY_0 PRIMARY KEY(ID) ENABLE;
- ALTER TABLE DEPARTMENT ADD CONSTRAINT UK_DEPARTMENT_1 UNIQUE (NAME);
- CREATE SEQUENCE DEPARTMENT_ID_SEQ MINVALUE 10000 MAXVALUE 999999999999999999999999 INCREMENT BY 1 NOCYCLE;
- CREATE TABLE DEPARTMENT (
- ID NUMBER(19,0) DEFAULT '0' NOT NULL,
- NAME VARCHAR2(255) NOT NULL,
- DESCRIPTION CLOB
- );
- ALTER TABLE DEPARTMENT ADD CONSTRAINT PRIMARY_0 PRIMARY KEY(ID) ENABLE;
- ALTER TABLE DEPARTMENT ADD CONSTRAINT UK_DEPARTMENT_1 UNIQUE (NAME);
- CREATE SEQUENCE DEPARTMENT_ID_SEQ MINVALUE 10000 MAXVALUE 999999999999999999999999 INCREMENT BY 1 NOCYCLE;
CREATE TABLE DEPARTMENT (
ID NUMBER(19,0) DEFAULT '0' NOT NULL,
NAME VARCHAR2(255) NOT NULL,
DESCRIPTION CLOB
);
ALTER TABLE DEPARTMENT ADD CONSTRAINT PRIMARY_0 PRIMARY KEY(ID) ENABLE;
ALTER TABLE DEPARTMENT ADD CONSTRAINT UK_DEPARTMENT_1 UNIQUE (NAME);
CREATE SEQUENCE DEPARTMENT_ID_SEQ MINVALUE 10000 MAXVALUE 999999999999999999999999 INCREMENT BY 1 NOCYCLE;
创建DEPARTMENT表,并为DEPARTMENT表创建一个单独的SEQUENCE,名字为SEQUENCE_ID_SEQ,并不需要创建触发器。
[2]hibernate映射文件的配置:
- <?xml version= "1.0" ?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
- <hibernate-mapping package = "com.liyanframework.demo.domain" >
- <class name= "Department" table= "DEPARTMENT" >
- <id name="id" column= "ID" >
- <generator class = "sequence" >
- <param name="sequence" >DEPARTMENT_ID_SEQ</param>
- </generator>
- </id>
- <property name="name" column= "NAME" type= "string" />
- <property name="description" column= "DESCRIPTION" type= "text" />
- </class >
- </hibernate-mapping>
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.liyanframework.demo.domain">
- <class name="Department" table="DEPARTMENT">
- <id name="id" column="ID">
- <generator class="sequence">
- <param name="sequence">DEPARTMENT_ID_SEQ</param>
- </generator>
- </id>
- <property name="name" column="NAME" type="string" />
- <property name="description" column="DESCRIPTION" type="text" />
- </class>
- </hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.liyanframework.demo.domain">
<class name="Department" table="DEPARTMENT">
<id name="id" column="ID">
<generator class="sequence">
<param name="sequence">DEPARTMENT_ID_SEQ</param>
</generator>
</id>
<property name="name" column="NAME" type="string" />
<property name="description" column="DESCRIPTION" type="text" />
</class>
</hibernate-mapping>
在hibernate映射文件中,对ID的生成策略选择sequence,指定sequence的名字DEPARTMENT_ID_SEQ就可以了,当你保存新对象的时候,hibernate会自动取得DEPARTMENT_ID_SEQ.NEXTVAL作为新对象的ID保存到数据库,所以不需要再使用触发器再来生成新记录的ID。
[/list]
第二种: 设置ID的增长策略是native,但是需要创建一个名字为 hibernate_sequence(这个名字好像是hibernate默认的sequence名字,不创建会出错的)的全局使用的sequence,然后再对每一个表的ID生成的时候,使用触发器,取得hibernate_sequence.CURRVAL作为新记录的ID,具体的oracle数据库脚本及hibernate配置文件如下:
[list]
[1]oracle数据表的创建脚本:
- CREATE TABLE STAFF (
- ID NUMBER(19 , 0 ) DEFAULT '0' NOT NULL,
- NAME VARCHAR2(255 ) NOT NULL,
- AGE NUMBER(3 , 0 ) NOT NULL,
- BIRTHDAY DATE NOT NULL,
- SALARY NUMBER(10 , 2 ) NOT NULL,
- LEVELNESS FLOAT NOT NULL,
- CREATETIME TIMESTAMP NOT NULL,
- ENABLE CHAR(2 ) DEFAULT 'Y' NOT NULL,
- STATUS VARCHAR2(64 ) NOT NULL,
- DEPARTMENT_ID NUMBER(19 , 0 )
- );
- ALTER TABLE STAFF ADD CONSTRAINT PRIMARY_1 PRIMARY KEY(ID) ENABLE;
- ALTER TABLE STAFF ADD CONSTRAINT STAFF_IBFK_0 FOREIGN KEY(DEPARTMENT_ID) REFERENCES DEPARTMENT(ID) ENABLE;
- ALTER TABLE STAFF ADD CONSTRAINT UK_STAFF_1 UNIQUE (NAME);
- CREATE INDEX IDX_STAFF_STATUS ON STAFF(STATUS);
- CREATE SEQUENCE HIBERNATE_SEQUENCE MINVALUE 90000 MAXVALUE 999999999999999999999999 INCREMENT BY 1 NOCYCLE;
- CREATE OR REPLACE TRIGGER STAFF_ID_TRG BEFORE INSERT ON STAFF
- FOR EACH ROW
- BEGIN
- IF INSERTING AND :NEW.ID IS NULL THEN
- SELECT HIBERNATE_SEQUENCE.CURRVAL INTO :NEW.ID FROM DUAL;
- END IF;
- END;
- CREATE TABLE STAFF (
- ID NUMBER(19,0) DEFAULT '0' NOT NULL,
- NAME VARCHAR2(255) NOT NULL,
- AGE NUMBER(3,0) NOT NULL,
- BIRTHDAY DATE NOT NULL,
- SALARY NUMBER(10,2) NOT NULL,
- LEVELNESS FLOAT NOT NULL,
- CREATETIME TIMESTAMP NOT NULL,
- ENABLE CHAR(2) DEFAULT 'Y' NOT NULL,
- STATUS VARCHAR2(64) NOT NULL,
- DEPARTMENT_ID NUMBER(19,0)
- );
- ALTER TABLE STAFF ADD CONSTRAINT PRIMARY_1 PRIMARY KEY(ID) ENABLE;
- ALTER TABLE STAFF ADD CONSTRAINT STAFF_IBFK_0 FOREIGN KEY(DEPARTMENT_ID) REFERENCES DEPARTMENT(ID) ENABLE;
- ALTER TABLE STAFF ADD CONSTRAINT UK_STAFF_1 UNIQUE (NAME);
- CREATE INDEX IDX_STAFF_STATUS ON STAFF(STATUS);
- CREATE SEQUENCE HIBERNATE_SEQUENCE MINVALUE 90000 MAXVALUE 999999999999999999999999 INCREMENT BY 1 NOCYCLE;
- CREATE OR REPLACE TRIGGER STAFF_ID_TRG BEFORE INSERT ON STAFF
- FOR EACH ROW
- BEGIN
- IF INSERTING AND :NEW.ID IS NULL THEN
- SELECT HIBERNATE_SEQUENCE.CURRVAL INTO :NEW.ID FROM DUAL;
- END IF;
- END;
CREATE TABLE STAFF (
ID NUMBER(19,0) DEFAULT '0' NOT NULL,
NAME VARCHAR2(255) NOT NULL,
AGE NUMBER(3,0) NOT NULL,
BIRTHDAY DATE NOT NULL,
SALARY NUMBER(10,2) NOT NULL,
LEVELNESS FLOAT NOT NULL,
CREATETIME TIMESTAMP NOT NULL,
ENABLE CHAR(2) DEFAULT 'Y' NOT NULL,
STATUS VARCHAR2(64) NOT NULL,
DEPARTMENT_ID NUMBER(19,0)
);
ALTER TABLE STAFF ADD CONSTRAINT PRIMARY_1 PRIMARY KEY(ID) ENABLE;
ALTER TABLE STAFF ADD CONSTRAINT STAFF_IBFK_0 FOREIGN KEY(DEPARTMENT_ID) REFERENCES DEPARTMENT(ID) ENABLE;
ALTER TABLE STAFF ADD CONSTRAINT UK_STAFF_1 UNIQUE (NAME);
CREATE INDEX IDX_STAFF_STATUS ON STAFF(STATUS);
CREATE SEQUENCE HIBERNATE_SEQUENCE MINVALUE 90000 MAXVALUE 999999999999999999999999 INCREMENT BY 1 NOCYCLE;
CREATE OR REPLACE TRIGGER STAFF_ID_TRG BEFORE INSERT ON STAFF
FOR EACH ROW
BEGIN
IF INSERTING AND :NEW.ID IS NULL THEN
SELECT HIBERNATE_SEQUENCE.CURRVAL INTO :NEW.ID FROM DUAL;
END IF;
END;
创建STAFF表,但是并没有为STAFF创建相应的主键sequence,而是创建了一个名字为HIBERNATE_SEQUENCE的 sequence,然后创建一个触发器STAFF_ID_TRG,当执行INSERT操作时,hibernate会先执行一次 HIBERNATE_SEQUENCE.NEXTVAL,所以在触发器中只需要取得HIBERNATE_SEQUENCE.CURRVAL作为新记录的 ID。
[2]hibernate映射文件的配置:
- <?xml version= "1.0" ?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
- <hibernate-mapping package = "com.liyanframework.demo.domain" >
- <class name= "Staff" table= "STAFF" >
- <id name="id" column= "ID" >
- <generator class = "native" />
- </id>
- <property name="name" column= "NAME" type= "string" />
- <property name="age" column= "AGE" type= "integer" />
- <property name="birthday" column= "BIRTHDAY" type= "date" />
- <property name="salary" column= "SALARY" type= "big_decimal" />
- <property name="level" column= "LEVELNESS" type= "float" />
- <property name="createTime" column= "CREATETIME" type= "timestamp" />
- <property name="enable" column= "ENABLE" type= "character" />
- <property name="status" column= "STATUS" type= "string" />
- <many-to-one name="department" column= "DEPARTMENT_ID" class = "Department" />
- </class >
- </hibernate-mapping>
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="com.liyanframework.demo.domain">
- <class name="Staff" table="STAFF">
- <id name="id" column="ID">
- <generator class="native" />
- </id>
- <property name="name" column="NAME" type="string" />
- <property name="age" column="AGE" type="integer" />
- <property name="birthday" column="BIRTHDAY" type="date" />
- <property name="salary" column="SALARY" type="big_decimal" />
- <property name="level" column="LEVELNESS" type="float" />
- <property name="createTime" column="CREATETIME" type="timestamp" />
- <property name="enable" column="ENABLE" type="character" />
- <property name="status" column="STATUS" type="string" />
- <many-to-one name="department" column="DEPARTMENT_ID" class="Department" />
- </class>
- </hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.liyanframework.demo.domain">
<class name="Staff" table="STAFF">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="name" column="NAME" type="string" />
<property name="age" column="AGE" type="integer" />
<property name="birthday" column="BIRTHDAY" type="date" />
<property name="salary" column="SALARY" type="big_decimal" />
<property name="level" column="LEVELNESS" type="float" />
<property name="createTime" column="CREATETIME" type="timestamp" />
<property name="enable" column="ENABLE" type="character" />
<property name="status" column="STATUS" type="string" />
<many-to-one name="department" column="DEPARTMENT_ID" class="Department" />
</class>
</hibernate-mapping>
在hibernate映射文件中,对ID的生成策略选择native,hibernate会根据你数据库的触发器来生成新记录的ID。
比较两种做法,第二种做法也就是hibernate在代码中,实现了oracle中的触发器功能。对于不同的情况,选择不懂的做法。如果新的系统,新建的oracle数据库,推荐使用第一种做法,简单,容易移植到其他支持自动增长的数据库;如果是老的系统,需要把其他数据库转换为oracle的,那就要用第二种了,使用native的方式,可以不改动配置文件,兼容oracle和 mysql之类带有自动增长的数据库。