Resolve Spring Boot JPA Table ‘dbname.hibernate_sequence’ Doesn’t Exist Error

In my recent spring boot example project, i use spring boot data JPA to operate MySQL server table. And when i invoke the CrudRepository’s save method to implement insert action to the table, it shows below error, the error message is “table ‘dev2qa_example.hibernate_sequence’ doesn’t exist”. This error cost me some time to fix, and i will record the fix method here to help other who maybe encounter this error also.

spring boot jpa table dbname.hibernate_sequence dose not exist error

1. MySQL DB Table Structure.

First let us look at the example db table structure as below. Table name is user_account, and the column name is id and it is the table primary key column, please note that you should check three checkbox in the column attributes list , PK(Primary Key), NN(Not Null) and AI(Auto Increment).

mysql table user_account structure

2. Spring Boot Data JPA Entity Class.

We use below java class UserAccount to map to above user_account table. And the id property is mapped to the table id column, and the column value is generated automatically.

@Entity(name = "user_account")
public class UserAccount {

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

    @javax.persistence.Column(name = "user_name")
    private String username;

But if you do not check the AI( auto increment ) checkbox of the MySQL table’s id column attributes, then the error table ‘dev2qa_example.hibernate_sequence’ doesn’t exist happened. Now check the AI( auto increment ) checkbox in MySQL db table’s id column attributes, and run the example again, you will find the error still exist.

3. Fix hibernate_sequence doesn’t exist error.

The reason for the error is because hibernate will look for the hibernate_sequence table when you use GenerationType.AUTO strategy for the entity class’s id field. So to fix it, you had to change the id field generate strategy from GenerationType.AUTO to GenerationType.IDENTITY and make sure the MySQL table’s primary key ( id ) column has the AI ( auto increment ) checkbox being checked.

@Entity(name = "user_account")
public class UserAccount {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x