gpt4 book ai didi

java - 使用 JPA 和 Hibernate 时如何选择 id 生成策略

转载 作者:IT老高 更新时间:2023-10-28 11:26:47 26 4
gpt4 key购买 nike

我正在阅读 Hibernate 引用指南的 Id generation 部分和“java persistence with Hibernate”

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

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

我也在寻找引爆点。

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

我想接受有关权衡的教育。

有文献吗?

最佳答案

API Doc对此非常清楚。

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.

如果您正在构建一个并发用户不多的简单应用程序,您可以选择 increment、identity、hilo 等。这些配置简单,不需要在 db 内进行太多编码。

您应该根据您的数据库选择 sequenceguid。这些是安全且更好的,因为 id 生成将发生在数据库内部。

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

关于java - 使用 JPA 和 Hibernate 时如何选择 id 生成策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10041938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com