Hibernate设置自动生成表失败

  • 出错代码:

<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License:
    GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the
    lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->
<!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>
        <!-- 数据库基本配置参数 -->
        <property name="hibernate.connection.url">jdbc:mysql:///javaweb</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">wyd</property>

        <!-- hibernate常用配置 -->
        <!-- 配置Hibernate生成sql的方式 -->
        <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property>
        <!-- 查看hibernate生成的sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 格式化sql语句 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 自动见表 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 映射配置 -->
        <mapping resource="claire/a_config/User.hbm.xml" />

    </session-factory>
</hibernate-configuration>

hibernate.cfg.xml

<property name="hibernate.hbm2ddl.auto">update</property>
  • 异常输出:
org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178)
    at org.hibernate.dialect.identity.GetGeneratedKeysDelegate.executeAndExtract(GetGeneratedKeysDelegate.java:57)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2933)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3524)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:637)
    at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:282)
    at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:263)
    at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:317)
    at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:318)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:275)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:692)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:684)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:679)
    at claire.a_config.APP.test(APP.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘javaweb.users‘ doesn‘t exist
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175)
    ... 45 more
  • 出错原因:hibernate里的dialect和MySQL的版本不匹配,本机采用的MySQL为5.5.40版本,原先设置的Dialect只能在MySQL5.0版本前使用。
  • 解决:将方言改为合适版本即可。

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

    * 方言版本查找方式

原文地址:https://www.cnblogs.com/clairexxx/p/10480826.html

时间: 2024-08-29 21:43:04

Hibernate设置自动生成表失败的相关文章

hibernate不能自动生成表的原因总结

1. upate->create <property name="hbm2ddl.auto">create</property> 2. 2.Mapping的映射文件有重名的,如: <mapping resource="com/runsky/domain/Status.hbm.xml" /> <mapping resource="com/runsky/domain/Status.hbm.xml" /&

Hibernate使用自定义脚本替换注解或者xml文件中的自动生成表结构

本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50534361 我们都清楚,可以使用hibernate的metadata元数据生成表结构,但是一般情况下,我们光靠hibernate的自动生成是远远不够的,我们期待使用自己的sql脚本,你hibernate自动执行那个脚本就可以.那么hibernate支持不支持呢?答案是yes! 只需要我们做以下设置: <property name="hibernate.hb

Hibernate框架hibernate.cfg.xml配置文件,配置自动生成表结构策略。

<property name="hbm2ddl.auto"></property> key -- hbm2ddl.auto:自动生成表结构策略 value -- update(使用最多):当数据库不存在表时,hibernate启动后会自动生成表结构. 当数据库表存在时,如果一样,则只会写入数据,不会改变表结构. 当数据库表存在时,如果不一样,则会修改表结构,原有的表结构不会改变.  create(很少):无论表结构是否存在,hibernate启动后都会重新生成表

Hibernate不能自动建表解决办法(转载自:http://blog.csdn.net/biangren/article/details/8010018)

最近开始学Hibernate,看的是李刚的那本<轻量级java ee企业应用实战>.头一个hibernate程序,我原原本本的按照书上例子写下来,同时只是改动了些mysql的连接参数,并且在mysql中新建了一个hibernate数据库,仅此而已.然而预想不到的事情发生了……程序写好之后,运行,报错 Hibernate: insert into news_table (title, content) values (?, ?)       Exception in thread "m

[转] Hibernate不能自动建表解决办法(hibernate.hbm2ddl.auto) (tables doesn&#39;t exist)

转自: http://blog.csdn.net/biangren/article/details/8010018 最近开始学Hibernate,看的是李刚的那本<轻量级java ee企业应用实战>.头一个hibernate程序,我原原本本的按照书上例子写下来,同时只是改动了些mysql的连接参数,并且在mysql中新建了一个hibernate数据库,仅此而已.然而预想不到的事情发生了……程序写好之后,运行,报错 Hibernate: insert into news_table (title

Spring整合Hibernate中自动建表

Spring整合Hibernate中自动建表 博客分类: JavaEE Java代码   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> <

yii2自动生成表单

视图中: 1.要use的两个文件类 use yii\helpers\Html;   use yii\widgets\ActiveForm; 2.生成表单,以添加商品为例说明.注意红线区域:上传文件需要注意的地方一级表单中有textarea文本域的使用 yii2自动生成表单

hibernate不能自动建表:MySQLSyntaxErrorException: Table &#39;&#39; doesn&#39;t exist

修改前: <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 修改后: <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> hibernate不能自动建表:MySQLSyntaxErrorExcept

spring和hibernate整合时设置自动生成数据库的表

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource"> <property name="hibernateProperties"> <props> <prop key="hibernat