在單元測試中應(yīng)用Hibernate配置文件
Hibernate是一個流行的開源對象關(guān)系映射工具,單元測試和持續(xù)集成的重要性也得到了廣泛的推廣和認(rèn)同,在采用了Hibernate的項目中如何保證測試的自動化和持續(xù)性呢?本文討論了Hibernate配置文件hibernate.properties和hibernate.cfg.xml的過程,以及怎么樣將hibernate提供的配置文件的訪問方法靈活運用到單元測試中。
介紹
Hibernate 是一個流行的開源對象關(guān)系映射工具,單元測試和持續(xù)集成的重要性也得到了廣泛的推廣和認(rèn)同,在采用了Hibernate的項目中如何保證測試的自動化和持續(xù)性呢?本文討論了Hibernate加載其配置文件hibernate.properties和hibernate.cfg.xml的過程,以及怎么樣將hibernate提供的配置文件的訪問方法靈活運用到單元測試中。注意:本文以hibernate2.1作為討論的基礎(chǔ),不保證本文的觀點適合于其他版本。
1.準(zhǔn)備
對于hibernate的初學(xué)者來說,***次使用hibernate的經(jīng)驗通常是:
1) 安裝配置好Hibernate,我們后面將%HIBERNATE_HOME%作為對Hibernate安裝目錄的引用,
2) 開始創(chuàng)建好自己的***個例子,例如hibernate手冊里面的類Cat,
3) 配置好hbm映射文件(例如Cat.hbm.xml,本文不討論這個文件內(nèi)配置項的含義)和數(shù)據(jù)庫(如hsqldb),
4) 在項目的classpath路徑下添加一個hibernate.cfg.xml文件,如下(***次使用hibernate最常見的配置內(nèi)容):
- <?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>
- ?。紁roperty name="connection.driver_class">org.hsqldb.jdbcDriver</property>
- ?。紁roperty name="connection.username">sa</property>
- ?。紁roperty name="connection.password"></property>
- ?。紁roperty name="dialect">net.sf.hibernate.dialect.HSQLDialect</property>
- ?。紁roperty name="hibernate.show_sql">false</property>
- <mapping resource="Cat.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
5) 然后還需要提供一個類來測試一下創(chuàng)建,更新,刪除和查詢Cat,對于熟悉JUnit的開發(fā)人員,可以創(chuàng)建一個單元測試類來進(jìn)行測試,如下:
- 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() {
- //請在此方法內(nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。
- }
- public void testUpdate() {
- //請在此方法內(nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。
- }
- public void testDelete() {
- //請在此方法內(nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。
- }
- public void testQuery() {
- //請在此方法內(nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。
- }
- }
2、new Configuration()都做了什么?
對于***次使用hibernate的新手來說,下面的這段代碼可以說是最常見的使用Configuration方式。
Configuration cfg = new Configuration().configure();
Configuration是hibernate的入口,在新建一個Configuration的實例的時候,hibernate會在classpath里面查找hibernate.properties文件,如果該文件存在,則將該文件的內(nèi)容加載到一個Properties的實例GLOBAL_PROPERTIES里面,如果不存在,將打印信息
hibernate.properties not found
然后是將所有系統(tǒng)環(huán)境變量(System.getProperties())也添加到GLOBAL_PROPERTIES里面( 注1)。如果hibernate.properties文件存在,系統(tǒng)還會驗證一下這個文件配置的有效性,對于一些已經(jīng)不支持的配置參數(shù),系統(tǒng)將打印警告信息。
3、configure()在做什么?
new Configuration()討論至此,下面討論configure()方法。
configure()方法默認(rèn)會在classpath下面尋找hibernate.cfg.xml文件,如果沒有找到該文件,系統(tǒng)會打印如下信息并拋出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里面對應(yīng)的配置,hibernate2.1發(fā)布包里面自帶的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()會順序訪問以下幾個元素的內(nèi)容
- <mapping>
- <jcs-class-cache>
- <jcs-collection-cache>
- <collection-cache>
其中<mapping>是必不可少的,必須通過配置<mapping>,configure()才能訪問到我們定義的Java對象和關(guān)系數(shù)據(jù)庫表的映射文件(hbm.xml),例如:
<mapping resource="Cat.hbm.xml"/>
通過以上的分析,我們對Hibernate配置文件hibernate.properties和hibernate.cfg.xml的默認(rèn)的加載過程就比較清楚了。
【編輯推薦】