The @SequenceGenerator annotation configures a database sequence that JPA and Hibernate use to produce primary key values. You pair it with @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "..."), and it controls the sequence name, the starting value and, most importantly, the allocation size that decides how many IDs are reserved per database round trip.
It is defined in jakarta.persistence (formerly javax.persistence before Jakarta EE 9 and Spring Boot 3). It only makes sense on databases that have sequences, such as PostgreSQL, Oracle, SQL Server, MariaDB 10.3+ and H2.
The name attribute is the generator name that @GeneratedValue refers to. The sequenceName is the real sequence in the database. The matching DDL on PostgreSQL is:
| Attribute | Meaning | Default |
|---|---|---|
name | Name referenced by @GeneratedValue(generator = ...). Required. | none |
sequenceName | Name of the database sequence | provider chosen |
initialValue | First value used by the sequence | 1 |
allocationSize | Number of IDs reserved per database call | 50 |
catalog | Catalog of the sequence | default catalog |
schema | Schema of the sequence | default schema |
Recent Hibernate and JPA releases also allow the generator to be declared once at class or package level, so several entities can share it.
With allocationSize = 50, Hibernate reads the sequence once, then hands out 50 identifiers from memory before asking the database again. This is the pooled optimiser: the sequence value is the upper end of a block, and the sequence must increment by the same amount.
This is the usual source of production bugs:
INCREMENT BY 1 but the entity declares allocationSize = 50, Hibernate raises a mismatch error on startup (when schema validation is on) or produces duplicate or skipped keys.allocationSize = 1, every insert triggers a sequence call. IDs are gap free in the sense of one-per-call, but you lose batching performance.Gaps in identifiers are normal and harmless. Do not rely on a sequence for gap-free numbering such as invoice numbers.
| Strategy | Works with | Batch inserts | Notes |
|---|---|---|---|
SEQUENCE | PostgreSQL, Oracle, SQL Server, MariaDB, H2 | Yes | Best choice where sequences exist; ID known before the insert |
IDENTITY | Most databases, including MySQL | Limited | Hibernate must insert immediately to learn the ID, which disables JDBC insert batching |
UUID | All | Yes | Needs no database round trip, larger keys |
AUTO | All | Depends | Provider picks a strategy; in Hibernate 6 this is sequence based when the dialect supports it |
MySQL has no sequences, so @SequenceGenerator is not applicable there; use IDENTITY or a UUID.
In Hibernate 6 and later, @GeneratedValue(strategy = GenerationType.SEQUENCE) with no generator uses a sequence named after the entity, for example Invoice_SEQ, with an increment of 50. Hibernate 5 used one shared hibernate_sequence. When you upgrade across that boundary, either rename and resize the sequences in a migration or declare @SequenceGenerator explicitly so the mapping no longer depends on defaults.
Keep schema changes in a migration tool such as Flyway or Liquibase and use spring.jpa.hibernate.ddl-auto=validate in production, so a mismatch between mapping and database fails fast.
INCREMENT BY equal to allocationSize.Long ID fields so the range never becomes a problem.No. Without it the provider picks a default sequence and allocation size. Declaring it makes the sequence name and increment explicit, which is safer across Hibernate versions.
Each application instance reserves a block of allocationSize IDs. Restarts or multiple instances leave gaps. This is expected and does not affect correctness.
Hibernate may fail schema validation, or generate IDs that overlap with values from another writer. Always align the two.
Not on MySQL, which lacks sequences. MariaDB 10.3 and later do support them.
@SequenceGenerator names a database sequence and defines how Hibernate reserves ID blocks from it. Match allocationSize to the sequence increment, create sequences through migrations, declare the generator explicitly so upgrades do not change behaviour, and choose IDENTITY or UUID on databases without sequences. Sequences let Hibernate batch inserts, which is the main reason to prefer them where available.