您的位置:首页 > 产品设计 > UI/UE

7.3 Hibernate:内置生成器 -- guid

2017-07-02 20:33 357 查看
使用由字符串类型的数据库生成的 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.o
4000
penSession();
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 单元测试】,测试结果相同。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate