Last Updated:
Hibernate Envers Error: 'Hibernate_sequence Does Not Exist' – Step-by-Step Solution Guide
Hibernate Envers is a powerful tool for auditing database changes in Java applications, enabling you to track modifications to entities over time. However, like any framework, it can throw cryptic errors that stall development. One such common issue is the "Hibernate_sequence does not exist" error. This error typically arises when Hibernate (or Envers) tries to generate unique identifiers for entities or audit records but cannot find the required database sequence.
In this guide, we’ll demystify this error, explore its root causes, and provide a step-by-step solution to resolve it. Whether you’re new to Hibernate Envers or a seasoned developer, this post will help you diagnose and fix the issue efficiently.
Table of Contents#
Understanding the Error#
The error message "Hibernate_sequence does not exist" indicates that Hibernate (or Hibernate Envers) is attempting to use a database sequence named hibernate_sequence to generate unique identifiers for entities or audit records, but this sequence does not exist in the database.
Why Does Hibernate Need a Sequence?#
Hibernate uses sequences to generate unique IDs for entities when the @GeneratedValue strategy is set to SEQUENCE or AUTO (depending on the database dialect). For example:
- Databases like PostgreSQL, Oracle, or SQL Server use sequences to auto-generate IDs.
- By default, Hibernate uses a global sequence named
hibernate_sequencewhen no explicit sequence is defined (e.g., with@SequenceGenerator).
Envers exacerbates this issue because it creates "audit tables" (e.g., employee_aud for an Employee entity) to track changes. These audit tables also require unique IDs, often relying on the same sequence as the original entity. If hibernate_sequence is missing, Envers cannot generate IDs for audit records, triggering the error.
Common Causes#
Before diving into solutions, let’s identify the most likely reasons this error occurs:
- Incorrect ID Generation Strategy: Using
GenerationType.AUTOwith a sequence-based database (e.g., PostgreSQL) without defining a sequence. - Missing Sequence Auto-Creation: Hibernate’s
hbm2ddl.autosetting (e.g.,validateornone) prevents automatic creation ofhibernate_sequence. - Envers Audit Tables: Envers’ audit tables inherit the ID generation strategy of the original entity, leading to dependency on
hibernate_sequence. - Database Dialect Mismatch: Using a dialect for a database that does not support sequences (e.g., MySQL) with a sequence-based ID strategy.
- Explicit Sequence Misconfiguration: Forgetting to define
@SequenceGeneratorwhen usingGenerationType.SEQUENCE.
Step-by-Step Solutions#
Let’s resolve the error with actionable steps, starting from diagnosis to fixes.
Step 1: Verify Hibernate ID Generation Strategy#
The first step is to check how your entities generate IDs. Open your entity class (e.g., Employee.java) and look for the @GeneratedValue annotation:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO) // Problematic for sequence-based DBs
private Long id;
// Other fields...
}Why This Causes Issues:#
GenerationType.AUTOlets Hibernate choose the best strategy for the database dialect. For sequence-based databases (PostgreSQL, Oracle), it defaults toSEQUENCEand looks forhibernate_sequence.- If
hibernate_sequenceisn’t created, Hibernate throws the error.
Step 2: Configure Hibernate to Auto-Generate the Sequence#
Hibernate can automatically create hibernate_sequence if configured to do so. Check your Hibernate properties (e.g., application.properties for Spring Boot or hibernate.cfg.xml for vanilla Hibernate):
# application.properties (Spring Boot)
spring.jpa.hibernate.ddl-auto=update # or "create" for development
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialectKey Property: hibernate.ddl-auto#
update: Updates the schema, creating missing tables/sequences (safe for development).create: Creates the schema from scratch (use only in development; deletes existing data).validate: Only validates the schema (does NOT create sequences/tables – common culprit!).
Fix: Set hibernate.ddl-auto=update (or create for testing) to let Hibernate auto-generate hibernate_sequence.
Step 3: Explicitly Define a Custom Sequence#
If you want to avoid relying on the global hibernate_sequence, explicitly define a sequence for your entity using @SequenceGenerator:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_seq")
@SequenceGenerator(
name = "employee_seq",
sequenceName = "employee_sequence", // Custom sequence name
allocationSize = 1 // Increment by 1
)
private Long id;
// Other fields...
}Why This Works:#
@SequenceGeneratortells Hibernate to use a sequence namedemployee_sequenceinstead ofhibernate_sequence.- Envers will now use
employee_sequencefor audit records (e.g.,employee_aud), avoiding the missinghibernate_sequenceerror.
Step 4: Adjust Envers-Specific Configuration#
Envers sometimes requires explicit configuration for audit tables. If your audit tables still fail to generate IDs, ensure Envers isn’t overriding sequence behavior.
Envers Audit Table Sequence Fix:#
If your entity uses a custom sequence (Step 3), Envers will inherit it. For global Envers settings, add these properties to application.properties:
# Envers: Use the same sequence as the original entity for audit tables
spring.jpa.properties.org.hibernate.envers.audit_table_suffix=_aud
spring.jpa.properties.org.hibernate.envers.use_revision_entity_with_native_id=trueuse_revision_entity_with_native_id=trueensures Envers uses the native ID strategy (e.g., sequence) of the original entity.
Step 5: Database-Specific Fixes#
Different databases handle ID generation differently. Adjust your strategy based on your database:
For MySQL/MariaDB (Auto-Increment):#
MySQL uses AUTO_INCREMENT instead of sequences. Use GenerationType.IDENTITY to avoid sequence dependency:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // Uses MySQL's AUTO_INCREMENT
private Long id;For PostgreSQL/Oracle (Sequences):#
If using GenerationType.AUTO, force Hibernate to use IDENTITY (not recommended) or explicitly define a sequence (Step 3). Alternatively, enable hibernate.hbm2ddl.auto=update to auto-create hibernate_sequence.
Step 6: Manually Create the Sequence (Last Resort)#
If all else fails, manually create hibernate_sequence in your database. Use this SQL command (adjust for your database):
PostgreSQL:#
CREATE SEQUENCE hibernate_sequence
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;Oracle:#
CREATE SEQUENCE hibernate_sequence
START WITH 1
INCREMENT BY 1
NOCACHE;When to Use This:#
- If you cannot use
hibernate.hbm2ddl.auto=update(e.g., production environments where schema changes are controlled manually). - If
hibernate_sequencewas accidentally dropped or never created.
Verification#
After applying the fixes, verify the error is resolved:
- Check the Database: Confirm
hibernate_sequence(or your custom sequence) exists using a database client (e.g., pgAdmin for PostgreSQL). - Test Entity Persistence: Save a new entity (e.g.,
employeeRepository.save(new Employee())) and ensure no errors are thrown. - Check Audit Tables: Verify Envers creates audit records (e.g.,
SELECT * FROM employee_aud). Theidcolumn should auto-generate correctly.
Conclusion#
The "Hibernate_sequence does not exist" error is a common pitfall when using Hibernate Envers with sequence-based databases. It stems from misconfigured ID generation strategies, missing sequences, or Envers audit table dependencies. By verifying your ID strategy, configuring Hibernate to auto-generate sequences, or explicitly defining custom sequences, you can resolve the error and ensure smooth auditing with Envers.