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.
- First, let us look at the example DB table structure.
- The 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 checkboxes in the column attributes list, PK(Primary Key), NN(Not Null), and AI(Auto Increment).
2. Spring Boot Data JPA Entity Class.
- We use the below java class UserAccount to map to the 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 { @Id @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 will appear.
- 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.
- 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 checked.
@Entity(name = "user_account") public class UserAccount { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
Thank you for tip. It was helpful to me.