7.7 Hibernate:内置生成器 – native

Hibernate 根据使用的数据库自行判断采用 identityhilosequence 其中一种作为主键生成方式,非常灵活。

如果使用的是 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();
    }

}

单元测试通过,查询数据库新插入一条数据,ID1
这里写图片描述

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 单元测试】,测试结果相同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

又言又语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值