EeE
EeE

Reputation: 675

hibernate table does not exist error

In configuration hibernate.cfg.xml, i add <property name="hibernate.hbm2ddl.auto">create</property> Hibernate do create table automatically when i run the application. However, i remove the table from database manually by running drop table sql. Then run the hibernate application again. The exception appear

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.person' doesn't exist

only way to fix the problem is restart the Mysql database. Could anyone explain this issue for me?

this is my hibernate.cfg.xml

<hibernate-configuration>  
<session-factory>  
    <property name="hibernate.connection.driver_class">  
        com.mysql.jdbc.Driver  
    </property>  
    <property name="hibernate.connection.url">  
        jdbc:mysql://localhost/test
    </property>  
    <property name="connection.username">root</property>  
    <property name="connection.password">root</property>  
    <property name="dialect">  
        org.hibernate.dialect.MySQLDialect  
    </property>  


    <!-- Drop and re-create the database schema on startup -->
    <property name="hibernate.hbm2ddl.auto">create</property>  

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Mapping files -->  
    <mapping resource="com/mapping/Event.hbm.xml" />  
    <mapping resource="com/mapping/Person.hbm.xml"/>
</session-factory>  

Thx

Upvotes: 19

Views: 48174

Answers (11)

Pratik Gaurav
Pratik Gaurav

Reputation: 907

I solved the issue by selecting the correct dialect I was initially using

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

this issue was resolved using

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

Complete application.properties

spring.datasource.name=jpaPratik
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/practice
spring.datasource.password=Welcome123#
spring.datasource.username=root
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

Upvotes: 0

Belal Ahamd
Belal Ahamd

Reputation: 41

use

<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

as according to your mysql version. If you are using mysql-8, then write this:

<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>

Upvotes: 4

mohit jindal
mohit jindal

Reputation: 26

You have missed update property.

<property name="hibernate.hbm2ddl.auto">update</property>

Also change:

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

to:

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

It will work fine.

Upvotes: 0

Luan Pham
Luan Pham

Reputation: 76

There is an important things that you need to give table name on your class:

@Entity
@Table(name = "person")
public class Person{
}

And inside your persistence.xml you need to check the persistent-unit name is the same with EntityManagerFactory name.

Upvotes: 0

besartm
besartm

Reputation: 568

I had the same problem with Maven Project - Hibernate. I had this dependency on my pom.xml file:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.8.Final</version>
</dependency>
<!-- Mysql Connector -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.21</version>
</dependency>

This solved my problem on hibernate configuration file - hibernate.dialect with MySQL5Dialect instead of MySQLDialect:

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

It seems that number "MySQL5Dialect" specify the version of MySQL

Upvotes: 2

CyberBits
CyberBits

Reputation: 167

org.hibernate.dialect.MySQLDialect

in case of mySQL database, the dialect property must be changed to following:

org.hibernate.dialect.MySQL5Dialect

as default it comes with MySQLDialect with is not supported by InnoDB Storage engine of MySQL database configuration, which is the default for creating the table in Default schema.

Upvotes: 3

Ashish Singh
Ashish Singh

Reputation: 479

Please change the code from:

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

to :

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

Try it. It really worked in my case.

Upvotes: 6

willxiang
willxiang

Reputation: 291

please change the code from:

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

to :

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

try it.

Upvotes: 26

Chetan Joshi
Chetan Joshi

Reputation: 349

I had this same problem. I observed that I was using auto_increment(I was using MySQL Dialect) for a String field. I changed it to int which solved the problem

Upvotes: 3

codelark
codelark

Reputation: 12334

I don't believe using create will update an in-place schema to re-add the table that you dropped. Try:

<property name="hibernate.hbm2ddl.auto">update</property>

This is create a schema if one doesn't exist, and attempt to modify an existing one to match the mapping you have defined.

Also, read this question about all the possible values.

Upvotes: 14

jpkroehling
jpkroehling

Reputation: 14061

Is there a space between "property" and "name"?

<propertyname="hibernate.hbm2ddl.auto">create</property>

If not, then that's probably the issue. Also, what do you mean that it fixes when you "reboot the MySQL database"? Does it means you just restart the MySQL server, or it means that you need to manually recreate the table? Also, if the XML excerpt above indeed contains an space between "property" and "name", please provide also the except for the hibernate logs, specially the part that it lists all the properties it identified.

Upvotes: 6

Related Questions