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 the 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 others who may be encounter this error also.

1. MySQL DB Table Structure.

  1. First, let us look at the example DB table structure.
  2. The table name is user_account, and the column name is id and it is the table primary key column.
  3. Please note that you should check three checkboxes in the column attributes list, PK(Primary Key), NN(Not Null), and AI(Auto Increment).

2. Spring Boot Data JPA Entity Class.

  1. We use the below java class UserAccount to map to the above user_account table.
  2. 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 {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @javax.persistence.Column(name = "user_name")
        private String username;
        ......
        ......
    }
  3. 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 will appear.
  4. 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 exists.

3. Fix hibernate_sequence doesn’t exist error.

  1. 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.
  2. 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 checked.
    @Entity(name = "user_account")
    public class UserAccount {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x