简体   繁体   English

为什么大多数hibernate应用程序都使用序列进行id生成?

[英]Why most hibernate applications are using sequence for id generation?

Why most hibernate application are using sequence for id generation? 为什么大多数hibernate应用程序都使用序列进行id生成?

Why not use the default GenerationType=AUTO in @GeneratedValue annotation? 为什么不在@GeneratedValue注释中使用默认的GenerationType = AUTO?

PS In my professional career I see everybody is use sequences, but I don't understand why they bother with harder to deploy solution (there is always sequence create SQL command in deployment instructions). PS在我的职业生涯中,我看到每个人都是使用序列,但我不明白为什么他们更难以部署解决方案(在部署指令中始终有序列创建SQL命令)。

I see several reasons: 我看到几个原因:

  1. The most used database in enterprise apps is probably Oracle, and Oracle doesn't have auto-generated IDs, but sequences. 企业应用程序中使用最多的数据库可能是Oracle,而Oracle没有自动生成的ID,而是序列。
  2. Sequences allows having the ID before inserting a new row, rather than after inserting the new row. 序列允许在插入新行之前使用ID,而不是在插入新行之后。 This is easier to use and more efficient because you can batch insert statements at the end of the transaction but still have IDs definned in the middle of the transaction. 这更容易使用且更有效,因为您可以在事务结束时批量插入语句,但仍然在事务中间定义ID。
  3. Sequences allow using hilo algorithms (which is the default with the hibernate sequence generation), and thus make only one DB call for several inserts, thus increasing performance. 序列允许使用hilo算法(这是hibernate序列生成的默认算法),因此只对几个插入进行一次DB调用,从而提高了性能。
  4. AUTO varies between databases, whereas sequence always uses the same strategy. AUTO在数据库之间变化,而序列始终使用相同的策略。

From the excellent book Pro JPA 2 Mastering Java Persistence API by Mike Keith and Merrick Schincario. 来自Mike Keith和Merrick Schincario的优秀书籍JPA 2 Mastering Java Persistence API

From Chapter 4: Object Relational Mapping, section Identifier Generation. 从第4章:对象关系映射,标识符生成部分。

[...] If an application does not care what kind of generation is used by the provider but wants generation to occur, it can specify a strategy of AUTO. [...]如果应用程序不关心提供程序使用哪种生成但希望生成,则可以指定AUTO策略。

There is a catch to using AUTO, though. 但是,使用AUTO有一个问题。 The provider gets to pick its own strategy to store the identifiers, but it needs to have some kind of persistent resource in order to do so. 提供者可以选择自己的策略来存储标识符,但它需要有某种持久性资源才能这样做。 For example, if it chooses a table-based strategy, it needs to create a table; 例如,如果它选择基于表的策略,则需要创建一个表; if it chooses a sequence-based strategy, it needs to create a sequence. 如果它选择基于序列的策略,则需要创建序列。 The provider can't always rely on the database connection that it obtains from the server to have permissions to create a table in the database. 提供程序不能始终依赖从服务器获取的数据库连接,以获得在数据库中创建表的权限。 This is normally a privileged operation that is often restricted to the DBA. 这通常是特权操作,通常仅限于DBA。 There will need to be some kind of creation phase or schema generation to cause the resource to be created before the AUTO strategy is able to function. 需要某种创建阶段或模式生成,以便在AUTO策略能够运行之前创建资源。

The AUTO mode is really a generation strategy for development or prototyping. AUTO模式实际上是开发或原型设计的一代策略。 It works well as a means of getting you up and running more quickly when the database schema is being generated. 在生成数据库模式时,它可以很好地帮助您快速启动和运行。 In any other situation, it would be better to use one of the other generation strategies discussed in the later sections [...] 在任何其他情况下,最好使用后面章节中讨论的其他一代策略[...]

At least for Oracle: one reason is to be able to track the number of objects in a table (for which the table-specific sequence is good, if no objects are deleted from the table). 至少对于Oracle来说:一个原因是能够跟踪表中的对象数(如果没有从表中删除对象,则特定于表的序列是好的)。 Using GenerationType=AUTO uses a global sequence number, which results in gaps in id numbers when having more than one table in the database. 使用GenerationType = AUTO使用全局序列号,当数据库中有多个表时,会导致id号中出现空白。


There are different considerations for choosing identity generator, the most important ones are performance and portability but also clustering and data migration might be a consideration. 选择身份生成器有不同的考虑因素,最重要的是性能和可移植性,但也可能需要考虑群集和数据迁移。

In practice in the latest Hibernate versions (if not all of them) the SEQUENCE strategy is actually a sequence based HiLo and not a pure sequence as must people assume. 在最新的Hibernate版本(如果不是全部)中,SEQUENCE策略实际上是一个基于序列的HiLo而不是人们所假设的纯序列。

You can read a pretty details post regarding identity generation strategies at my blog: here 您可以在我的博客上阅读有关身份生成策略的相关详细信息: 此处

Eyal 的Eyal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM