Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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

like image 262
IAdapter Avatar asked Apr 15 '11 12:04

IAdapter


3 Answers

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.
like image 163
JB Nizet Avatar answered Sep 19 '22 17:09

JB Nizet


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

like image 25
Edwin Dalorzo Avatar answered Sep 18 '22 17:09

Edwin Dalorzo


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.

like image 40
simon Avatar answered Sep 18 '22 17:09

simon