在单元测试中灵活运用Hibernate的配置文件

类别:Java 点击:0 评论:0 推荐:

 


(草稿)

1 介绍
本文讨论了Hibernate加载其配置文件hibernate.properties和hibernate.cfg.xml的过程,Configuration提供的多种配置途径,以及怎么样结合这些配置方法,灵活运用到单元测试中,满足集成测试的基本要求:自动化测试。
注意:本文以hibernate2.1作为讨论的基础,不保证本文结论适合于其他版本。

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

3 参考文档
官方hibernate reference
夏昕hibernate开发指南


4 内容

对于初学者来说,第一次使用hibernate,在创建好自己的第一个例子之后,例如hibernate手册里面的类Cat,配置好hbm映射文件(例如Cat.hbm.xml,本文不讨论这个文件内配置项的含义),就是在自己的项目的classpath里面添加一个hibernate.cfg.xml文件,典型的配置内容如下:

<?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>


还需要提供一个测试类

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 testLogic()  {
     //writing ur test code here.
    }

}

//注意setup()方法的这一行,这是本文重要讨论研究的地方。


这对于第一次使用hibernate的新手来说,下面的这段代码可以说是最常见的使用Configuration的凡事。
Configuration cfg = new Configuration().configure();                               

Configuration可以说是hibernate的入口,在新建一个这个类的实例的时候,会在classpath里面查找hibernate.properties文件,如果该文件存在,则将该文件的内容添加到一个Properties的实例里面,如果不存在,将打印信息
hibernate.properties not found
然后是将所有系统环境变量(System.getProperties())也添加到GLOBAL_PROPERTIES
如果hibernate.properties文件存在,系统还会验证一下这个文件配置的有效性,对于一些已经不支持的配置,系统将提出警告(注意不是异常)。有关的代码请参考Environment类的static{}。

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/>元素配置的信息,正如前面的例子
  <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里面对应的配置
hibernate.connection.url
hibernate.connection.driver_class
hibernate.connection.username
hibernate.connection.password
hibernate.dialect

注意,如果在hibernate.cfg.xml的<property/>配置的name没有以hibernate开头,那么configure()内部会自动在前面添加hibernate.

然后configure()会访问以下几个元素的内容
<mapping/>
<jcs-class-cache/>
<jcs-collection-cache/>
<collection-cache/>

我们常用的是<mapping/>,我们必须通过配置<mapping/>,configure()才能访问到我们定义的java对象和关系数据库表的映射文件
如:
<mapping resource="cat.hbm.xml"/>

Configuration的config方法还支持带参数的访问方式,你可以指定文件的位置,而不是使用默认的classpath下面的hibernate.cfg.xml这种方式。
例如
Configuration cfg = new Configuration().configure("myexample.xml");


至此对hibernate配置文件hibernate.properties和hibernate.cfg.xml的默认的加载过程就比较清楚了。


hibernate还提供了一系列方法用来定制hibernate的加载配置文件的过程,让你的应用更加灵活,我们常用的是以下几种
addProperties(Element)
addProperties(Properties)
setProperties(Properties)
setProperty(String, String)
以上几个方法用来添加或更改配置项,除了默认的hibernate.properties之外,你还可以提供多个properties配置文件,根据不同的情况使用不同的配置文件,例如:
Properties properties = Properties.load("my.properties");
Configuration config = new Configuration().setProperties(properties).configure();

addClass(Class)
addFile(File)
addFile(String)
前面我们已经讲了,configure()方法通过访问hibernate.cfg.xml的<mapping/>元素来加载我们提供的java类和关系数据库表的映射文件,还有一个方法是直接使用addClass()方法,直接加载对应的映射文件,或者干脆只用addFile,直接指定映射文件,这种方法的好处是如果你的映射文件很多,在某个应用中用不了那么多,可以通过这个方法选择需要的就可以了。这种情况下,我们需要在classpath里面提供hibernate.properties文件
Configuration config = new Configuration().addClass(Cat.class);

应用举例
在刚开始学习hibernate的时候,对于hibernate的hbm映射文件里的各种配置参数没有一个感性的认识,例如inverse="true",lazy="true"这样的配置参数,不通过实践是无法体会到其作用的,传统的方法就是每需要测试一种参数的效果就更改相应的配置文件,然后运行我们的测试来观察结果,如果能够灵活的运用Configuration提供的定制配置的方法,我们可以提供多个配置文件,每个配置文件里面有不同的配置参数,再加上我们提供的测试案例就方便多了。

例如针对ono-to-many和many-to-one映射关系,我们想测试在one-to-many方使用inverse="false"和inverse="true"的不同效果,假设已经正确的配置好了hibernate.properties,
我们需要提供两个不同的hbm.xml文件,假设分别名为bidirect.inverse.false.hbm.xml和bidirect.inverse.true.hbm.xml。

然后需要写两个不同的测试案例,分别针对两个不同的配置文件进行测试就可以了,这样的好处是,不用针对不同的测试案例修改配置文件,特别是在集成测试的时候,一切都是自动化的,如果每测试一个案例就需要更改配置文件,这肯定是一个失败的测试。

代码模板如下:


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;

/**
 * test false inverse
 */
public class FalseInverseTest extends TestCase {

    private Session session;
    private Transaction tx;

    protected void setUp() throws Exception {
        Configuration cfg = new Configuration().addFile("bidirect.inverse.false.hbm.xml");
        session = cfg.buildSessionFactory().openSession();
        tx = session.beginTransaction();
    }

    protected void tearDown() throws Exception {
  tx.commit();
  session.close();
    }
   
    public void testLogic()  {
     //writing ur test code here.
    }

}

 

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;

/**
 * test true inverse
 */
public class TrueInverseTest extends TestCase {

    private Session session;
    private Transaction tx;

    protected void setUp() throws Exception {
        Configuration cfg = new Configuration().addFile("bidirect.inverse.true.hbm.xml");
        session = cfg.buildSessionFactory().openSession();
        tx = session.beginTransaction();
    }

    protected void tearDown() throws Exception {
  tx.commit();
  session.close();
    }
   
    public void testLogic()  {
     //writing ur test code here.
    }

}

 

 

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