測試環境: Eclipse 201903; Hibernate 5.4.3; MySQL 5.7
現象:
hibernate.cfg.xml配置文件中明明寫了以下語句, 這樣即使表不存在hibernate也應該可以自動創建
<property name="hibernate.hbm2ddl.auto">update</property>
然而運行時控制台報錯:
WARN: SQL Error: 1146, SQLState: 42S02
ERROR: Table 'hiber01.tuser' doesn't exist
原因:
1146的錯誤原因是:在默認的數據中找不到指定的表。
hibernate里的dialect和Mysql的版本不匹配,SQL語句里的type=“****”使用在MySQL5.0之前,5.0之后就要是使用engine=“****”。
解決:
修改hibernate.cfg.xml文件
1 MySql5.0之前的配置 2 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 3 4 MySql5.0之后的配置 5 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
使用了dialect.MySQL,生成的建表語句為:
Hibernate: create table Student ( id integer not null, name varchar(255), age integer, primary key (id) ) type=MyISAM
使用了dialect.MySQL5之后,生成的建表語句為:
Hibernate: create table Teacher ( id integer not null, age integer not null, name varchar(255), qq integer not null, primary key (id) ) engine=MyISAM
總而言之就一句話
mysql5.0之前 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
mysql5.0之后 <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
---------------------
以上內容大部分轉載:
原作者:世宇同學
來源:CSDN
原文:https://blog.csdn.net/weixin_40327259/article/details/80803754
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!