您的位置:首页 > 数据库

Hibernate不能自动建表 关于数据库方言设置

2014-08-31 18:28 375 查看
最近开始学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.MySQLDialect</property>

这样就行了 ,然后整个世界都安静了。。
虽然没明白为什么这么做,但是终于解决了问题,还是有点点的欣慰。。。
哪位大侠路过能帮忙解释下 ,这是为什么?

资料::
Hibernate dialect大全
RDBMS方言
DB2org.hibernate.dialect.DB2Dialect
DB2 AS/400org.hibernate.dialect.DB2400Dialect
DB2 OS390org.hibernate.dialect.DB2390Dialect
PostgreSQLorg.hibernate.dialect.PostgreSQLDialect
MySQLorg.hibernate.dialect.MySQLDialect
MySQL with InnoDBorg.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAMorg.hibernate.dialect.MySQLMyISAMDialect
Oracle (any version)org.hibernate.dialect.OracleDialect
Oracle 9i/10gorg.hibernate.dialect.Oracle9Dialect
Sybaseorg.hibernate.dialect.SybaseDialect
Sybase Anywhereorg.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Serverorg.hibernate.dialect.SQLServerDialect
SAP DBorg.hibernate.dialect.SAPDBDialect
Informixorg.hibernate.dialect.InformixDialect
HypersonicSQLorg.hibernate.dialect.HSQLDialect
Ingresorg.hibernate.dialect.IngresDialect
Progressorg.hibernate.dialect.ProgressDialect
Mckoi SQLorg.hibernate.dialect.MckoiDialect
Interbaseorg.hibernate.dialect.InterbaseDialect
Pointbaseorg.hibernate.dialect.PointbaseDialect
FrontBaseorg.hibernate.dialect.FrontbaseDialect
Firebirdorg.hibernate.dialect.FirebirdDialect

Access数据库的方言包:http://www.hxtt.com/test/hibernate.zip

路径:com.hxtt.support.hibernate.HxttAccessDialect

例子:

<property name="hibernate.connection.username" value="sa"/>

<property name="hibernate.connection.password" value="sa"/>

<property name="hibernate.connection.url" value="jdbc:sqlserver://localhost:1433;databaseName=test"/>

<property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>

<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>

<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
如果是Toplink则添加:
<property name="toplink.jdbc.user" value="sa"/>

<property name="toplink.jdbc.password" value="sa"/>

<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/test"/>

<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>

<property name="toplink.plafrom.class.name" value="oracle.toplink.essentials.platform.database.HSQLPlatform" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐