ID Generation Strategies : Hibernate

Author Avatar
Informative page
5 min read •

ID Generation Strategies

Extremely Important: Understanding @GeneratedValue, IDENTITY vs SEQUENCE, and Database Mechanics

1. What is @GeneratedValue & Why do we need it?

When we create an entity, it needs a unique ID. Instead of manually calculating max(id) + 1 in our application code, we use @GeneratedValue. This tells JPA/Hibernate that the entity ID should be generated automatically according to a configured strategy.

@Entity public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; }

The Concurrency Problem:
If you manually set IDs (e.g., count() + 1), two concurrent threads might both see a max ID of 3, both choose 4, and both try to insert ID 4, causing a database crash. ID-generation mechanisms solve this using native database safeguards.

2. GenerationType.IDENTITY

Meaning: The database generates the ID when the row is inserted. Commonly maps to AUTO_INCREMENT in MySQL.

Java (persist) ↓ Hibernate executes INSERT ↓ Database generates ID (e.g., 101) ↓ Hibernate obtains the generated ID

The T3 Implication: Because the DB generates the ID during the insert, Hibernate must execute the INSERT statement immediately before it can know the identifier. This limits Hibernate's ability to delay and batch insert statements efficiently.

3. GenerationType.SEQUENCE

Meaning: A database sequence (a DB object that generates numbers) generates the ID before the row is inserted. Common in PostgreSQL and Oracle.

Java (persist) ↓ Hibernate asks DB Sequence for next value ↓ Sequence returns ID (e.g., 101) ↓ Hibernate knows ID beforehand ↓ Hibernate executes INSERT WITH the ID

Why is SEQUENCE powerful? Because Hibernate obtains the identifier before the INSERT, it gains immense flexibility. It can reserve a block of IDs, delay the actual INSERT statements, and execute them in a highly optimized JDBC batch at flush time.

4. AUTO & TABLE Strategies

GenerationType.AUTO:
The persistence provider (Hibernate) chooses the ID generation strategy based on the database capabilities. It makes mapping database-agnostic, but production teams often avoid it in favor of explicit control over schema behavior.

GenerationType.TABLE:
JPA uses a dedicated database table as an ID generator (e.g., a table with generator_name and next_value columns). While highly portable, it is less common today because reading and updating a table creates extra database operations and locking contention compared to native sequences.

5. The Ultimate Comparison

Strategy Who generates ID? ID available before INSERT? Common Use Case
AUTO Provider chooses Depends on chosen strategy General/default choice
IDENTITY Database identity column No MySQL, SQL Server
SEQUENCE Database Sequence Yes PostgreSQL, Oracle
TABLE JPA (using a DB table) Yes Legacy/Highly portable needs

6. Rapid-Fire T3 Interview Questions

Most Important Q: What is the exact difference between IDENTITY and SEQUENCE?

With IDENTITY, the database generates the ID as part of the INSERT operation. Hibernate cannot know the ID until the insert occurs.

With SEQUENCE, Hibernate obtains the next ID from a database sequence before the INSERT. This difference drastically affects Hibernate's ability to batch and order inserts efficiently for performance.

Q: Does @GeneratedValue itself generate the ID?

No. @GeneratedValue is merely metadata telling JPA/Hibernate how the ID should be generated. The actual generator is the native Database feature (Identity/Sequence) or a Provider mechanism.

The Mental Model (Memorize this)

That "when does Hibernate know the ID?" question is the easiest way to remember the fundamental difference:

IDENTITY INSERT first → Database generates ID SEQUENCE Get ID first → INSERT with ID

Comments (0)