-->
Skip to main content

Solution : Table 'hibernate_sequence' doesn't exist

Table 'hibernate_sequence' doesn't exist 

Maven + Eclipse + Hibernate 5 + Java 14 + Mysql and After successful project creation when application starts then hits the following error message Table 'hibernate_sequence' doesn't exist.

Table 'hibernate_sequence' doesn't exist

Tested with:

1, Hibernate 5.4.26
2. mysql-connector-java-8.0.22.jar
3. Java 14
4. Maven 3.6.3
Caused by: java.sql.SQLSyntaxErrorException: Table 'hibernate.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:953)
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1003)
	at org.hibernate.id.enhanced.TableStructure.executeQuery(TableStructure.java:216)
	at org.hibernate.id.enhanced.TableStructure.access$300(TableStructure.java:46)
	at org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:138)
	at org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:126)
	at org.hibernate.jdbc.WorkExecutor.executeReturningWork(WorkExecutor.java:55)
	at org.hibernate.jdbc.AbstractReturningWork.accept(AbstractReturningWork.java:34)
	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:57)
	... 41 more

Entity Class

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 @Entity
 public class Person {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	@Column
	String name;

	public Person() {
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + "]";
	}	
 }

hibernate.cfg.xml

<hibernate-configuration>
	<session-factory>
		<!--Database connection settings-->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
		<property name="connection.username">root</property>
		<property name="connection.password">mysql</property>
		<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
		<!--Echo all executed SQL to stdout-->
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<!--Drop and re-create the database schema on startup-->
		<property name="hbm2ddl.auto">update</property>		
		<mapping class="com.javacodegeek.helloworld.Person">
	</mapping></session-factory>
</hibernate-configuration>

Solution

By default, Hibernate generates key from hibernate_sequence table, we can disable it by setting property hibernate.id.new_generator_mappings to false as shown below
<property name="hibernate.id.new_generator_mappings">false</property>

Updated hibernate.cfg.xml 

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- Database connection settings -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
		<property name="connection.username">root</property>
		<property name="connection.password">mysql</property>
		<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<!-- Drop and re-create the database schema on startup -->
		<property name="hbm2ddl.auto">update</property>
		<property name="hibernate.id.new_generator_mappings">false</property>
		<mapping class="com.javacodegeek.helloworld.Person" />
	</session-factory>
</hibernate-configuration>

Conclusion

That's all about Table 'hibernate_sequence' doesn't exist. In this tutorial, we solved this issue which occurs due to changes from hibernate 4 to hibernate 5 as shown in the hibernate excerpt

Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.

Happy Learning!!!

References


Comments