Hibernate不能自动建表解决办法【转载】

  最近开始学Hibernate,看的是李刚的那本《轻量级java ee企业应用实战》。头一个hibernate程序,我原原本本的按照书上例子写下来,同时只是改动了些mysql的连接参数,并且在mysql中新建了一个hibernate数据库,仅此而已。然而预想不到的事情发生了……程序写好之后,运行,报错

       Hibernate: insert into news_table (title, content) values (?, ?)
       Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [org.crazyit.app.domain.News]

      …………此处省略
      Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘hibernate.news_table‘ doesn‘t exist

      …………此处省略

       当时我就蛋疼了……情况是这样的,如果在mysql中把news_table表建好,然后运行成功,这说明连接肯定是没问题的。可就是不能自动建表,百度啊,遇到同样问题的人不少,按照他们所说的一个个去解决,可是还是没有效果。csdn上也发帖问了,有人说是:  <propertyname="hibernate.hbm2ddl.auto">update(create)</property>这个设置有问题,应该用update。好吧,我敢说我本来用的就是update,还是会出错。。有人说是什么mysql引擎不配对的缘故,好吧,我敢说<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>这样设置,在mysql的配置文件ini中什么引擎之类的也是innodb的(这个不太了解,随便说点),当时我想问题肯定也不在这。还有人说,可能是你持久类的字段设置成为关键字,这个更不靠谱了。。。。。

       后来我想也许是版本兼容问题,书上说的是用Hibernate3我用的是hibernate4。好吧我就重新下载hibernate3还是没用。。。。。。就这样,两天下来,什么各种方式都尝试了,还是报一样错

       最后实在没法了,看了下hibernate的视频教程,一步一步跟着做,每一个细节都不放过,尝试着hibernate.cfg.xml和**.hbm.xml的每一个配置。。。。。终于才被我发现了

原来的配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<!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.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">liaobin1992</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  <mapping resource="org/crazyit/app/domain/News.hbm.xml"/>       
</session-factory>
</hibernate-configuration>
更改之后的配置:

<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
把这行换成:

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

这样就行了 ,然后整个世界都安静了。。

虽然没明白为什么这么做,但是终于解决了问题,还是有点点的欣慰。。。

哪位大侠路过能帮忙解释下 ,这是为什么?

时间: 2024-08-01 22:42:56

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

[转]Hibernate不能自动建表解决办法及Hibernate不同数据库的连接及SQL方言

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

Spring整合Hibernate中自动建表

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

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

java中根据hibernate配置文件自动建表

1.对与java项目,做数据库迁移时,都会用的数据脚本. 2.当引入hibernate时,可以创建数据库表的配置文件.可以根据表的配置文件自动在数据库建表.(数据库要预先建立好,因为hibernate只会建表,不会建库) 步骤: 1).在配置文件 hibernate.cfg.cml 中加入参数 ,配置相关数据源参数和pojo文件 <property name="hbm2dll.auto">update</property> <hibernate-confi

mysql数据库下hibernate无法自动建表

mysql5使用的方言应为 <property name="dialect"> org.hibernate.dialect.MySQL5InnoDBDialect </property>

配置hibernate根据实体类自动建表功能

Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码 Xml代码<property name="hbm2ddl.auto">update</property>     update:表示自动根据model对象来更新表结构,启动hibernate时会自动检查数据库,如果缺少表,则自动建表:如果表里缺少列,则自动添加列. 还有其他的参数: create:

Hibernate中的自动建表及11选5平台开发

Hibernate支持自动建表.11选5平台开发(企 娥:217 1793 408)开发阶段,可通过在hibernate.cfg.xml中添加常量值来设置自动建表. 以mysql为例,只需要在配置文件中添加如下语句即可. <property name="hbm2ddl.auto"> 参数 </property> hibernate框架为参数提供了四个可选项(update.create.create-drop.validate),我们根据需求选择. 四个参数意义: