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

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

  介绍

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

  读者
 
  Java开发人员,要求熟悉JUnit和掌握Hibernate的基础知识

  内容

  1.准备

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

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

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

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

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

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
  PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>

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

 <property name="hibernate.show_sql">false</property>

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

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

import junit.framework.TestCase;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

public class CatTest extends TestCase {

 private Session session;
 private Transaction tx;

 protected void setUp() throws Exception {
  Configuration cfg = new Configuration().configure();////注意这一行,这是本文重点讨论研究的地方。
  session = cfg.buildSessionFactory().openSession();
  tx = session.beginTransaction();
 }

 protected void tearDown() throws Exception {
  tx.commit();
  session.close();
 }

 public void testCreate() {
  //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。
 }
 public void testUpdate() {
  //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。
 }
 public void testDelete() {
  //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。
 }
 public void testQuery() {
  //请在此方法内添加相关的代码,本文不讨论怎么样使用Hibernate API。
 }
}
  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),如前面我们使用的配置文件

<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property>
  会覆盖hibernate.properties里面对应的配置,hibernate2.1发布包里面自带的hibernate.properties文件(位于%HIBERNATE_HOME%/etc下面)里面的值,如下:

hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class org.hsqldb.jdbcDriver
hibernate.connection.username sa
hibernate.connection.password
hibernate.connection.url jdbc:hsqldb:hsql://localhost
  然后configure()会顺序访问以下几个元素的内容

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

<mapping resource="Cat.hbm.xml"/>
  通过以上的分析,我们对hibernate配置文件hibernate.properties和hibernate.cfg.xml的默认的加载过程就比较清楚了。

本文地址:http://com.8s8s.com/it/it10244.htm