使用JPA和Hibernate时如何选择id生成策略 [英] How to choose the id generation strategy when using JPA and Hibernate

查看:28
本文介绍了使用JPA和Hibernate时如何选择id生成策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Hibernate 参考指南的 Id 生成部分和Java 持久性与 Hibernate"

I was going through Id generation section of the Hibernate reference guide and "java persistence with Hibernate"

Hibernate 和 JPA 的组合有很多可用的选项.

There are quite a few options available with Hibernate and JPA combined.

我正在寻找有关如何选择特定 ID 生成策略的进一步文档.

I was looking for a further documentation on how to choose the specific id generation strategy.

我也在寻找引爆点.

例如,hilo 策略有望减少争用.我假设必须有与此选择相关的权衡.

For example, hilo strategy is expected to reduce contention. I am assuming there must be a trade off associated with this choice.

我想接受权衡方面的教育.

I want to be educated about the trade offs.

有文献资料吗?

推荐答案

API Doc 对此非常清楚.

所有生成器都实现了接口 org.hibernate.id.IdentifierGenerator.这是一个非常简单的界面.一些应用程序可以选择提供自己的专用实现,但是,Hibernate 提供了一系列内置实现.内置生成器的快捷方式名称如下:

All generators implement the interface org.hibernate.id.IdentifierGenerator. This is a very simple interface. Some applications can choose to provide their own specialized implementations, however, Hibernate provides a range of built-in implementations. The shortcut names for the built-in generators are as follows:

增量

生成 long、short 或 int 类型的标识符,这些标识符仅在没有其他进程将数据插入同一个表时才是唯一的.请勿在集群中使用.

generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.

身份

支持 DB2、MySQL、MS SQL Server、Sybase 和 HypersonicSQL 中的标识列.返回的标识符的类型为 long、short 或 int.

supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.

顺序

使用 DB2、PostgreSQL、Oracle、SAP DB、McKoi 中的序列或 Interbase 中的生成器.返回的标识符是 long、short 或 int 类型

uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int

hilo

使用 hi/lo 算法有效地生成 long、short 或 int 类型的标识符,给定一个表和列(默认情况下分别为 hibernate_unique_key 和 next_hi)作为 hi 值的来源.hi/lo 算法生成仅对特定数据库唯一的标识符.

uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database.

seqhilo

在给定命名的数据库序列的情况下,使用 hi/lo 算法有效地生成 long、short 或 int 类型的标识符.

uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.

uuid

使用 128 位 UUID 算法生成在网络中唯一的字符串类型标识符(使用 IP 地址).UUID 编码为 32 个十六进制数字的字符串.

uses a 128-bit UUID algorithm to generate identifiers of type string that are unique within a network (the IP address is used). The UUID is encoded as a string of 32 hexadecimal digits in length.

指导

在 MS SQL Server 和 MySQL 上使用数据库生成的 GUID 字符串.

uses a database-generated GUID string on MS SQL Server and MySQL.

原生

根据底层数据库的功能选择身份、序列或 hilo.

selects identity, sequence or hilo depending upon the capabilities of the underlying database.

已分配

让应用程序在调用 save() 之前为对象分配一个标识符.如果未指定元素,则这是默认策略.

lets the application assign an identifier to the object before save() is called. This is the default strategy if no element is specified.

选择

通过按某个唯一键选择行并检索主键值来检索由数据库触发器分配的主键.

retrieves a primary key, assigned by a database trigger, by selecting the row by some unique key and retrieving the primary key value.

国外

使用另一个关联对象的标识符.它通常与主键关联结合使用.

uses the identifier of another associated object. It is usually used in conjunction with a primary key association.

序列标识

一种特殊的序列生成策略,它利用数据库序列进行实际的值生成,但将其与 JDBC3 getGeneratedKeys 结合起来,作为插入语句执行的一部分返回生成的标识符值.此策略仅在针对 JDK 1.4 的 Oracle 10g 驱动程序上受支持.由于 Oracle 驱动程序中的错误,对这些插入语句的注释被禁用.

a specialized sequence generation strategy that utilizes a database sequence for the actual value generation, but combines this with JDBC3 getGeneratedKeys to return the generated identifier value as part of the insert statement execution. This strategy is only supported on Oracle 10g drivers targeted for JDK 1.4. Comments on these insert statements are disabled due to a bug in the Oracle drivers.

如果您正在构建一个并发用户不多的简单应用程序,您可以选择增量、身份、hilo 等.这些配置简单,不需要在数据库内部进行太多编码.

If you are building a simple application with not much concurrent users, you can go for increment, identity, hilo etc.. These are simple to configure and did not need much coding inside the db.

您应该根据您的数据库选择sequenceguid.这些是安全和更好的,因为 id 生成将发生在数据库中.

You should choose sequence or guid depending on your database. These are safe and better because the id generation will happen inside the database.

更新:最近我们遇到了一个 idendity 问题,其中原始类型 (int) 是通过使用 warapper 类型 (Integer) 来修复的.

这篇关于使用JPA和Hibernate时如何选择id生成策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆