二.六 创建SessionFactory对象
在service包下,创建一个基本的CreateTest类进行测试。 用Junit4进行测试。
一定要创建到SessionFactory对象。 当出现这个对象时,才会执行自动创建表。
package com.yjl.service; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.junit.Test; /** @author: 两个蝴蝶飞 @date: 2018年9月29日 下午8:30:59 @Description 测试创建User表是否成功 */ public class CreateTest { @Test public void createTest(){ //获取配置 Configuration config=new Configuration(); config.configure(); //创建sessionFactory对象,要到session这一步。 SessionFactory sessionFactory=config.buildSessionFactory(); /*上面这是Hibernate 5的写法。 如果用的是Hibernate 4的话,那么是这样,要添加服务登记*/ /*configuration=new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // 实例化服务登记 sessionFactory=configuration.buildSessionFactory(serviceRegistry); */ } }
二.七 Junit启动createTest方法,进行测试
在进行测试时,出现了两个相当大的问题。
二.七.一 添加Log4j2.xml 日志 文件
报的错误是:
ERROR StatusLogger No log4j2 configuration file found.
Using default configuration: logging only errors to the console.
搜索网上的答案,需要创建一个log4j2.xml文件。 log4j.properties文件不好使。 将log4j.properties文件改成log4j2.properties文件也不好使。只有创建log4j2.xml文件。
在src下,必须是src下创建一个log4j2.xml文件。内容是:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%m%n" /> </Console> </Appenders> <Loggers> <!-- 修改级别为DEBUG --> <Root level="DEBUG"> <AppenderRef ref="Console" /> </Root> </Loggers> </Configuration>
二.七.二 报创建表时搜索引擎的问题 (楼主MySQL版本为5.5 5.1也会报这个错误。其余版本请忽略)
将log4j2.xml文件添加成功之后,运行时,出现:
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=MyISAM’ at line 9
查看了一下MySQL中my.ini中文件的引擎值,为
其实,这是与MySQL无关的。 是Hibernate自己的问题。
在5.5和5.1版本时,MySQL创建的引擎是MyISAM。 其余版本是INNODB。 故其余版本请忽略这个问题。
问题答案出处(感谢!!!): http://jasonhan-sh-hotmail-com.iteye.com/blog/1473955
将hibernate.cfg.xml中的
org.hibernate.dialect.MySQLDialect
改成:
org.hibernate.dialect.MySQL5InnoDBDialect
重新测试: 引擎变成了InnoDB。
表也创建成功了。
三. Hibernate对MySQL方言的支持
对:
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
进行ctrl+鼠标点击,进行查看:
@Deprecated /* */ public class MySQL5InnoDBDialect /* */ extends MySQL5Dialect /* */ { /* */ protected MySQLStorageEngine getDefaultMySQLStorageEngine() /* */ { /* 20 */ return InnoDBStorageEngine.INSTANCE; /* */ } // }
但是这个类MySQL5InnoDBDialect 已经过时了。
它的那个返回值是:
public static final MySQLStorageEngine INSTANCE = new InnoDBStorageEngine();
表示创建的是InnoDB引擎。
它继承了MySQL5Dialect。 查看MySQL5Dialect
public class MySQL5Dialect extends MySQLDialect{}
它继承了MySQLDialect 类。这个MySQLDialect表示是关于MySQL的方言。
而MySQLDialect类继承了Dialect类
public class MySQLDialect extends Dialect{}
这个类Dialect自然就是所有数据库类的父类。
ctrl+T, 查看Dialect类的继承结构:
里面有MySQL,有DB2,有Oracle等各个方言。
ctrl+T, 查看MySQL5Dialect
既然MySQL5InnoDBDialect已经过时了,那么Hibernate官方肯定要给一个更好的。此时用MySQL5Dialect试试。
org.hibernate.dialect.MySQL5Dialect
可以运行。 以后都改成MySQL5Dialect了。 刚开始的MySQLDialect太父类了,基本没有什么功能了。
四. 其他常见错误的演示
四.一 没有引入mysql驱动jar包
四.二 User.hibernate.xml中的属性property没有与User类相对应
<!-- 正确的是:<property name="userName"></property> 现在改成写:username--> <property name="username"></property>
四.三 没有引入User.hibernate.xml文件
这个时候,不会报错。 但不会创建user表。 因为hibernate.cfg.xml与User.hibernate.xml没有任何关系。这个问题最难发现,要多注意。
四.四 引入了错误的User.hibernate.xml文件
<!-- 正确的应该是pojo包下,而不是po包下 --> <mapping resource="com/yjl/po/User.hibernate.xml"/>
可以通过ctrl+鼠标进行点击,如何路径正确,会跳转到相应的xml文件。如果不正确,不会进行跳转。
注意此时写的是 ///(斜杠) 文件夹的形式,并不是…(点) 包的形式。(这一点不像MyBatis) 也会报这一个not found的错误。
Hibernate是一个非常牛的框架。建议多学习。
谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!