7.3 Hibernate:内置生成器 -- guid

使用由字符串类型的数据库生成的 GUID。

GUID:Globally Unique Identifier,全球唯一标识符,也称作 UUID,是一个 128 位长的数字,用 16 进制表示。算法核心思想是结合机器的网卡、当地时间和一个随机数生成 GUID。

Hibernate 维护主键时先查询数据库,获得一个 uuid 字符串,即主键值。该值唯一,缺点是长度大且支持的数据库有限,优点是跨数据库,但仍然需要访问数据库。

注意:长度因数据库有所不同。

  • MySQL 中使用 SELECT UUID() 语句获得一个 36 位字符串(包含“-”的标准格式)
  • Oracle 中使用 SELECT RAWTOHEX(SYS_GUID()) FROM DUAL 语句获得一个 32 位字符串(不包含“-”)

特点:需要数据库支持查询 uuid,生成时需要查询数据库,效率没有 uuid 高,推荐使用 uuid。

使用 MySQL 演示:

1 使用 XML

1.1 持久化类定义:

package hibernate;

import java.util.Date;

public class Person {

    private String 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 String getId() {
        return id;
    }

    public void setId(String 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 + "]";
    }

}

注意:MySQL 生成的 GUID 是 36 位带“-”的标准 UUID 字符串,所以持久化类主键要定义为 String 类型,映射文件中也需保持一致。

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="java.lang.String">
      <column name="ID" />
      <generator class="guid" />
    </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();
        Date date = new Date(System.currentTimeMillis());
        Person person1 = new Person("admin1", "Nick", date);
        session.save(person1);
        Person person2 = new Person("admin2", "King", date);
        session.save(person2);
        transaction.commit();
        session.close();
        sessionFactory.close();
    }

}

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

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 = "guid")
    private String 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 String getId() {
        return id;
    }

    public void setId(String 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、付费专栏及课程。

余额充值