Hibernate不能自动建数据表解决办法

来源:互联网 发布:拿破仑全面战争mac 编辑:程序博客网 时间:2024/04/24 10:36

首先自己要注意自己的MYSQL版本,然后设置对应的方言

  • 兼容性模式

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

  • 需要注意的是5.5一下版本可以使用

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

  • Mysql数据库版本是5.5的,设置事务性方言时要修改,就是加一个5

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


此外还要注意其他方面的问题

1.错误原因:hbm文件中定义的元素含有sql的关键字

例如: <property name="name"></property>    <property name="birthday"></property>    <property name="from"></property>   代码中含有"from"关键字. 


2.错误原因:配置文件中使用了create-drop属性,数据库表其实已经创建,只是又被删除了: 

<property name="hbm2ddl.auto">create-drop</property>   只要把create-drop替换为create即可 


 3.错误原因:默认的数据库类型错误。  

把mysql中my.ini文件中配置的 MyISAM 变成 INNODB,代码如下: 
# The default storage engine that will be used when create new tables when    

default-storage-engine=INNODB 


4.错误原因;hbm文件中未写错,但是在myeclipse中开发的  
myeclipse不能自动识别如下语句,需要在Configuration中手动重新配置一下即可运行; 

<property name="hbm2ddl.auto">update</property> 


5.如果在spring里面集成hibernate3,

例如配置代码如下:

复制代码
<property name="hibernateProperties">      <value>     hibernate.dialect=org.hibernate.dialect.MySQLDialect//注意这些地方没有空格     hibernate.hbm2ddl.auto=update//注意这些地方没有空格                                   
hibernate.show_sql=false//注意这些地方没有空格
hibernate.format_sql=false//注意这些地方没有空格
</value> </property>
复制代码

 

注意每行结尾没有空格,不然也会造成不能自动建表的后果

What a meaningless sense if losing myself,though owning all of the world.

0 0
原创粉丝点击