Hibernate自增主键
Hibernate自增主键(oracle数据库)可以通过序列来实现。
(1)创建表person和序列person_sequence
?
-----建表-------create table person( id number(10) primary key, name varchar2(10) not null, age number(3) )-----建序列-----CREATE SEQUENCE person_sequence INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE CACHE 10;
?(2)src下新建demo包,包中新建person的pojo类。
?(3)person.java同目录下新建Person.hbm.xml
?
<?xml version="1.0" encoding='UTF-8'?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="demo"> <class name="Person" table="person"> <id name="id" column="ID" type="int"> <generator column="NAME" type="string" not-null="true" /> <property name="age" column="AGE" type="int" /> </class> </hibernate-mapping>
?按照这样的配置即可以实现主键的自增。