hibernate表不存在错误 [英] hibernate table does not exist error

查看:107
本文介绍了hibernate表不存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在配置hibernate.cfg.xml中,我添加了
< property name =hibernate.hbm2ddl.auto>创建< / property>
当我运行应用程序时,Hibernate会自动创建表。但是,我通过运行drop table sql手动从表中删除表。然后再次运行hibernate应用程序。出现异常


导致:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'test.person'不存在

解决问题的唯一方法就是重新启动Mysql数据库。任何人都可以为我解释这个问题吗?



这是我的hibernate.cfg.xml

 < hibernate构型> 
< 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>


<! - 在启动时删除并重新创建数据库模式 - >
< property name =hibernate.hbm2ddl.auto>建立< / property>

<! - 启用Hibernate的自动会话上下文管理 - >
< property name =current_session_context_class>线程< / property>

<! - 禁用二级缓存 - >
< property name =cache.provider_class> org.hibernate.cache.NoCacheProvider< / property>

<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> true< / property>

<! - 映射文件 - >
< mapping resource =com / mapping / Event.hbm.xml/>
< mapping resource =com / mapping / Person.hbm.xml/>
< / session-factory>



Thx

解决方案

我不相信使用 create 会更新就地模式,添加您删除的表。尝试:

 < property name =hibernate.hbm2ddl.auto>更新< / property> 

这是创建一个模式(如果不存在的话),并尝试修改现有模式以匹配您已定义的映射。



另外,阅读关于所有可能的值。


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

解决方案

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.

这篇关于hibernate表不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆