轉(zhuǎn)載-
http://blog.csdn.net/ufoer23/archive/2005/02/24/299619.aspx2005 年 1 月
Hibernate 是一個(gè)流行的開源對象關(guān)系映射工具,單元測試和持續(xù)集成的重要性也得到了廣泛的推廣和認(rèn)同,在采用了Hibernate的項(xiàng)目中如何保證測試的自動(dòng)化和持續(xù)性呢?本文討論了Hibernate加載其配置文件hibernate.properties和hibernate.cfg.xml的過程,以及怎么樣將hibernate提供的配置文件的訪問方法靈活運(yùn)用到單元測試中。
1 介紹
Hibernate 是一個(gè)流行的開源對象關(guān)系映射工具,單元測試和持續(xù)集成的重要性也得到了廣泛的推廣和認(rèn)同,在采用了Hibernate的項(xiàng)目中如何保證測試的自動(dòng)化和持續(xù)性呢?本文討論了Hibernate加載其配置文件hibernate.properties和hibernate.cfg.xml的過程,以及怎么樣將hibernate提供的配置文件的訪問方法靈活運(yùn)用到單元測試中。注意:本文以hibernate2.1作為討論的基礎(chǔ),不保證本文的觀點(diǎn)適合于其他版本。
2 讀者
Java開發(fā)人員,要求熟悉JUnit和掌握Hibernate的基礎(chǔ)知識
3 內(nèi)容
3.1.準(zhǔn)備
對于hibernate的初學(xué)者來說,第一次使用hibernate的經(jīng)驗(yàn)通常是:
1, 安裝配置好Hibernate,我們后面將%HIBERNATE_HOME%作為對Hibernate安裝目錄的引用,
2, 開始創(chuàng)建好自己的第一個(gè)例子,例如hibernate手冊里面的類Cat,
3, 配置好hbm映射文件(例如Cat.hbm.xml,本文不討論這個(gè)文件內(nèi)配置項(xiàng)的含義)和數(shù)據(jù)庫(如hsqldb),
4, 在項(xiàng)目的classpath路徑下添加一個(gè)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>
<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, 然后還需要提供一個(gè)類來測試一下創(chuàng)建,更新,刪除和查詢Cat,對于熟悉JUnit的開發(fā)人員,可以創(chuàng)建一個(gè)單元測試類來進(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();////注意這一行,這是本文重點(diǎn)討論研究的地方。
session = cfg.buildSessionFactory().openSession();
tx = session.beginTransaction();
}
protected void tearDown() throws Exception {
tx.commit();
session.close();
}
public void testCreate() {
//請?jiān)诖朔椒▋?nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。
}
public void testUpdate() {
//請?jiān)诖朔椒▋?nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。
}
public void testDelete() {
//請?jiān)诖朔椒▋?nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。
}
public void testQuery() {
//請?jiān)诖朔椒▋?nèi)添加相關(guān)的代碼,本文不討論怎么樣使用Hibernate API。
}
}
|
3.2 new Configuration()都做了什么?
對于第一次使用hibernate的新手來說,下面的這段代碼可以說是最常見的使用Configuration方式。
Configuration cfg = new Configuration().configure();
|
Configuration是hibernate的入口,在新建一個(gè)Configuration的實(shí)例的時(shí)候,hibernate會在classpath里面查找hibernate.properties文件,如果該文件存在,則將該文件的內(nèi)容加載到一個(gè)Properties的實(shí)例GLOBAL_PROPERTIES里面,如果不存在,將打印信息
hibernate.properties not found
|
然后是將所有系統(tǒng)環(huán)境變量(System.getProperties())也添加到GLOBAL_PROPERTIES里面(注1)。如果hibernate.properties文件存在,系統(tǒng)還會驗(yàn)證一下這個(gè)文件配置的有效性,對于一些已經(jīng)不支持的配置參數(shù),系統(tǒng)將打印警告信息。
3.3. configure()在做什么?
new Configuration()討論至此,下面討論configure()方法。
configure()方法默認(rèn)會在classpath下面尋找hibernate.cfg.xml文件,如果沒有找到該文件,系統(tǒng)會打印如下信息并拋出HibernateException異常。
hibernate.cfg.xml not found
|
如果找到該文件,configure()方法會首先訪問< session-factory >,并獲取該元素的name屬性,如果非空,將用這個(gè)配置的值來覆蓋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()會順序訪問以下幾個(gè)元素的內(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)的加載過程就比較清楚了。
3.4 Configuration的其他用法
Configuration的configure ()方法還支持帶參數(shù)的訪問方式,你可以指定hbm.xml文件的位置,而不是使用默認(rèn)的classpath下面的hibernate.cfg.xml這種方式,例如:
Configuration cfg = new Configuration().configure("myexample.xml");
|
同時(shí)Configuration還提供了一系列方法用來定制hibernate的加載配置文件的過程,讓你的應(yīng)用更加靈活,常用的是以下幾種:
addProperties(Element)
addProperties(Properties)
setProperties(Properties)
setProperty(String, String)
|
通過以上幾個(gè)方法,除了使用默認(rèn)的hibernate.properties文件,你還可以提供多個(gè).properties配置文件,使用Hibernate的時(shí)候根據(jù)不同的情況使用不同的配置文件,例如:
Properties properties = Properties.load("my.properties");
Configuration config = new Configuration().setProperties(properties).configure();
|
除了指定.properties文件之外,還可以指定.hbm.xml文件,下面列出幾個(gè)常用的方法:
addClass(Class)
addFile(File)
addFile(String)
addURL(URL)
|
前面我們已經(jīng)講了,configure()方法默認(rèn)是通過訪問hibernate.cfg.xml的<mapping>元素來加載我們提供的.hbm.xml文件,上面列出的方法可以直接指定hbm.xml文件,例如addClass()方法可以直接通過指定class來加載對應(yīng)的映射文件,hibernate會將提供的class的全名(包括package)自動(dòng)轉(zhuǎn)化為文件路徑,如net.sf.hibernate.examples.quickstart.Cat.class對應(yīng)了net/sf/hibernate/examples/quickstart/Cat.hbm.xml,還可以用addFile方法直接指定映射文件。
例一:
Configuration config = new Configuration().addClass(Cat.class);
|
例二:
Configuration config = new Configuration().addURL(Configuration.class.getResource ("Cat.hbm.xml"));
|
例三:
Configuration config = new Configuration().addFile("Cat.hbm.xml");
|
3.5 總結(jié)
Configuration提供的這些方法的好處如下:
1. 一個(gè)應(yīng)用中往往有很多.hbm.xml映射文件,開發(fā)的過程中如果只是為了測試某個(gè)或幾個(gè)Java PO(Persistence Object),我們沒有必要把所有的.hbm.xml都加載到內(nèi)存,這樣可以通過addClass或者addFile直接,顯得非常靈活。
2. 學(xué)習(xí)Hibernate的過程中,往往需要通過練習(xí)來體會Hibernate提供的各種特征,而很多特征是需要修改配置文件的,如果要觀察相同的代碼在不同的特征下的表現(xiàn),就需要手工改配置文件,這樣太麻煩了,而且容易出錯(cuò),我們可以提供多個(gè)配置文件,每個(gè)配置文件針對需要的特征而配置,這樣我們在調(diào)用程序的時(shí)候,把不同的配置文件作為參數(shù)傳遞進(jìn)去,而程序代碼里面使用setProperties和addFile指定傳入的配置文件參數(shù)就可以了。
3. 在單元測試中,特別是在集成測試?yán)锩妫麄€(gè)過程是自動(dòng)化的,我們不能手工干預(yù)測試過程,往往需要準(zhǔn)備多個(gè)配置文件針對不同的測試案例,這個(gè)時(shí)候setProperties和addFile方法就顯得特別有用了,在不同的測試案例中用這些方法來指定相應(yīng)的配置文件,這樣就可以做到自動(dòng)化測試,保證了持續(xù)性。
3.6 應(yīng)用舉例
在剛開始學(xué)習(xí)hibernate的時(shí)候,對于hibernate的hbm映射文件里的各種配置參數(shù)沒有一個(gè)感性的認(rèn)識,例如inverse="true",lazy="true"這樣的配置參數(shù),不通過實(shí)踐是無法體會到其作用的,傳統(tǒng)的方法就是每需要測試一種參數(shù)的效果就更改相應(yīng)的配置文件,然后運(yùn)行測試來觀察結(jié)果,如果能夠靈活的運(yùn)用Configuration提供的定制配置的方法,我們可以提供多個(gè)配置文件,每個(gè)配置文件里面有不同的配置參數(shù),配合相應(yīng)的測試案例就方便多了。
例如針對ono-to-many和many-to-one的雙向關(guān)聯(lián)的映射關(guān)系,我們想測試在one-to-many一方使用inverse="false"和inverse="true"的不同效果,假設(shè)已經(jīng)正確的配置好了hibernate.properties,那么還需要提供兩個(gè)不同的hbm.xml文件,假設(shè)分別名為bidirect.inverse.false.hbm.xml和bidirect.inverse.true.hbm.xml。
然后需要寫兩個(gè)不同的測試案例,分別針對兩個(gè)不同的配置文件進(jìn)行測試就可以了,這樣的好處是,不用針對不同的測試案例修改配置文件,特別是在集成測試的時(shí)候,一切都是自動(dòng)化的,如果每測試一個(gè)案例就需要手工去更改配置文件,這肯定是一個(gè)失敗的測試。
代碼模板如下:
FalseInverseTest.java文件
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() {
//在此編寫測試代碼
}
}
|
TrueInverseTest.java文件
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() {
//在此編寫測試代碼
}
}
|
結(jié)束語
通過對Hibernate默認(rèn)的配置文件的加載順序和Hibernate提供的加載配置文件的方法的討論,我們對在使用到Hibernate的項(xiàng)目的單元測試中使用多個(gè)Hibernate配置文件有了比較清楚的認(rèn)識。
持續(xù)集成中的測試的特征是自動(dòng)化和持續(xù)性,不能手工干預(yù)其過程,在使用到Hibernate的項(xiàng)目如果要實(shí)現(xiàn)持續(xù)集成,就要為不同的測試案例提供不同的配置文件,而不是針對不同的測試案例進(jìn)行手工調(diào)整,因此,在使用到Hibernate的項(xiàng)目中靈活的運(yùn)用多配置文件,可以提高測試的效率,保證自動(dòng)化和持續(xù)性。
注1:有關(guān)的代碼請參考Environment類的static{}。
注2:如果在hibernate.cfg.xml的<property/>配置的name沒有以hibernate開頭,那么configure()內(nèi)部會自動(dòng)在前面添加hibernate,例如connection.url,hibernate會自動(dòng)將其轉(zhuǎn)化為hibernate.connection.url。
參考資料
posted on 2006-08-31 09:47
阿成 閱讀(228)
評論(0) 編輯 收藏 所屬分類:
Hibernate