UUID:Universally Unique Identifier,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。
按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字,标准的UUID格式为:
xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)
其中每个
x
是 0 – 9 或 a – f 范围内的一个十六进制的数字。
Hibernate 在保存对象时生成一个 UUID 字符串作为主键,保证了唯一性,但其并无任何业务逻辑意义,只能作为主键。唯一缺点是长度较大,32位(Hibernate 将 UUID 中的“-”屏蔽)字符串占用存储空间大,但是有两个很重要的优点:
(1) Hibernate 维护主键,不用去数据库查询,从而提升了效率;
(2) 跨数据库,且切换数据库很方便。
特点: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 + "]";
}
}
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" column="ID">
<generator class="uuid"/>
</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();
}
}
单元测试通过,查询数据库,hibernate 自动生成的表结构定义如下:
数据库中新插入两条记录,主键为 UUID 字符串。
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(name = "person")
public class Person {
@Id
@GeneratedValue(generator = "idGenerator")
@GenericGenerator(name = "idGenerator", strategy = "uuid")
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 单元测试】,测试结果相同。