The @TableGenerator annotation (jakarta.persistence.TableGenerator) defines a primary key generator that stores its counters in a database table. You reference it from @GeneratedValue(strategy = GenerationType.TABLE, generator = "..."). It is portable across every database, because it needs only plain SQL tables, but it is slower than sequences or identity columns.
Use it when the database has no sequences and you need ids before the insert, or when you must stay database independent. For most new projects on PostgreSQL, Oracle, SQL Server or modern MySQL and MariaDB, a sequence or identity column is the better choice.
The generator keeps one row per sequence name in a small table. Each row holds a name column and a value column. When the persistence provider needs new ids, it reads the current value, adds the allocation size, and writes the new value back in a separate transaction. It then hands out the reserved range from memory.
A typical generator table looks like this:
Because the update happens in its own transaction, ids are never rolled back. Gaps in the id sequence are normal.
The name in @TableGenerator must match the generator in @GeneratedValue. You can also place @TableGenerator on the class, so several entities can share one definition, or reuse one generator table with different pkColumnValue entries.
| Attribute | Meaning | Default |
|---|---|---|
name | Generator name, referenced by @GeneratedValue | Required |
table | Table that stores the counters | Provider specific |
pkColumnName | Column that holds the generator name | Provider specific |
valueColumnName | Column that holds the last allocated value | Provider specific |
pkColumnValue | Row key for this generator | Provider specific |
initialValue | Starting value | 0 |
allocationSize | Ids reserved per database round trip | 50 |
schema, catalog | Location of the table | Connection default |
uniqueConstraints, indexes | Extra DDL hints for the table | None |
allocationSize is the main performance lever. A value of 50 means one database round trip per 50 inserts. If you set it to 1, every insert needs an extra transaction on the generator table. If another application or manual SQL also inserts rows using the same ids, the allocation size must match how that code reserves ids.
A table generator serialises id allocation on one row, normally with a pessimistic lock or an update with a version check. Under heavy concurrent inserts this row becomes a contention point, and it needs a second connection and transaction per allocation.
Compared with other strategies:
| Strategy | Portability | Batch insert friendly | Notes |
|---|---|---|---|
SEQUENCE | Databases with sequences | Yes | Preferred when available |
IDENTITY | Almost all | Limited, id known only after insert | Hibernate cannot batch inserts for it in many setups |
TABLE | All | Yes, with large allocation size | Slowest, uses a separate transaction |
AUTO | All | Depends | Provider chooses; Hibernate 6 usually picks a sequence |
Modern Hibernate versions also ship their own enhanced table generator, and in Hibernate 6 a GenerationType.TABLE mapping is implemented on top of it. Check the generated SQL in your version if you depend on exact column names.
SEQUENCE where the database supports it, and use TABLE only when portability or id pre-allocation without sequences is a hard requirement.allocationSize large enough to avoid a round trip on every insert, and keep it consistent with the real database state.@SequenceGenerator uses a native database sequence, while @TableGenerator simulates one with an ordinary table. The sequence is faster and more scalable, the table works on every database.
Yes. @TableGenerator only defines the generator. The @GeneratedValue annotation with GenerationType.TABLE and a matching generator name activates it.
Ids are reserved in blocks of allocationSize and the reservation is committed in a separate transaction. Unused ids from a restart or a rolled back insert are not reused.
No, it is still part of Jakarta Persistence, but it is rarely recommended because of its performance cost.
@TableGenerator stores id counters in a table so any database can generate keys. Pair it with @GeneratedValue(strategy = GenerationType.TABLE), tune allocationSize, manage the table through migrations, and choose sequences instead when your database offers them.