Hibernate 根据使用的数据库自行判断采用 identity
,hilo
或 sequence
其中一种作为主键生成方式,非常灵活。
如果使用的是 Oracle 数据库,Hibernate 需要使用一个叫 hibernate_sequence
的 sequence 。
除 Oracle 数据库外,针对大部分其他数据库,native
泛指自增。
注意:如果 Hibernate 自动选择 sequence 或者 hilo,则所有表的主键都会从 Hibernate 默认的 sequence 或 hilo 表中取。并且有的数据库对于默认情况主键生成测试的支持,效率并不高。
使用 sequence 或 hilo 时可以加入参数,指定 sequence 名称或 hi 值表名称,如
<param name="sequence">hibernate_id</param>
特点:根据数据库自动选择,项目中如果用到多个数据库时可以使用此方式,使用时需要设置表的自增字段或建立序列、建表等。
使用 MySQL 演示:
1 使用 XML
1.1 持久化类定义:
package hibernate;
import java.util.Date;
public class Person {
private Integer id;
private String account;
private String name;
private Date birth;
public Person() {}
public Person(String account, String name, Date birth) {
this.account = account;
this.name = name;
this.birth = birth;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Person [id=" + id + ", account=" + account + ", name=" + name + ", birth=" + birth + "]";
}
}
1.2 定义映射:
<?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>
<class name="hibernate.Person" table="PERSON">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="account" type="java.lang.String">
<column name="ACCOUNT" />
</property>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="birth" type="java.util.Date">
<column name="BIRTH" />
</property>
</class>
</hibernate-mapping>
1.3 单元测试:
package hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateTest {
@Test
public void test() {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person("admin", "Nick", new Date(System.currentTimeMillis()));
session.save(person);
transaction.commit();
session.close();
sessionFactory.close();
}
}
单元测试通过,查询数据库新插入一条数据,ID
为 1
。
2 使用注解(annotation)
使用注解定义持久化类:
package hibernate;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table
public class Person {
@Id
@GeneratedValue(generator = "assignedGenerator")
@GenericGenerator(name = "assignedGenerator", strategy = "native")
private Integer id;
private String account;
private String name;
private Date birth;
public Person() {}
public Person(String account, String name, Date birth) {
this.account = account;
this.name = name;
this.birth = birth;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Person [id=" + id + ", account=" + account + ", name=" + name + ", birth=" + birth + "]";
}
}
运行【1.3 单元测试】,测试结果相同。