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

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

问题描述



Hibernate和JPA结合使用的选项有很多。



我正在寻找关于如何选择特定id生成策略的进一步文档。



我也是寻找转折点。



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



我希望受到关于权衡的教育。



是否有任何文献可用? API Doc 对此非常清楚。


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

增量

生成long,short或int类型的唯一标识符,只有在没有其他进程将数据插入到同一个表中时才是唯一的。不要在集群中使用。

身份



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

序列

在Interbase中使用DB2,PostgreSQL,Oracle,SAP DB,McKoi或生成器中的序列。返回的标识符的类型为long,short或int

hilo



用法一个hi / lo算法,用于高效生成long,short或int类型的标识符,给定一个表和列(分别默认为hibernate_unique_key和next_hi)作为hi值的来源。 hi / lo算法会生成仅针对特定数据库的唯一标识符。

seqhilo


$ b $给定一个命名的数据库序列,b

使用hi / lo算法有效地生成long,short或int类型的标识符。

uuid

使用128位的UUID算法来生成网络中唯一的字符串类型(使用IP地址)的标识符。 UUID的长度为32位十六进制数字。

guid

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



本地



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

分配

允许应用程序在调用save()之前为对象分配一个标识符。如果没有指定元素,这是默认策略。



select
$ b p检索一个由数据库触发器分配的主键,通过按某个唯一键选择行并检索主键值。


$ b foreign



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

序列标识

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


如果您正在构建一个简单的应用程序,用户,你可以使用 increment,identity,hilo 等等。这些配置很简单,并且不需要在db内部编写很多代码。



您应根据数据库选择序列 GUID 。这些都是安全和更好的,因为 id 代会在数据库内部发生。



更新:
最近我们遇到了一个原始类型(int)的问题,它通过使用warapper类型(Integer)来解决。

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

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

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

I am also looking for tipping points.

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.

Is there any literature available?

解决方案

The API Doc are very clear on this.

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:

increment

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.

identity

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

sequence

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

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

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

uuid

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.

guid

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

native

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

assigned

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

select

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

foreign

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

sequence-identity

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.

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.

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

Update: Recently we had an an issue with idendity where primitive type (int) this was fixed by using warapper type (Integer) instead.

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

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