Why most hibernate application are utilizing sequence for id generation?

Why don't you make use of the default GenerationType=AUTO in @GeneratedValue annotation?

P.S. During my professional career I see everyone is use sequences, however i do not understand why they make use of harder to deploy solution (there's always sequence create SQL command in deployment instructions).

In 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 the application doesn't care what type of generation can be used through the provider but wants generation to occur, it may specify a method of AUTO.

There's a catch to presenting AUTO, though. The provider reaches pick its own technique to keep identifiers, but it must possess some type of persistent resource to be able to achieve this. For instance, whether it selects a table-based strategy, it must produce a table whether it selects a sequence-based strategy, it must produce a sequence. The provider can’t always depend around the database connection it acquires in the server to have permissions to produce a table in the database. This really is ordinarily a fortunate operation that's frequently limited to the DBA. There'll need to become some type of creation phase or schema generation to result in the resource to become produced prior to the AUTO strategy has the capacity to function.

The Car mode can be a generation technique for development or prototyping. It really works well as a way of having you ready to go more rapidly once the database schema is being produced. In almost any other situation, it might be easier to use among the other generation methods talked about within the later sections [...]

several reasons:

  1. The most famous database in enterprise applications is most likely Oracle, and Oracle does not have auto-produced IDs, but sequences.
  2. Sequences enables getting the ID before placing a brand new row, instead of after placing the brand new row. This really is simpler to make use of and much more efficient since you can batch place claims in the finish from the transaction but nonetheless have IDs definned in the center of the transaction.
  3. Sequences allow using hilo calculations (the default using the hibernate sequence generation), and therefore make just one DB demand several card inserts, thus growing performance.
  4. AUTO varies between databases, whereas sequence always uses exactly the same strategy.

A minimum of for Oracle: one good reason would be to have the ability to track the amount of objects inside a table (that the table-specific sequence is nice, if no objects are erased in the table). Using GenerationType=AUTO utilizes a global sequence number, which leads to gaps in id amounts when getting several table within the database.


You will find different factors for selecting identity generator, the most crucial ones are performance and portability but additionally clustering and data migration may well be a consideration.

Used within the latest Hibernate versions (if not completely of these) the succession technique is really a sequence based HiLo and never a pure sequence as must people assume.

Read quite a particulars publish regarding identity generation methods inside my blog: here

Eyal