Resolving hibernate_sequence Doesn't Exist Error in Hibernate 5 Upgrade with Generator Mapping Configuration

Dec 07, 2025 · Programming · 9 views · 7.8

Keywords: Hibernate upgrade | ID generator | Sequence table error

Abstract: This article provides an in-depth analysis of the "hibernate_sequence doesn't exist" error encountered during migration from Hibernate 4 to 5. The error stems from Hibernate 5's default activation of new ID generator mappings, causing the system to attempt accessing non-existent sequence tables. The paper examines the mechanism of the hibernate.id.new_generator_mappings property, compares ID generation strategies across different databases, and offers configuration solutions for Spring Boot environments. Through code examples and configuration explanations, it helps developers understand the underlying principles of Hibernate ID generators, ensuring smooth upgrade processes.

Problem Background and Error Analysis

During the migration from Hibernate version 4 to 5, many developers encounter a typical error: Table 'test.hibernate_sequence' doesn't exist. This error usually appears when performing data update operations, persisting even after setting the entity class's ID generation strategy to @GeneratedValue(strategy=GenerationType.AUTO).

Changes in Hibernate ID Generator Mapping Mechanism

Hibernate 5 introduces a significant configuration change: the new ID generator mapping mechanism is enabled by default. This change is controlled by the hibernate.id.new_generator_mappings property. In Hibernate 4.x versions, this property defaults to false, while in Hibernate 5.x, the default value changes to true.

When hibernate.id.new_generator_mappings=true, Hibernate adopts ID generation strategies compliant with JPA 2.0 specifications. For entities using the GenerationType.AUTO strategy, Hibernate selects appropriate generation methods based on database type:

// Entity class example
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    // Other properties and methods
}

For databases like MySQL that support auto-increment fields, Hibernate expects to use the AUTO_INCREMENT mechanism. However, under certain configurations, particularly when Hibernate misjudges the database type or has incomplete configuration, it may attempt to access a sequence table named hibernate_sequence, which doesn't exist in most MySQL databases.

Solution: Configuring hibernate.id.new_generator_mappings

The core solution to this problem is correctly configuring the hibernate.id.new_generator_mappings property. Configuration methods vary depending on the application framework:

Traditional Hibernate Configuration

In Hibernate's XML configuration file, add the following property:

<!-- For Hibernate 5.x -->
<property name="hibernate.id.new_generator_mappings">false</property>

<!-- For Hibernate 4.x -->
<prop key="hibernate.id.new_generator_mappings">false</prop>

Spring Boot Environment Configuration

In Spring Boot applications, configuration can be done through application.properties or application.yml files:

# In application.properties
spring.jpa.properties.hibernate.id.new_generator_mappings=false

Or using YAML format:

# In application.yml
spring:
  jpa:
    properties:
      hibernate:
        id:
          new_generator_mappings: false

Alternative Solution: Using GenerationType.IDENTITY

Besides modifying Hibernate configuration, another solution is to directly change the entity class's ID generation strategy. Changing from GenerationType.AUTO to GenerationType.IDENTITY allows the database to completely manage primary key generation:

@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    // This approach is particularly suitable for MySQL databases,
    // as it directly utilizes the database's AUTO_INCREMENT functionality
}

The advantage of this method is that it doesn't rely on Hibernate's sequence table mechanism, instead using the database's native auto-increment functionality. In MySQL, the corresponding table definition should include:

CREATE TABLE product (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    -- Other fields
);

Relationship Between Database Types and ID Generation Strategies

Understanding ID generation mechanisms across different databases is crucial for solving such problems. Hibernate's GenerationType.AUTO strategy automatically selects the most appropriate ID generation method based on the underlying database:

When Hibernate is configured to use new generator mappings, for databases supporting sequences, it attempts to find a default sequence named hibernate_sequence. If this sequence doesn't exist, it throws the error discussed in this article.

Practical Recommendations and Considerations

When upgrading Hibernate versions, the following steps are recommended:

  1. Test Environment First: Perform upgrades in test environments first, verifying all data operation functionalities
  2. Check Configuration Compatibility: Ensure all Hibernate configurations are compatible with the new version
  3. Prepare Database Scripts: Prepare corresponding DDL scripts if sequence tables need to be created
  4. Rollback Plan: Establish a clear problem rollback strategy

For production environments, using the GenerationType.IDENTITY strategy is more recommended because it:

  1. Reduces dependency on Hibernate-specific mechanisms
  2. Improves compatibility with different databases
  3. Simplifies database migration processes

By properly understanding Hibernate's ID generation mechanisms and appropriately configuring relevant properties, developers can successfully resolve sequence table non-existence issues during upgrades, ensuring stable application operation.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.