hibernate表不存在错误

EeE*_*EeE 12 hibernate

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

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'test.person'不存在

解决问题的唯一方法是重启Mysql数据库.谁能为我解释这个问题?

这是我的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>  
Run Code Online (Sandbox Code Playgroud)

谢谢

cod*_*ark 12

我不相信使用create会更新就地架构来重新添加您删除的表.尝试:

<property name="hibernate.hbm2ddl.auto">update</property>
Run Code Online (Sandbox Code Playgroud)

如果模式不存在,则创建模式,并尝试修改现有模式以匹配您定义的映射.

此外,阅读这个问题有关的所有可能的值.

  • 更好的是,如果您只想删除表中的所有行,请使用TRUNCATE TABLE test.person. (2认同)

小智 12

请从中更改代码?

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

至 ?

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

试试吧。


jpk*_*ing 6

"财产"和"名称"之间是否有空格?

<propertyname="hibernate.hbm2ddl.auto">create</property>
Run Code Online (Sandbox Code Playgroud)

如果没有,那么这可能就是问题所在.另外,当你"重新启动MySQL数据库"时,你的意思是什么?这是否意味着您只需重新启动MySQL服务器,或者这意味着您需要手动重新创建表?此外,如果上面的XML摘录确实包含"property"和"name"之间的空格,请同时提供hibernate日志的除外,特别是它列出了它标识的所有属性的部分.


Ash*_*ngh 6

请从\xef\xbc\x9a更改代码

\n\n
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>\n
Run Code Online (Sandbox Code Playgroud)\n\n

到\xef\xbc\x9a

\n\n
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>\n
Run Code Online (Sandbox Code Playgroud)\n\n

尝试一下。这对我来说确实有效。

\n