Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How to Implement Sequence-Based Primary Keys in JPA

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 4, 2024
UpdatedNov 16, 2026
5 min read

In JPA you generate primary keys from a database sequence by marking the id field with @Id and @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "name"), and declaring the generator with @SequenceGenerator. The persistence provider reads the next value from the sequence before inserting, which works well for batching and for databases such as PostgreSQL and Oracle.

The annotations come from Jakarta Persistence (the jakarta.persistence package since Jakarta EE 9). Older projects on javax.persistence use identical annotations with a different package name.

Table of Contents

  • What a sequence-based primary key is
  • Declaring the generator with annotations
  • Using the entity
  • Choosing allocationSize and avoiding duplicate keys
  • Default sequence names and schema management
  • Testing the behaviour
  • Frequently Asked Questions
    • What is the difference between GenerationType.AUTO and SEQUENCE?
    • Can two entities share a sequence?
    • Why do IDs jump by 50 between application restarts?
    • Does this work in MySQL?
  • Summary

What a sequence-based primary key is

A sequence is a database object that produces increasing numbers, independent of any table. JPA lets an entity borrow those numbers as its identifier. Compared with IDENTITY columns, the ID is available before the INSERT, so the provider can queue and batch writes and you can reference a new entity's ID before flushing.

StrategyID known before insertInsert batchingTypical databases
SEQUENCEYesYesPostgreSQL, Oracle, SQL Server, DB2, H2
IDENTITYNoLimitedMySQL, SQL Server
TABLEYesYes, but slowAny
AUTOProvider decidesDependsAny

Declaring the generator with annotations

java

The attributes of @SequenceGenerator are:

  • name: the label that @GeneratedValue(generator = ...) refers to.
  • sequenceName: the real sequence in the database.
  • initialValue: the first number the sequence returns.
  • allocationSize: how many IDs the provider reserves per database call. The default is 50.
  • catalog and schema: optional qualifiers for the sequence location.

Create the sequence with an increment that matches allocationSize:

sql

Using the entity

java

With Spring Data JPA the same entity works with a JpaRepository<Product, Long> and save(...). After persist, getId() is populated even though the transaction has not yet flushed the insert.

Choosing allocationSize and avoiding duplicate keys

The provider calls nextval once and then hands out allocationSize IDs from memory. That makes bulk inserts fast: 1,000 rows need roughly 20 sequence calls with the default of 50.

Follow these rules:

  1. Keep INCREMENT BY in the database equal to allocationSize. A mismatch is the classic cause of duplicate key errors or startup validation failures.
  2. Do not expect consecutive numbers. Rolled-back transactions and application restarts leave gaps.
  3. If other applications insert with their own nextval calls, give them the same increment or a separate sequence.
  4. Use allocationSize = 1 only when you must have one round trip per ID, for example with legacy integrations.

Default sequence names and schema management

If you write only @GeneratedValue(strategy = GenerationType.SEQUENCE), the provider chooses a sequence name. Hibernate 6 uses <EntityName>_SEQ with an increment of 50. Because names differ between providers, declare the generator explicitly and create the sequence in a Flyway or Liquibase migration. Let Hibernate only validate the schema:

properties

Testing the behaviour

Persist a batch and look at the SQL:

properties
text

You should see one nextval for each block of 50 entities, followed by batched inserts.

Frequently Asked Questions

What is the difference between GenerationType.AUTO and SEQUENCE?

AUTO lets the provider pick a strategy for the database and Hibernate version. SEQUENCE states it explicitly, which is clearer and portable across upgrades.

Can two entities share a sequence?

Yes. Use the same generator name and sequence in both @SequenceGenerator declarations or on a @MappedSuperclass. Dedicated sequences reduce contention on busy tables.

Why do IDs jump by 50 between application restarts?

The provider reserves a whole block per nextval call and discards the unused part when the application stops. That is expected and harmless.

Does this work in MySQL?

MySQL has no sequences, so use IDENTITY. MariaDB 10.3 and later supports them.

Summary

Annotate the key with @Id, @GeneratedValue(strategy = GenerationType.SEQUENCE) and @SequenceGenerator, keep allocationSize equal to the sequence increment, create the sequence in migrations, and accept gaps. You get early IDs and efficient batching.

Share