在Hibernate中使用Oracle的sequence主键
在Oracle中建立sequence,名称为pk:
create sequence pk
start with 1
increment by 1
maxvalue 2000
nocycle
cache 30;
在映射文件中修改:
<class name="com.kay.pojo.Student" table="STUDENT" schema="KAY">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">pk</param>//pk为sequence名称
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" />
</property>
</class>
测试代码:
@Test
public void testAdd() {
Student stu = new Student();
stu.setName("Bill");
boolean flag = dao.add(stu);
assertEquals(true, flag);
}
控制台输出:
Hibernate: select pk.nextval from dual
Hibernate: insert into KAY.STUDENT (NAME, ID) values (?, ?)
create sequence pk
start with 1
increment by 1
maxvalue 2000
nocycle
cache 30;
在映射文件中修改:
<class name="com.kay.pojo.Student" table="STUDENT" schema="KAY">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">pk</param>//pk为sequence名称
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" />
</property>
</class>
测试代码:
@Test
public void testAdd() {
Student stu = new Student();
stu.setName("Bill");
boolean flag = dao.add(stu);
assertEquals(true, flag);
}
控制台输出:
Hibernate: select pk.nextval from dual
Hibernate: insert into KAY.STUDENT (NAME, ID) values (?, ?)