Hibernate中无法自动创建表的问题.(SQL Error: 1146, SQLState: 42S02)

测试环境: 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 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/chashaotree/p/11093121.html
今日推荐