在单元测试中应用Hibernate配置文件

开发 后端
本文向您介绍在单元测试中应用开源对象关系映射Hiberate的配置文件,通过对new Configuration()的操作得到相应的效果。

Hibernate是一个流行的开源对象关系映射工具,单元测试和持续集成的重要性也得到了广泛的推广和认同,在采用了Hibernate的项目中如何保证测试的自动化和持续性呢?本文讨论了Hibernate配置文件hibernate.properties和hibernate.cfg.xml的过程,以及怎么样将hibernate提供的配置文件的访问方法灵活运用到单元测试中。

介绍

Hibernate 是一个流行的开源对象关系映射工具,单元测试和持续集成的重要性也得到了广泛的推广和认同,在采用了Hibernate的项目中如何保证测试的自动化和持续性呢?本文讨论了Hibernate加载其配置文件hibernate.properties和hibernate.cfg.xml的过程,以及怎么样将hibernate提供的配置文件的访问方法灵活运用到单元测试中。注意:本文以hibernate2.1作为讨论的基础,不保证本文的观点适合于其他版本。

1.准备

对于hibernate的初学者来说,***次使用hibernate的经验通常是:

1) 安装配置好Hibernate,我们后面将%HIBERNATE_HOME%作为对Hibernate安装目录的引用,

2) 开始创建好自己的***个例子,例如hibernate手册里面的类Cat,

3) 配置好hbm映射文件(例如Cat.hbm.xml,本文不讨论这个文件内配置项的含义)和数据库(如hsqldb),

4) 在项目的classpath路径下添加一个hibernate.cfg.xml文件,如下(***次使用hibernate最常见的配置内容):

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-configuration  
  3.   PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"  
  4.   "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">  
  5.  
  6. <hibernate-configuration>  
  7. <session-factory>  
  8.  
  9.  <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>  
  10.  <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>  
  11.  <property name="connection.username">sa</property>  
  12.  <property name="connection.password"></property>  
  13.  <property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property>  
  14.  
  15.  <property name="hibernate.show_sql">false</property>  
  16.  
  17.  <mapping resource="Cat.hbm.xml"/>  
  18.  
  19. </session-factory>  
  20. </hibernate-configuration> 

5) 然后还需要提供一个类来测试一下创建,更新,删除和查询Cat,对于熟悉JUnit的开发人员,可以创建一个单元测试类来进行测试,如下:

  1. import junit.framework.TestCase;  
  2. import net.sf.hibernate.HibernateException;  
  3. import net.sf.hibernate.Session;  
  4. import net.sf.hibernate.Transaction;  
  5. import net.sf.hibernate.cfg.Configuration;  
  6.  
  7. public class CatTest extends TestCase {  
  8.  
  9.  private Session session;  
  10.  private Transaction tx;  
  11.  
  12.  protected void setUp() throws Exception {  
  13.   Configuration cfg = new Configuration().configure();////注意这一行,这是本文重点讨论研究的地方。  
  14.   session = cfg.buildSessionFactory().openSession();  
  15.   tx = session.beginTransaction();  
  16.  }  
  17.  
  18.  protected void tearDown() throws Exception {  
  19.   tx.commit();  
  20.   session.close();  
  21.  }  
  22.  
  23.  public void testCreate() {  
  24.   //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。  
  25.  }  
  26.  public void testUpdate() {  
  27.   //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。  
  28.  }  
  29.  public void testDelete() {  
  30.   //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。  
  31.  }  
  32.  public void testQuery() {  
  33.   //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。  
  34.  }  

2、new Configuration()都做了什么?

对于***次使用hibernate的新手来说,下面的这段代码可以说是最常见的使用Configuration方式。

Configuration cfg = new Configuration().configure();

Configuration是hibernate的入口,在新建一个Configuration的实例的时候,hibernate会在classpath里面查找hibernate.properties文件,如果该文件存在,则将该文件的内容加载到一个Properties的实例GLOBAL_PROPERTIES里面,如果不存在,将打印信息

hibernate.properties not found

然后是将所有系统环境变量(System.getProperties())也添加到GLOBAL_PROPERTIES里面( 注1)。如果hibernate.properties文件存在,系统还会验证一下这个文件配置的有效性,对于一些已经不支持的配置参数,系统将打印警告信息。

3、configure()在做什么?

new Configuration()讨论至此,下面讨论configure()方法。

configure()方法默认会在classpath下面寻找hibernate.cfg.xml文件,如果没有找到该文件,系统会打印如下信息并抛出HibernateException异常。

hibernate.cfg.xml not found

如果找到该文件,configure()方法会首先访问< session-factory >,并获取该元素的name属性,如果非空,将用这个配置的值来覆盖hibernate.properties的hibernate.session_factory_name的配置的值,从这里我们可以看出,hibernate.cfg.xml里面的配置信息可以覆盖hibernate.properties的配置信息。

接着configure()方法访问<session-factory>的子元素,首先将使用所有的<property>元素配置的信息( 注2),如前面我们使用的配置文件

  1. <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>  
  2. <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>  
  3. <property name="connection.username">sa</property>  
  4. <property name="connection.password"></property>  
  5. <property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property> 

会覆盖hibernate.properties里面对应的配置,hibernate2.1发布包里面自带的hibernate.properties文件(位于%HIBERNATE_HOME%/etc下面)里面的值,如下:

  1. hibernate.dialect net.sf.hibernate.dialect.HSQLDialect  
  2. hibernate.connection.driver_class org.hsqldb.jdbcDriver  
  3. hibernate.connection.username sa  
  4. hibernate.connection.password  
  5. hibernate.connection.url jdbc:hsqldb:hsql://localhost 

然后configure()会顺序访问以下几个元素的内容

  1. <mapping>  
  2. <jcs-class-cache>  
  3. <jcs-collection-cache>  
  4. <collection-cache> 

其中<mapping>是必不可少的,必须通过配置<mapping>,configure()才能访问到我们定义的Java对象和关系数据库表的映射文件(hbm.xml),例如:

<mapping resource="Cat.hbm.xml"/>

通过以上的分析,我们对Hibernate配置文件hibernate.properties和hibernate.cfg.xml的默认的加载过程就比较清楚了。

 

【编辑推荐】

  1. 强人Hibernate文档笔记(上)
  2. 强人Hibernate文档笔记(中)
  3. 强人Hibernate文档笔记(下)
  4. Hibernate优化方法解析
  5. Hibernate的性能优化
责任编辑:佚名 来源: 天极网
相关推荐

2009-09-29 16:21:31

Hibernate单元

2009-09-25 10:33:25

Hibernate单元

2022-12-08 08:01:02

Python测试单元

2009-06-02 14:24:32

Hibernate单元测试HSQLDB

2022-04-08 09:01:56

脚本Go应用单元

2017-01-14 23:42:49

单元测试框架软件测试

2009-09-25 10:49:25

Hibernate加载

2009-09-22 10:23:15

Hibernate配置

2021-03-24 09:30:02

Jupyter not单元测试代码

2021-08-03 12:58:38

鸿蒙HarmonyOS应用

2009-09-27 13:25:22

2009-09-29 15:52:26

Hibernate X

2009-09-29 17:29:43

Hibernate S

2017-01-16 12:12:29

单元测试JUnit

2017-01-14 23:26:17

单元测试JUnit测试

2020-08-18 08:10:02

单元测试Java

2023-07-26 08:58:45

Golang单元测试

2011-07-04 18:16:42

单元测试

2020-05-07 17:30:49

开发iOS技术

2017-03-23 16:02:10

Mock技术单元测试
点赞
收藏

51CTO技术栈公众号