设置ID的增长策略是sequence,同时指定sequence的名字,最好每个表建一个sequence
例如创建一个序列:
CREATE SEQUENCE DEPARTMENT_ID_SEQ MINVALUE 10000 MAXVALUE 999999999999999999999999 INCREMENT BY 1 NOCYCLE;
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>
在
hibernate映射文件中,对ID的生成策略选择sequence,指定sequence的名字DEPARTMENT_ID_SEQ就可以了,当你保
存新对象的时候,hibernate会自动取得DEPARTMENT_ID_SEQ.NEXTVAL作为新对象的ID保存到数据库,所以不需要再使用触发
器再来生成新记录的ID。
时间: 2024-10-18 06:02:25