Spring Boot MySQL : Table ‘DB_NAME.hibernate_sequence’ doesn’t exist

Spring Boot + Spring Data JPA + MySQL, and hits the following error message when the application starts :

Tested with :

  • Spring Boot 2.1.2.RELEASE
  • mysql-connector-java 8.0.13
  • Hibernate 5.3.7

java.sql.SQLSyntaxErrorException: Table 'DB_NAME.hibernate_sequence' doesn't exist
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:974)
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1024)
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
	at org.hibernate.id.enhanced.TableStructure.executeQuery(TableStructure.java:216)
	at org.hibernate.id.enhanced.TableStructure.access$300(TableStructure.java:46)
application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=mkyong
spring.datasource.password=password

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book {

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

Solution

By default, Hibernate generates key from hibernate_sequence table, we can disable it by setting this hibernate.use-new-id-generator-mappings to false.

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=mkyong
spring.datasource.password=password

spring.jpa.hibernate.use-new-id-generator-mappings=false

References

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
Jeet Singh Parmar
Guest
Jeet Singh Parmar

Instead of setting this property to false
spring.jpa.hibernate.use-new-id-generator-mappings=false

We can add below property then also it will work
spring.jpa.hibernate.ddl-auto=update

Raj
Guest
Raj

Yes

Raccoon
Guest
Raccoon

Why do we get this error?
And this solution doesn’t work for me
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

CREATE TABLE model
(
id SERIAL NOT NULL,
name VARCHAR(64) NOT NULL UNIQUE,
PRIMARY KEY (id)
);

sagar
Guest
sagar

this solution works