Quick revision
The short answer
IDENTITY uses an auto-increment column, so Hibernate must run each INSERT immediately to learn the id, which disables JDBC insert batching. SEQUENCE takes ids from a database sequence, and with the default pooled optimizer and allocationSize of 50 Hibernate gets a block of ids per round trip and can batch inserts. UUID, available as GenerationType.UUID since Jakarta Persistence 3.1, creates ids in the application without any round trip, but random UUIDs scatter inserts across B-tree indexes, which time-ordered UUIDs such as version 7 avoid.
What to remember
In this answer, we saw that IDENTITY gets its id from the INSERT itself, so Hibernate inserts at persist() and cannot batch. SEQUENCE reserves blocks of ids with the pooled optimizer and an allocationSize of 50, which allows batching and makes it the best default for numeric ids. UUID creates ids in the application without round trips, but random UUIDs fragment B-tree indexes, so time-ordered UUIDs such as version 7 are preferable, and AUTO in Hibernate 6 prefers sequences where the database supports them.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
IDENTITY uses an auto-increment column, so Hibernate must run each INSERT immediately to learn the id, which disables JDBC insert batching. SEQUENCE takes ids from a database sequence, and with the default pooled optimizer and allocationSize of 50 Hibernate gets a block of ids per round trip and can batch inserts. UUID, available as GenerationType.UUID since Jakarta Persistence 3.1, creates ids in the application without any round trip, but random UUIDs scatter inserts across B-tree indexes, which time-ordered UUIDs such as version 7 avoid.
Why this question is asked
The id strategy is chosen once and lives as long as the table, and it decides how fast inserts can be, how indexes behave as tables grow, and how easily data moves between systems. Interviewers ask this to see whether we know what each strategy costs, not just how to write the annotation. In this answer, we will recap what an id generator does, look at IDENTITY, SEQUENCE and UUID one at a time with the SQL they cause, compare them side by side, and see what AUTO picks in Hibernate 6.
1A Quick Recap of Id Generation
Most entities use a surrogate primary key, a value with no business meaning whose only job is to identify the row. @GeneratedValue tells JPA that the value is created for us, and the strategy decides who creates it: the database during the insert, a database sequence before the insert, or the application itself.
The question that separates the strategies is simple: when does Hibernate know the id? Hibernate needs it as soon as we call persist(), because the persistence context is a map keyed by id. If the id only exists after the INSERT, Hibernate has to run the INSERT right away.
2IDENTITY: The Database Assigns the Id During the Insert
With GenerationType.IDENTITY, the column is auto-increment (IDENTITY in PostgreSQL and SQL Server, AUTO_INCREMENT in MySQL). The database assigns the value as part of the INSERT and returns it.
Notice that each persist() runs its INSERT immediately, before the flush. The reason is that Hibernate cannot learn the id any other way. So Hibernate disables JDBC batching for inserts of IDENTITY entities, and inserting 10,000 rows means 10,000 round trips. IDENTITY is simple and fine for low write volume, but it is the slowest option for bulk inserts.
3SEQUENCE: Ids Reserved in Blocks
With GenerationType.SEQUENCE, Hibernate asks a database sequence for values before inserting. A sequence is a database object that hands out increasing numbers and never gives the same number twice, even to concurrent sessions. Hibernate's default pooled optimizer adds an important trick: it treats each sequence value as a reservation of a block of allocationSize ids, which is 50 by default. The sequence must therefore be created with INCREMENT BY 50 to match.
So 100 inserts cost two sequence calls and two batched round trips instead of 100 round trips. Because the id is known at persist(), Hibernate can wait until flush and batch the inserts. This makes SEQUENCE the best general choice for numeric ids on databases that support sequences, such as PostgreSQL and Oracle. Ids can have gaps, for example when an application restarts with half a block unused, which is harmless for surrogate keys.
4UUID: The Application Creates the Id
GenerationType.UUID, added in Jakarta Persistence 3.1, makes the provider generate a java.util.UUID in the application. No database round trip is needed, ids are unique across databases and services, and batching works. In PostgreSQL we store it in the native uuid type, 16 bytes, instead of a 36-character string.
| Aspect | IDENTITY | SEQUENCE (pooled, 50) | UUID |
|---|---|---|---|
| Who creates the id | database, during insert | database sequence, before insert | application |
| Round trips for 1,000 inserts | 1,000 | 20 sequence calls + batches | batches only |
| JDBC insert batching | disabled | yes | yes |
| Key size | 8 bytes (bigint) | 8 bytes (bigint) | 16 bytes |
| Index locality | appends at the end | appends at the end | random for v4, good for v7 |
| Unique across databases | no | no | yes |
But there is a catch: random version 4 UUIDs are spread evenly over the whole value range. In a B-tree index, every insert lands on a random page, so the database keeps loading and splitting pages all over the index instead of appending at the right edge. Large tables then suffer from more I/O, half-empty pages and a bigger index. Time-ordered UUIDs, such as version 7, which begins with a millisecond timestamp, keep new values close together and avoid most of that fragmentation. Hibernate offers time-based styles in @UuidGenerator; check your version for which UUID versions it can produce. The other cost is size: 16 bytes per key instead of 8 for a bigint, in the primary key and in every foreign key and index that refers to it.
5What AUTO Chooses in Hibernate 6
GenerationType.AUTO, the default when we write @GeneratedValue without a strategy, lets the provider decide. For numeric ids, Hibernate 6 uses a sequence where the database supports sequences, by default one sequence per entity named after the entity with a _SEQ suffix and an increment of 50, and falls back to a table-based generator where sequences are missing. For a UUID-typed id, AUTO generates UUIDs. Because the default naming and fallback rules have changed between major versions, it is safer to declare the strategy and the sequence explicitly, so a Hibernate upgrade cannot change our ids.
6Follow-up questions
Can we use SEQUENCE on MySQL?
MySQL 8.4 has no sequence objects, so the practical choices there are IDENTITY, with its batching limit, or application-generated ids such as time-ordered UUIDs. Hibernate's table generator emulates a sequence with a table, but it needs row locks on that table and is rarely worth it.
Why must the sequence increment match allocationSize?
Because the pooled optimizer reads each sequence value as a reservation of allocationSize ids. If the sequence increments by 1 while allocationSize is 50, consecutive values reserve overlapping blocks and different instances generate the same ids, which shows up as duplicate key errors.
Is a UUID primary key a good idea for every table?
Not automatically. It is a good fit when ids are created in many places or must be unique across systems. For a single database with heavy inserts and large indexes, a sequence-based bigint is smaller and simpler, and we can still expose a separate random public id where needed.
7Conclusion
In this answer, we saw that IDENTITY gets its id from the INSERT itself, so Hibernate inserts at persist() and cannot batch. SEQUENCE reserves blocks of ids with the pooled optimizer and an allocationSize of 50, which allows batching and makes it the best default for numeric ids. UUID creates ids in the application without round trips, but random UUIDs fragment B-tree indexes, so time-ordered UUIDs such as version 7 are preferable, and AUTO in Hibernate 6 prefers sequences where the database supports them.
Open with
The three differ mainly in when Hibernate learns the id. With IDENTITY the database assigns it during the insert, so Hibernate has to execute the insert immediately at persist to get it back.
Then, if they want more
That means JDBC insert batching is disabled for those entities, and ten thousand inserts are ten thousand round trips.
With SEQUENCE Hibernate gets the id from a database sequence before the insert, and with the default pooled optimizer and an allocation size of 50, one nextval call reserves a block of 50 ids, so inserts can wait until flush and go out in batches. The sequence must be created with increment by 50 to match.
That is my default for numeric keys on PostgreSQL or Oracle. With UUID, GenerationType.UUID since Jakarta Persistence 3.1, the application generates the id, so there is no round trip and the id is unique across databases, which helps for distributed systems.
The downsides are 16 bytes instead of 8 in every key and index, and with random version 4 UUIDs, inserts land all over the B-tree index, causing page splits and poor cache use. Time-ordered UUIDs like version 7 avoid most of that, and Hibernate has time-based styles in UuidGenerator, depending on the version.
If you leave it on AUTO, Hibernate 6 picks a sequence where the database supports it, but I prefer to declare the strategy explicitly.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6, MySQL 8.4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare GenerationType.IDENTITY, SEQUENCE and UUID for primary keys in JPA with Hibernate.": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/c9788b7a7c/