简体   繁体   中英

Why most hibernate applications are using sequence for id generation?

Why most hibernate application are using sequence for id generation?

Why not use the default GenerationType=AUTO in @GeneratedValue annotation?

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).

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.
  2. Sequences allows having the ID before inserting a new row, rather than after inserting the new row. 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.
  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.
  4. AUTO varies between databases, whereas sequence always uses the same strategy.

From the excellent book Pro JPA 2 Mastering Java Persistence API by Mike Keith and Merrick Schincario.

From Chapter 4: Object Relational Mapping, section Identifier Generation.

[...] 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.

There is a catch to using AUTO, though. 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. 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.

The AUTO mode is really a generation strategy for development or prototyping. 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). Using GenerationType=AUTO uses a global sequence number, which results in gaps in id numbers when having more than one table in the database.


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.

You can read a pretty details post regarding identity generation strategies at my blog: here

Eyal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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