gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:02:58 25 4
gpt4 key购买 nike

我正在阅读 Hibernate 引用指南的 Id 生成部分和“Hibernate 的 java 持久性”

结合 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.

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

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

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

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

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