Hibernate Identifier Mapping

Generator Classes in Hibernate

Learn how Hibernate generates unique identifiers for entities, which strategies are recommended today, and how older XML generator names map to modern identifier generation.

What Is a Hibernate Generator?

An identifier generator supplies the value for an entity's primary-key property when the entity is persisted. In older Hibernate XML mappings, the generator was configured as a child of <id>. In modern applications, JPA annotations such as @GeneratedValue and @SequenceGenerator are usually preferred.

<id name="id" column="id">
  <generator class="identity"/>
</id>
Important: generator names and behavior differ between Hibernate generations. Strategies such as hilo, seqhilo, guid, and sequence-identity mainly belong to older Hibernate documentation. Choose a strategy supported by your Hibernate version and database dialect.

Recommended Modern Strategies

StrategyTypical useExample
IDENTITYDatabase identity or auto-increment column.@GeneratedValue(strategy = GenerationType.IDENTITY)
SEQUENCEDatabases that provide sequences, such as PostgreSQL or Oracle.@GeneratedValue(strategy = GenerationType.SEQUENCE)
AUTOLets the persistence provider choose a suitable strategy.@GeneratedValue(strategy = GenerationType.AUTO)
UUIDDistributed identifiers without a central numeric sequence.@UuidGenerator or application-generated UUID

Identity Generation

With identity generation, the database assigns a value when it inserts the row. It is common with MySQL auto-increment and SQL Server identity columns.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

The generated value is available after the insert is executed. Because the database must insert the row before returning the key, identity generation can limit some batching optimizations.

Sequence Generation

A database sequence produces numeric values independently of table inserts. This is a strong choice when the database supports sequences and batching is important.

@Id
@SequenceGenerator(
    name = "employee_sequence",
    sequenceName = "employee_seq",
    allocationSize = 50
)
@GeneratedValue(
    strategy = GenerationType.SEQUENCE,
    generator = "employee_sequence"
)
private Long id;

allocationSize controls how many values Hibernate can reserve at a time. Keep it aligned with the database sequence increment when configuring pooled allocation.

The equivalent legacy XML mapping is:

<id name="id">
  <generator class="sequence">
    <param name="sequence_name">employee_seq</param>
  </generator>
</id>

UUID Generation

UUIDs are useful when identifiers must be created across services or before a database insert. They avoid a single numeric sequence but are larger than a typical integer key and can affect index locality.

import java.util.UUID;
import org.hibernate.annotations.UuidGenerator;

@Id
@UuidGenerator
private UUID id;

Choose a UUID storage type deliberately. A native UUID column or binary representation is generally more compact than storing the value as a long text string.

Legacy Hibernate Generator Classes

assigned

The application supplies the identifier before calling persist(). It is useful for stable business keys but requires uniqueness and validation in application code.

increment

Hibernate reads the current maximum identifier and increments it. It is unsafe for concurrent applications and should not be used for production key generation.

sequence

Uses a database sequence. Prefer the JPA SEQUENCE strategy with an explicit sequence generator in new code.

native

Older Hibernate strategy that chooses identity, sequence, or another approach based on the dialect. Prefer an explicit strategy when portability and schema behavior matter.

identity

Uses a database identity or auto-increment column. In annotation mapping, use GenerationType.IDENTITY.

uuid

Generates a UUID-style identifier. Modern Hibernate applications should use an explicit UUID property and the current UUID generator support.

hilo and seqhilo

Use a high/low allocation algorithm. These are legacy strategies; sequence-based pooled optimizers are normally clearer for new applications.

guid, select, foreign

Database GUIDs, trigger-returned keys, and identifiers borrowed from an associated object. They are specialized legacy mappings and need database-specific or association-specific design.

Custom Identifier Generators

When built-in strategies do not meet an application's requirements, Hibernate can use a custom generator. The implementation must follow the generator SPI for the Hibernate version in use.

public class BusinessKeyGenerator
        implements IdentifierGenerator {
    @Override
    public Object generate(
            SharedSessionContractImplementor session,
            Object entity) {
        return "EMP-" + UUID.randomUUID();
    }
}

Custom generators should be collision-resistant, testable, and compatible with the database column type. Avoid using timestamps or table maximum values as a uniqueness guarantee in concurrent systems.

How to Choose a Strategy

  1. Use IDENTITY when the schema is built around an auto-increment or identity column.
  2. Use SEQUENCE when the database supports sequences and you want efficient allocation and batching.
  3. Use UUIDs when identifiers must be generated independently across services or before persistence.
  4. Use assigned identifiers only when the application owns a stable, unique business key.
  5. Avoid increment for concurrent production workloads.
  6. Do not change an entity's identifier after it has been placed in a HashSet or used as a HashMap key.
Rule of thumb: make the identifier strategy explicit, match it to the database schema, and test inserts, batching, rollback, and concurrent writes before production deployment.