首页 > 代码库 > Hibernate在oracle中ID增长的方式
Hibernate在oracle中ID增长的方式
引用链接:http://blog.csdn.net/w183705952/article/details/7367272
Hibernate在oracle中ID增长的方式
第一种:设置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;
创建DEPARTMENT表,并为DEPARTMENT表创建一个单独的SEQUENCE,名字为SEQUENCE_ID_SEQ,并不需要创建触发器。
[2]hibernate映射文件的配置:
Java代码
<?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在oracle中ID增长的方式