Hibernate-主键生成策略汇总
2017-10-12 23:19
344 查看
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,这些插入语句的注释被关闭了。
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,这些插入语句的注释被关闭了。
相关文章推荐
- Hibernate主键生成策略汇总
- Hibernate与TopLink在主键自动生成策略上的差异
- Hibernate实现,使用UUID.主键的生成策略
- 基于Annotation的Hibernate主键生成策略
- 常用Hibernate 主键生成策略
- 关于Hibernate的oracle主键id生成策略uuid、native和sequence
- hibernate注解主键生成策略
- hibernate的主键生成策略
- Hibernate主键生成策略总结
- Oracle: hibernate 的主键生成策略
- Hibernate--Increment和Hilo主键生成策略原理
- hibernate 主键生成策略
- hibernate框架(4)---主键生成策略
- 新手上路之Hibernate(二):基本映射以及一些原则与主键生成策略
- Hibernate 中关于主键生成策略之联合主键
- Hibernate主键生成策略和持久化对象的三种状态和MySql常用的数据库语句
- Hibernate各种主键生成策略与配置详解
- Hibernate之ID主键生成策略
- 基于按annotation的hibernate主键生成策略
- Hibernate进阶之ID主键生成策略