1、创建表
CREATE TABLE T_CUST(
CUST_ID INT NOT NULL,
CUST_NAME VARCHAR2(20) NOT NULL,
CONSTRAINT PK_T_CUST PRIMARY KEY (CUST_ID)
);
2、创建序列
CREATE SEQUENCE S_CUST_ID
MINVALUE 1
MAXVALUE 10000000
START WITH 1
INCREMENT BY 1
NOCACHE;
3、试试序列是否起作用
INSERT INTO T_CUST(CUST_ID, CUST_NAME) VALUES(S_CUST_ID.NEXTVAL, 'hahaha');
SELECT * FROM T_CUST;
结果:
|CUST_ID|CUST_NAME|
|1 |hahaha |
4、配置TCust.hbm.xml文件
<hibernate-mapping>
<class name="demo.hb.sequence.Cust" table="T_CUST" schema="DEMO">
<id name="custId" type="java.lang.Long">
<column name="CUST_ID" precision="22" scale="0" />
<!-- 使用Oracle Sequence的重点 -->
<generator class="sequence">
<!-- Oracle Sequence的名字 -->
<param name="sequence">S_CUST_ID</param>
</generator>
</id>
<property name="custName" type="java.lang.String">
<column name="CUST_NAME" length="20" not-null="true" />
</property>
</class>
</hibernate-mapping>