19

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

EeE
  • 665
  • 5
  • 12
  • 27
  • How are you dropping the table? In the application or command line? Are you stopping your application after the drop? – dom farr Dec 20 '10 at 15:26
  • Thx for your reply. I drop the table by running command line, not in application. By the way, i just find that when i want to do another operation by application after i create the table by application. The exception throw as well. any clue? – EeE Dec 20 '10 at 15:39

11 Answers11

26

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.

willxiang
  • 291
  • 3
  • 6
14

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.

Community
  • 1
  • 1
codelark
  • 12,254
  • 1
  • 45
  • 49
  • Yes i change to update. but problem still there. i want to re-add the table, but keep throwing "Table 'test.person' doesn't exist"; the table is suppose to be add automatically. Then i have to restart my SQL server. Then i can add table. I think i do run the commit() and close() after the transaction. But after the first operation create a table, It keeps throwing "Table 'test.person' doesn't exist" exception. – EeE Dec 20 '10 at 16:15
  • Stop dropping the table? IF you want to delete the data use DELETE FROM instead. – OrangeDog Dec 20 '10 at 17:13
  • 2
    Even better, if you just want to remove all rows in the table, use TRUNCATE TABLE test.person. – Jeshurun Aug 03 '11 at 17:07
  • I had trouble getting the provided form to work, but this form worked fine for me: `` – Torque Mar 07 '13 at 00:13
6

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.

jpkroehling
  • 13,881
  • 1
  • 37
  • 39
  • By the way, the log entry that I'm talking about look like this: 01:30:08,032 INFO Environment:540 - loaded properties from resource hibernate.properties: {url=jdbc:hsqldb:hsqldb/hibernate, hibernate.connection.driver_class=org.hsqldb.jdbcDriver, hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider, schema=, hibernate.cache.use_query_cache=true, hibernate.dialect=org.hibernate.dialect.HSQLDialect, username=sa, hibernate.jdbc.use_streams_for_binary=true ... – jpkroehling Dec 20 '10 at 15:30
  • Sorry for my missing typing. i restart MySQL server, when exception throws. However, after i create the table by application. i want to run further operation such as read, update operation. The "Table 'test.person' doesn't exist" exception throws – EeE Dec 20 '10 at 15:42
  • Can you try to remove the "hibernate" prefix from the property name? IIRC, it's invalid for cfg.xml files, and is needed only for .properties files. Also, please – jpkroehling Dec 20 '10 at 16:03
6

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.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
Ashish Singh
  • 399
  • 4
  • 11
4

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>
drops
  • 1,524
  • 1
  • 11
  • 20
3

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

Chetan Joshi
  • 339
  • 2
  • 5
  • 19
  • I had the same case of problem today, problem with SQLException is that it only shows you that a table doesn't exist, not telling you why the table couldn't be created (Hibernate configuration, columns types ...). – Yassir Khaldi Nov 10 '17 at 11:52
3

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.

Muhammad Usama
  • 167
  • 1
  • 3
2

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

besartm
  • 558
  • 1
  • 7
  • 14
0

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.

Luan Pham
  • 76
  • 4
0

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.

0

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
Pratik Gaurav
  • 661
  • 7
  • 8