溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在單元測試中如何應(yīng)用Hibernate配置文件

發(fā)布時間:2021-12-06 09:15:43 來源:億速云 閱讀:133 作者:小新 欄目:編程語言

這篇文章主要介紹了在單元測試中如何應(yīng)用Hibernate配置文件,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

Hibernate是一個流行的開源對象關(guān)系映射工具,單元測試和持續(xù)集成的重要性也得到了廣泛的推廣和認同,在采用了Hibernate的項目中如何保證測試的自動化和持續(xù)性呢?本文討論了Hibernate配置文件hibernate.properties和hibernate.cfg.xml的過程,以及怎么樣將hibernate提供的配置文件的訪問方法靈活運用到單元測試中。

介紹

Hibernate 是一個流行的開源對象關(guān)系映射工具,單元測試和持續(xù)集成的重要性也得到了廣泛的推廣和認同,在采用了Hibernate的項目中如何保證測試的自動化和持續(xù)性呢?本文討論了Hibernate加載其配置文件hibernate.properties和hibernate.cfg.xml的過程,以及怎么樣將hibernate提供的配置文件的訪問方法靈活運用到單元測試中。注意:本文以hibernate2.1作為討論的基礎(chǔ),不保證本文的觀點適合于其他版本。

1.準備

對于hibernate的初學者來說,***次使用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>   ?。紁roperty 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>   ?。糾apping resource="Cat.hbm.xml"/>   </session-factory>  </hibernate-configuration>

5) 然后還需要提供一個類來測試一下創(chuàng)建,更新,刪除和查詢Cat,對于熟悉JUnit的開發(fā)人員,可以創(chuàng)建一個單元測試類來進行測試,如下:

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()方法默認會在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的默認的加載過程就比較清楚了。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“在單元測試中如何應(yīng)用Hibernate配置文件”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI