7.11 Hibernate:内置生成器 – uuid

2023-12-16 04:48

本文主要是介绍7.11 Hibernate:内置生成器 – uuid,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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;}@Overridepublic 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 {@Testpublic 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;}@Overridepublic String toString() {return "Person [id=" + id + ", account=" + account + ", name=" + name + ", birth=" + birth + "]";}}

运行【1.3 单元测试】,测试结果相同。

这篇关于7.11 Hibernate:内置生成器 – uuid的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/499149

相关文章

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

Mybatis官方生成器的使用方式

《Mybatis官方生成器的使用方式》本文详细介绍了MyBatisGenerator(MBG)的使用方法,通过实际代码示例展示了如何配置Maven插件来自动化生成MyBatis项目所需的实体类、Map... 目录1. MyBATis Generator 简介2. MyBatis Generator 的功能3

java面试常见问题之Hibernate总结

1  Hibernate的检索方式 Ø  导航对象图检索(根据已经加载的对象,导航到其他对象。) Ø  OID检索(按照对象的OID来检索对象。) Ø  HQL检索(使用面向对象的HQL查询语言。) Ø  QBC检索(使用QBC(Qurey By Criteria)API来检索对象。 QBC/QBE离线/在线) Ø  本地SQL检索(使用本地数据库的SQL查询语句。) 包括Hibern

uuid.js 使用

相关代码 import { NIL } from "uuid";/** 验证UUID* 为空 则返回 false* @param uuid* @returns {boolean}*/export function MyUUIDValidate(uuid: any): boolean {if (typeof uuid === "string" && uuid !== NIL) { //uuid

Python 内置的一些数据结构

文章目录 1. 列表 (List)2. 元组 (Tuple)3. 字典 (Dictionary)4. 集合 (Set)5. 字符串 (String) Python 提供了几种内置的数据结构来存储和操作数据,每种都有其独特的特点和用途。下面是一些常用的数据结构及其简要说明: 1. 列表 (List) 列表是一种可变的有序集合,可以存放任意类型的数据。列表中的元素可以通过索

org.hibernate.hql.ast.QuerySyntaxException:is not mapped 异常总结

org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [select u from User u where u.userName=:userName and u.password=:password] 上面的异常的抛出主要有几个方面:1、最容易想到的,就是你的from是实体类而不是表名,这个应该大家都知道,注意

Caused by: org.hibernate.MappingException: Could not determine type for: org.cgh.ssh.pojo.GoodsType,

MappingException:这个主要是类映射上的异常,Could not determine type for: org.cgh.ssh.pojo.GoodsType,这句话表示GoodsType这个类没有被映射到

Hibernate框架中,使用JDBC语法

/*** 调用存储过程* * @param PRONAME* @return*/public CallableStatement citePro(final String PRONAME){Session session = getCurrentSession();CallableStatement pro = session.doReturningWork(new ReturningWork<C

hibernate修改数据库已有的对象【简化操作】

陈科肇 直接上代码: /*** 更新新的数据并并未修改旧的数据* @param oldEntity 数据库存在的实体* @param newEntity 更改后的实体* @throws IllegalAccessException * @throws IllegalArgumentException */public void updateNew(T oldEntity,T newEntity

python内置模块datetime.time类详细介绍

​​​​​​​Python的datetime模块是一个强大的日期和时间处理库,它提供了多个类来处理日期和时间。主要包括几个功能类datetime.date、datetime.time、datetime.datetime、datetime.timedelta,datetime.timezone等。 ----------动动小手,非常感谢各位的点赞收藏和关注。----------- 使用datet