Red Hat Training

A Red Hat training course is available for Red Hat JBoss Web Server

25.4. Identifier Generation

When considering portability between databases, another important decision is selecting the identifier generation strategy you want to use. Originally Hibernate provided the native generator for this purpose, which was intended to select between a sequence, identity, or table strategy depending on the capability of the underlying database. However, an insidious implication of this approach comes about when targeting some databases which support identity generation and some which do not. identity generation relies on the SQL definition of an IDENTITY (or auto-increment) column to manage the identifier value; it is what is known as a post-insert generation strategy because the insert must actually happen before we can know the identifier value. Because Hibernate relies on this identifier value to uniquely reference entities within a persistence context it must then issue the insert immediately when the users requests the entity be associated with the session (like via save() e.g.) regardless of current transactional semantics. The underlying issue is that the semantics of the application itself changes in these cases.

Note

Hibernate has been improved so that the insert is delayed in cases where that is feasible.
Starting with version 3.2.3, Hibernate comes with a set of enhanced identifier generators targeting portability in a much different way.
There are specifically two bundled enhanced generators:
  • org.hibernate.id.enhanced.SequenceStyleGenerator
  • org.hibernate.id.enhanced.TableGenerator
See Section 5.1.13, “Enhanced Identifier Generators” for a more detailed description of these generators.