Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Hibernate cannot find the sequence hibernate_sequence on PostgreSQL

ErrorMissing sequence or table: hibernate_sequence

Short answer

Hibernate 6 no longer uses one shared hibernate_sequence. It expects one sequence per entity, named after the entity, so an older schema fails validation or insert. Create the sequences, keep the old behaviour with a naming-strategy setting, or switch the entity to IDENTITY. Read the exact name in the error.

  • Applies to Hibernate ORM 6.6, Spring Boot 3.4, PostgreSQL 17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the sequences in the database and compare them with the name in the error.

    psql -d mydb -c '\ds'
  2. 2Match what you see, then jump to its fix

    1. 1The upgraded app asks for a name like person_seq, or fails validation, while the database only has hibernate_sequenceHibernate 6 expects one sequence per entity, but the schema has the old shared oneFix
    2. 2The table has an auto-increment (serial or identity) id column and no sequence exists for itThe entity uses IDENTITY in the database but the mapping asks for a sequenceFix
    3. 3validate or inserts fail on a PostgreSQL database that the app did not createNo schema manager created the sequenceFix

What does the error look like?

  • Missing sequence or table: hibernate_sequence
  • ERROR: relation "hibernate_sequence" does not exist

Cause 1 of 3

Hibernate 6 expects one sequence per entity, but the schema has the old shared one

1Check

The Hibernate 6.0 migration guide says implicit sequences are now one per entity hierarchy, named <entity name>_seq (Person uses person_seq), with an allocation size of 50. The shared hibernate_sequence is no longer the default.

grep -rn "GeneratedValue" src/main/java
psql -d mydb -c '\ds'

2Fix

Either keep the old naming with the setting from the migration guide, or create the new sequences. The second option also needs the allocation size to match: 50 by default.

# keep the old behaviour
spring.jpa.properties.hibernate.id.db_structure_naming_strategy=legacy

-- or create the per-entity sequence (increment 50)
CREATE SEQUENCE person_seq START WITH 1 INCREMENT BY 50;

3Verify

Insert a row through the app and read the new id.

psql -d mydb -c 'SELECT id FROM person ORDER BY id DESC LIMIT 3'

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

The entity uses IDENTITY in the database but the mapping asks for a sequence

1Check

With GenerationType.AUTO on a numeric id, Hibernate uses a sequence-style generator. IDENTITY is the strategy that lets the database generate the id column and does not use a sequence object of Hibernate's.

grep -rn -B1 "private Long id" src/main/java

2Fix

Switch the id mapping to IDENTITY. Hibernate then cannot batch inserts for that entity, which is the price of this strategy.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

3Verify

Save a new entity; the id comes back from the database.

repository.save(new Person());

Confirmed by docs.hibernate.org

Did this fix it?

Cause 3 of 3

No schema manager created the sequence

1Check

For a database that is not embedded, spring.jpa.hibernate.ddl-auto defaults to none unless Flyway or Liquibase manages the schema, so Spring Boot creates nothing.

grep -rn "ddl-auto\|flyway" src/main/resources/ pom.xml

2Fix

Create the sequence in a migration, with the increment that matches the mapping.

-- db/migration/V2__person_sequence.sql
CREATE SEQUENCE IF NOT EXISTS person_seq START WITH 1 INCREMENT BY 50;

3Verify

Restart the application and list the sequences.

psql -d mydb -c '\ds'

Confirmed by docs.spring.io

Did this fix it?

How do I stop it coming back?

  • Name the generator on each entity with @SequenceGenerator, so the sequence name does not depend on a Hibernate default that changes between versions.
  • Read the Hibernate migration guide before you raise the major version, and test inserts against a copy of the production schema.
  • Keep sequences in migrations, with the same increment as the mapping's allocation size.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.