Hibernate-主键生成策略汇总

本文详细介绍了Hibernate的主键生成策略,包括increment、identity、sequence、hilo、seqhilo、uuid、uuid2、guid、native、assigned、select和foreign等。不同策略适用于不同的数据库环境,例如increment在非集群环境中适用,identity支持MySQL、SQL Server等,sequence适用于Oracle等,而uuid和uuid2则生成128位UUID。此外,还提到了native策略会根据数据库能力自动选择策略,以及sequence-identity策略的特殊性,它结合了数据库序列和JDBC3的getGeneratedKeys功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hibernate 提供了很多内置生成策略的实现。下面是一些内置生成器的快捷名字:

increment

用于为 long, short 或者 int 类型生成 唯一标识。只有在没有其他进程往同一张表中插入数据时才能使用。在集群下不要使用。

identity

对 DB2,MySQL,MS SQL Server,Sybase 和 HypersonicSQL 的内置标识字段提供支持。返回的标识符是
long,short 或者 int 类型的。

sequence

在 DB2,PostgreSQL,Oracle,SAP DB,McKoi 中使用序列(sequence), 而在 Interbase
中使用生成器(generator)。返回的标识符是 long,short 或者 int 类型的。

hilo

使用一个高/低位算法高效的生成 long,short 或者 int 类型的标识符。给定一个表和字段(默认分别是
hibernate_unique_key 和 next_hi)作为高位值的来源。高/低位算法生成的标识符只在一个特定的数据库中是唯一的。

seqhilo

使用一个高/低位算法来高效的生成 long,short 或者 int 类型的标识符,给定一个数据库序列(sequence)的名字。

uuid

Generates a 128-bit UUID based on a custom algorithm. The value
generated is represented as a string of 32 hexidecimal digits. Users
can also configure it to use a separator (config parameter
“separator”) which separates the hexidecimal digits into
8{sep}8{sep}4{sep}8{sep}4. Note specifically that this is different
than the IETF RFC 4122 representation of 8-4-4-4-12. If you need RFC
4122 compliant UUIDs, consider using “uuid2” generator discussed
below.

uuid2

Generates a IETF RFC 4122 compliant (variant 2) 128-bit UUID. The
exact “version” (the RFC term) generated depends on the pluggable
“generation strategy” used (see below). Capable of generating values
as java.util.UUID, java.lang.String or as a byte array of length 16
(byte[16]). The “generation strategy” is defined by the interface
org.hibernate.id.UUIDGenerationStrategy. The generator defines 2
configuration parameters for defining which generation strategy to
use:

uuid_gen_strategy_class

Names the UUIDGenerationStrategy class to use

uuid_gen_strategy

Names the UUIDGenerationStrategy instance to use

Out of the box, comes with the following strategies:

org.hibernate.id.uuid.StandardRandomStrategy (the default) - generates
“version 3” (aka, “random”) UUID values via the randomUUID method of
java.util.UUID org.hibernate.id.uuid.CustomVersionOneStrategy -
generates “version 1” UUID values, using IP address since mac address
not available. If you need mac address to be used, consider leveraging
one of the existing third party UUID generators which sniff out mac
address and integrating it via the
org.hibernate.id.UUIDGenerationStrategy contract. Two such libraries
known at time of this writing include
http://johannburkard.de/software/uuid/ and
http://commons.apache.org/sandbox/id/uuid.html

guid

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

native

根据底层数据库的能力选择 identity、sequence 或者 hilo 中的一个。

assigned

让应用程序在调用 save() 之前为对象分配一个标识符。这是 元素没有指定时的默认生成策略。

select

通过数据库触发器选择一些唯一主键的行并返回主键值来分配一个主键。

foreign

使用另外一个相关联的对象的标识符。它通常和 联合起来使用。

sequence-identity

一种特别的序列生成策略,它使用数据库序列来生成实际值,但将它和 JDBC3 的 getGeneratedKeys
结合在一起,使得在插入语句执行的时候就返回生成的值。目前为止只有面向 JDK 1.4 的 Oracle 10g 驱动支持这一策略。由于
Oracle 驱动程序的一个 bug,这些插入语句的注释被关闭了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值