使用hibernate和单元测试类,不能在MySQL数据库中自动生成对应数据表的问题【已解决】

 在JUnit单元测试中,通过hibernate在MySQL数据库中生成 对应类的数据表时,

提示异常信息: 

 org.hibernate.exception.SQLGrammarException: could not execute statement

 ...

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernate1.news' doesn't exist

 配置信息:

<!-- 连接数据库的基本信息 -->
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate1</property>

<!-- 配置hibernate的基本信息 -->
<!-- hibernate所使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

<!-- 执行操作时是否在控制台打印SQL -->
<property name="show_sql">true</property>

<!-- 是否对 SQL进行格式化 -->
<property name="format_sql">true</property>

<!-- 指定生成数据表的策略update:自动生成数据表 -->
<property name="hbm2ddl.auto">update</property>

<!-- 指定关联的hbm.xml文件 -->
<mapping resource="com/huaxu/hibernate/helloworld/News.hbm.xml"/>

 问题:既然配置了<property name="hbm2ddl.auto">update</property>,为什么会提示我 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernate1.news' doesn't exist的信息呢?

按理说应该自动生成表格,而不是告诉我Table 'hibernate1.news' doesn't exist的信息。

解决过程:

 1.修改JUnit单元测试类,把执行保存操作的步骤注掉如下:

@Test
void test() {
// 1.创建sessionFactory对象
SessionFactory sessionFactory = null;
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
// 2.创建一个session对象
Session session = sessionFactory.openSession();
// 3.开启事务
Transaction transaction = session.beginTransaction();
// 4.执行保存操作
/*
News news = new News("Java", "huaxu", new Date(new java.util.Date().getTime()));
session.save(news);
*/
// 5.提交事务
transaction.commit();
// 6.关闭session
session.close();
// 7.关闭sessionFactory对象
sessionFactory.close();
}

2.还是有错误提示,如下:

控制台输出信息:

Hibernate: 
    create table NEWS (
       ID integer not null auto_increment,
        TITLE varchar(255),
        AUTHOR varchar(255),
        DATE date,
        primary key (ID)
    ) type=InnoDB

错误信息:

WARN: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement

...

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 7

此时提示的错误信息是:SQL语法错误type=InnoDB。该句不能正常解析。正好与配置信息:

<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>相对应。

3.在修改数据库方言无效的情况下,将该条配置信息<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>删除后,重新进行单元测试。问题解决。

可以自动在MySQL数据库中生成对应的表格信息。



 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值