最近準(zhǔn)備學(xué)NHibernate,于是網(wǎng)上狂找,看來有不少文章,但仔細(xì)看就會明白,搞來搞去,其實(shí)就那么幾篇大同小異的文章,但還是終于在我們的博客上找到篇好點(diǎn)的,就是下面那篇了,我也不明白是哪為高手寫的了,因?yàn)檗D(zhuǎn)載的太多了,有點(diǎn)糊涂了,也許是張老三的作品吧,但不知道是沒有把相關(guān)源碼共享出來,還是我沒有找到,對一個新手來說,就一些文字真的有點(diǎn)困難啊,何況下面的文章可能由于手誤,還是某些原因,我是調(diào)試很久才調(diào)得出來,也許是我的NHibernate,和NUIT的版本跟原文的不同,我用的NHibernate 0.9.1.0,NUnit2.2。順便把NUnit也學(xué)了,還是蠻不錯的了。下面的文章也許是我有些修改的了,望給想學(xué)NHibernate的稍有補(bǔ)益。
http://files.cnblogs.com/sifang2004/NHibernateTest.rar
NHibernate 博客園專題之一
(原文標(biāo)題,本人尊重原作,故此保留,可以點(diǎn)擊看到原文)
http://www.cnblogs.com/wxx/archive/2005/07/17/194337.html
(因?yàn)樘闊蚁旅鎸υ乃龅男薷模粫儆姓f明,也再次感謝本文原作的努力,隨著我學(xué)習(xí)的深入,我也會不斷修改下文)
本文約定:
1. Nhibernate簡寫為NHB;
2. 本文例子的開發(fā)平臺為win2000xp+sp2, sql server2000, Nhibernate0.9.1.0;
3. 使用SQL Server自帶的羅斯文商貿(mào)數(shù)據(jù)庫(Northwind),中文版;
4. 本文例子是基于測試驅(qū)動開發(fā)(TDD)的,因此建議使用NUnit2.2和Log4Net (如果你不熟悉NUnit,不要緊啊,趁此機(jī)會學(xué)習(xí)點(diǎn)簡單的應(yīng)用);
一 NHB簡介
NHB是基于ms.net的O/R Mapping持久框架,它從基于Java的Hibernate項(xiàng)目移植而來。O/R Mapping就是把對象到映射關(guān)系數(shù)據(jù)庫的記錄,簡單的說就是能實(shí)現(xiàn)把一個對象存儲為數(shù)據(jù)表中的一條記錄和由一條記錄創(chuàng)建一個相應(yīng)的對象,數(shù)據(jù)表中的數(shù)據(jù)就是對象的屬性。
那么為什么要使用O/R Mapping?它與傳統(tǒng)的DataSet/DataTable又有什么不同了?
首先是設(shè)計上的不同,當(dāng)使用O/R Mapping時,更多的是從對象的角度來設(shè)計程序,而把數(shù)據(jù)(對象的屬性)存儲的細(xì)節(jié)放在后面, 可以完全采用面向?qū)ο?OO)的方式來設(shè)計,而在使用DataSet/DataTable時,它只是存放數(shù)據(jù)的對象,看起來更像一個數(shù)據(jù)表,不能直觀的表達(dá)業(yè)務(wù)概念。
二 NHB中主要接口的介紹
ISession
ISession是面向用戶的主要接口,主要用于對象持久化,數(shù)據(jù)加載等操作,支持?jǐn)?shù)據(jù)庫事務(wù),它隱藏了NHB內(nèi)部復(fù)雜的實(shí)現(xiàn)細(xì)節(jié),ISession由ISessionFactory創(chuàng)建。
ISessionFactory
ISessionFactory是NHB內(nèi)部的核心類,它維護(hù)到持久機(jī)制(數(shù)據(jù)庫)的連接并對它們進(jìn)行管理,同時還會保存所有持久對象的映射信息。
ISessionFactory由Configuration創(chuàng)建,因?yàn)閯?chuàng)建ISessionFactory的開銷非常大(需要加載映射信息),所以這個對象一般使用Singleton(單例)模式。
ITransaction
ITransaction是NHB的事務(wù)處理接口,它只是簡單的封裝了底層的數(shù)據(jù)庫事務(wù)。
事務(wù)必須由ISession來啟動。
ICriteria
ICriteria是Expression(表達(dá)式)數(shù)據(jù)加載接口,Expression是一個關(guān)系表達(dá)式組合,通過它能產(chǎn)生SQL語句的Where部分, 用戶需要通過ISession來間接調(diào)用它。
IQuery
IQuery是HQL數(shù)據(jù)加載接口,HQL(Hibernate Query Language)是NHB專用的面向?qū)ο蟮臄?shù)據(jù)查詢語言,它與數(shù)據(jù)庫的SQL有些類似,但功能更強(qiáng)大!同ICriteria一樣,也需要通過ISession來間接調(diào)用它。
三 持久化操作
1. 會話和會話工廠
要進(jìn)行持久化操作,必須先取得ISession和ISessionFactory,我們用一個Sessions類來封裝它們, Sessions類的屬性和方法都是靜態(tài)的,它有一個Factory屬性, 用于返回ISessionFactory, 有一個GetSession方法,用于取得一個新的ISession。
測試類代碼如下:
using System;
using NUnit.Framework;
using NHibernate;
namespace NHibernateTest
{
///
/// SessionsFixture 的摘要說明。
///
///
[TestFixture]
public class SessionsFixture
{
public SessionsFixture()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
[Test] // 測試能否取得NHB會話工廠。
public void FactoryTest()
{
ISessionFactory sf = Sessions.Factory;
Assert.IsNotNull( sf, "get sessionfactory fail!" );
}
[Test] // 測試能否取得NHB會話。
public void GetSessionTest()
{
ISession s = Sessions.GetSession();
Assert.IsNotNull( s, "get session fail!" );
}
}
}
現(xiàn)在還沒寫Sessions類,將不能通過編譯! 下面我們來實(shí)現(xiàn)Sessions類.
using System;
using NHibernate;
using System.Reflection;
namespace NHibernateTest
{
///
/// Sessions 的摘要說明。
///
public class Sessions
{
private static readonly object lockObj = new object();
private static ISessionFactory _factory;
public Sessions()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
public static ISessionFactory Factory
{
get
{
if ( _factory == null )
{
lock ( lockObj )
{
if ( _factory == null )
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration ();
cfg.AddAssembly( Assembly.GetExecutingAssembly() );
_factory = cfg.BuildSessionFactory();
}
}
}
return _factory;
}
}
public static ISession GetSession()
{
return Factory.OpenSession();
}
}
}
OK,現(xiàn)在編譯可以通過了,啟動NUnit并選擇生成的文件NHibernateTest.exe,運(yùn)行測試。我們得到了紅色的條,出錯了!原來還沒有加入NHibernate的配置信息(當(dāng)使用NHibernate時,需要在項(xiàng)目的配置文件中加入NHibernate的配置信息。關(guān)于配置信息,在下面有說明)。在項(xiàng)目的配置文件App.Config(如沒有請自行創(chuàng)建一個)中加入以下內(nèi)容.
稍做解釋,下面的配置文件主要是配置了NHibernate和log4net,例如配置了NHibernate連接數(shù)據(jù)庫的連接字符串;log4net把日志記到哪個文件中,本例就是"log.txt"。每次寫日志向文件中追加,而不是重寫
xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
< SPAN>configSections>
<nhibernate>
<add
key="hibernate.show_sql"
value="true"
/>
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add
key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"
/>
<add
key="hibernate.connection.connection_string"
value="Server=127.0.0.1;initial catalog=Northwind;User id =golinjoe;Password=2525775"
/>
< SPAN>nhibernate>
<log4net>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
< SPAN>layout>
< SPAN>appender>
<root>
<priority value="ALL" />
<appender-ref ref="rollingFile" />
< SPAN>root>
< SPAN>log4net>
< SPAN>configuration>
再次運(yùn)行測試,就可以看見綠色的條了。
在取得會話工廠的代碼中,我使用了如下代碼:
if ( _factory == null ) {
lock ( lockObj ) {
if ( _factory == null ) {
// build sessionfactory code;
}
}
}
這是一個典型的double lock方式,用來產(chǎn)生線程安全的Singletion(單例)對象。
2. 基本CRUD操作
在很多介紹NHB的文章,包括NHB帶的測試用例中,業(yè)務(wù)對象只是做為一個數(shù)據(jù)實(shí)體存在的,它沒有任何操作!這在java中是比較典型的作法。
而我希望我們的業(yè)務(wù)對象自身就能完成基本的Create/Retrieve/Update/Delete,即CRUD操作,
在羅斯文商貿(mào)應(yīng)用中,存在客戶(customer)業(yè)務(wù)對象,先來為它建立一個測試用例,
using System;
using NHibernate;
using NUnit.Framework;
namespace NHibernateTest
{
///
/// CustomerFixture 的摘要說明。
///
[TestFixture]
public class CustomerFixture
{
public CustomerFixture()
{}
[Test] // 測試Customer對象的CRUD操作。
public void TestCRUD()
{
Customer c = new Customer();
c.CustomerId = "test";
c.CompanyName = "company name";
c.ContactName = "contact name";
c.Address = "address";
c.Create(); // 測試 insert操作,
Customer c2 = new Customer( c.CustomerId ); // 測試 retrieve 操作.
Assert.AreEqual( c2.CompanyName, "company name", "save companyname fail! " );
c2.CompanyName = "update name";
c2.Update(); // 測試 update 操作.
Customer c3 = new Customer( c.CustomerId );
Assert.AreEqual( c3.CompanyName, "update name", "update companyname fail! " );
c3.Delete(); // 測試 delete 操作.
}
}
}
接下來創(chuàng)建Customer業(yè)務(wù)類:
using System;
namespace NHibernateTest
{
///
/// Customer 的摘要說明。
///
public class Customer : BizObject
{
public Customer() { }
public Customer( string existingId ) : base( existingId ) { }
#region persistent properties.
private string _customerId = string.Empty;
private string _companyName = string.Empty;
private string _contactName = string.Empty;
private string _contactTitle = string.Empty;
private string _address = string.Empty;
private string _city = string.Empty;
private string _region = string.Empty;
private string _postalCode = string.Empty;
private string _country = string.Empty;
private string _phone = string.Empty;
private string _fax = string.Empty;
public string CustomerId
{
get { return _customerId; }
set { _customerId = value; }
}
public string CompanyName
{
get { return _companyName; }
set { _companyName = value; }
}
public string ContactName
{
get { return _contactName; }
set { _contactName = value; }
}
public string ContactTitle
{
get { return _contactTitle; }
set { _contactTitle = value; }
}
public string Address
{
get { return _address; }
set { _address = value; }
}
public string City
{
get { return _city; }
set { _city = value; }
}
public string Region
{
get { return _region; }
set { _region = value; }
}
public string PostalCode
{
get { return _postalCode; }
set { _postalCode = value; }
}
public string Country
{
get { return _country; }
set { _country = value; }
}
public string Phone
{
get { return _phone; }
set { _phone = value; }
}
public string Fax
{
get { return _fax; }
set { _fax = value; }
}
#endregion
}
}
在Customer類中,沒有實(shí)現(xiàn)CRUD操作,這些操作在業(yè)務(wù)對象基類BizObject中實(shí)現(xiàn),代碼如下:
using System;
namespace NHibernateTest
{
///
/// BizObject 的摘要說明。
///
public class BizObject
{
public BizObject() { }
public BizObject( object existingId )
{
ObjectBroker.Load( this, existingId );
}
public virtual void Create()
{
ObjectBroker.Create( this );
}
public virtual void Update()
{
ObjectBroker.Update( this );
}
public virtual void Delete()
{
ObjectBroker.Delete( this );
}
}
}
BizObject簡單的將數(shù)據(jù)操作轉(zhuǎn)發(fā)至ObjectBroker類, 目的是為了降低業(yè)務(wù)層和NHB之間的耦合, 以利于持久層間的移植。
using System;
using NHibernate;
using NHibernateTest;
namespace NHibernateTest
{
///
/// ObjectBroker 的摘要說明。
///
public class ObjectBroker
{
private ObjectBroker() { }
public static void Load( object obj, object id )
{
ISession s = Sessions.GetSession();
try
{
s.Load( obj, id );
}
finally
{
s.Close();
}
}
public static void Create( object obj )
{
ISession s = Sessions.GetSession();
ITransaction trans = null;
try
{
trans = s.BeginTransaction();
s.Save( obj );
trans.Commit();
}
finally
{
s.Close();
}
}
public static void Update( object obj )
{
ISession s = Sessions.GetSession();
ITransaction trans = null;
try
{
trans = s.BeginTransaction();
s.Update( obj );
trans.Commit();
}
finally
{
s.Close();
}
}
public static void Delete( object obj )
{
ISession s = Sessions.GetSession();
ITransaction trans = null;
try
{
trans = s.BeginTransaction();
s.Delete( obj );
trans.Commit();
}
finally
{
s.Close();
}
}
}
}
ObjectBroker對ISession進(jìn)行了必要的封裝,通過ISession,就可以簡單的完成對象的CRUD操作了。
編譯并運(yùn)行測試,CustomerFixture的TestCRUD操作還是不能通過! 異常信息為:
NHibernateTest.CustomerFixture.TestCRUD : NHibernate.MappingException : Unknown entity class: NHibernateTest.Customer
顯然,是因?yàn)槲覀冞€沒有為Customer對象編寫映射文件,而導(dǎo)致NHB不能對Customer對象進(jìn)行持久化操作。
Customer對象的映射文件(Customer.hbm.xml)內(nèi)容如下:
xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateTest.Customer,NHibernateTest" table="Customers">
<id name="CustomerId" column="customerId" type="String" unsaved-value="">
<generator class="assigned"/>
< SPAN>id>
<property name="CompanyName" column="companyName" type="String" />
<property name="ContactName" column="contactName" type="String" />
<property name="ContactTitle" column="contactTitle" type="String" />
<property name="Address" column="address" type="String" />
<property name="City" column="city" type="String" />
<property name="Region" column="region" type="String" />
<property name="PostalCode" column="postalCode" type="String" />
<property name="Country" column="country" type="String" />
<property name="Phone" column="phone" type="String" />
<property name="Fax" column="fax" type="String" />
< SPAN>class>
< SPAN>hibernate-mapping>
這個映射文件算是NHB中較為簡單的了。
class的name指定業(yè)務(wù)對象全名及其所在程序集,table指定數(shù)據(jù)表的名稱;
id用于指定一個對象標(biāo)識符(數(shù)據(jù)表中的主鍵)及其產(chǎn)生的方式, 常用的主健產(chǎn)生方式有自增型(identity)和賦值型(assigned),這里使用了assigned,需要注意的是unsaved-value屬性,它指定對象沒有持久化時的Id值,主要用于SaveOrUpdate操作;
property用于指定其它映射的數(shù)據(jù)列;
在id和property中,name指定屬性名稱,column指定數(shù)據(jù)列的名稱,type指定屬性類型,注意這里的類型是NHB中的類型,而不是.NET或數(shù)據(jù)庫中的數(shù)據(jù)類型。
另外,對象映射文件名稱請按”對象名.hbm.xml”的規(guī)范來命名, 最后在映射文件的屬性中把操作改為"嵌入的資源"(特別注意這點(diǎn)很重要,我就是沒有注意到這。郁悶了很久啊!!!)。
現(xiàn)在重新編譯程序并運(yùn)行測試,就能看到綠條了!
因?yàn)镻roduct對象將在后面的案例中多次使用,在這里按與Customer相同的步驟創(chuàng)建它。
// Product單元測試
using System;
using NUnit.Framework;
namespace NHibernateTest
{
///
/// ProductFixture 的摘要說明。
///
[TestFixture]
public class ProductFixture
{
public ProductFixture() { }
[Test] // 測試Product對象的CRUD操作。
public void TestCRUD()
{
Category c = null;
try
{
c = new Category();
c.CategoryName = "test";
c.Create();
Product p = new Product();
p.ProductName = "test";
p.Category = c;
p.SupplierId = 3;
p.QuantityPerUnit = "1箱10只";
p.UnitPrice = 10.5M;
p.Create();
Product p2 = new Product( p.ProductId );
p2.UnitPrice = 15.8M;
p2.Update();
Product p3 = new Product( p.ProductId );
Assert.AreEqual( p3.UnitPrice, 15.8M, "update fail! " );
p3.Delete();
}
finally
{
if ( c != null && c.CategoryId > 0 ) c.Delete();
}
}
}
}
// Product對象(注意,注釋部分并不是無意義的,是在不同的情況下做測試用的,本文中的注釋都基本如此)
using System;
namespace NHibernateTest
{
///
/// Product 的摘要說明。
///
public class Product : BizObject
{
public Product() : base() { }
public Product( int existingId ) : base( existingId ) { }
#region persistent properties
private int _productId = 0;
private string _productName = string.Empty;
private int _supplierId = 0; // 應(yīng)使用many-to-one, 需要重構(gòu)。
// private int _categoryId = 0; // 應(yīng)使用many-to-one, 需要重構(gòu)。
private Category _category;
private string _quantityPerUnit = string.Empty;
private decimal _unitPrice = 0;
private int _unitsInStock = 0;
private int _unitsOnOrder = 0;
private int _reorderLevel = 0;
private bool _discontinued = false;
public int ProductId
{
get { return _productId; }
set { _productId = value; }
}
public string ProductName
{
get { return _productName; }
set { _productName = value; }
}
public int SupplierId
{
get { return _supplierId; }
set { _supplierId = value; }
}
// public int CategoryId
// {
// get { return _categoryId; }
// set { _categoryId = value; }
// }
public Category Category
{
get { return _category; }
set { _category = value; }
}
public string QuantityPerUnit
{
get { return _quantityPerUnit; }
set { _quantityPerUnit = value; }
}
public decimal UnitPrice
{
get { return _unitPrice; }
set { _unitPrice = value; }
}
public int UnitsInStock
{
get { return _unitsInStock; }
set { _unitsInStock = value; }
}
public int UnitsOnOrder
{
get { return _unitsOnOrder; }
set { _unitsOnOrder = value; }
}
public int ReorderLevel
{
get { return _reorderLevel; }
set { _reorderLevel = value; }
}
public bool Discontinued
{
get { return _discontinued; }
set { _discontinued = value; }
}
#endregion
}
}
// 映射文件
xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateTest.Product, NHibernateTest" table="Products">
<id name="ProductId" column="productId" type="Int32" unsaved-value="0">
<generator class="identity"/>
< SPAN>id>
<property name="ProductName" column="ProductName" type="String" />
<property name="QuantityPerUnit" column="QuantityPerUnit" type="String" />
<property name="UnitPrice" column="unitPrice" type="Decimal" />
<property name="UnitsInStock" column="unitsInStock" type="Int32" />
<property name="UnitsOnOrder" column="unitsOnOrder" type="Int32" />
<property name="ReorderLevel" column="reorderLevel" type="Int32" />
<property name="Discontinued" column="discontinued" type="Boolean" />
<property name="SupplierId" column="SupplierId" type="Int32" />
<many-to-one name="Category" column="categoryId" unique="true" class="NHibernateTest.Category, NHibernateTest" />
< SPAN>class>
< SPAN>hibernate-mapping>
編譯并運(yùn)行測試,檢查錯誤直到單元測試通過。
注意:因?yàn)樵跀?shù)據(jù)庫中,products表與categories表、suppliers表有外鍵約束,必須先刪除這兩個約束,product測試用例才能通過,后面我們再加上這兩個約束。
現(xiàn)在我們已經(jīng)掌握了NHB的基本CRUD操作了,整個過程應(yīng)該說是比較簡單吧。呵呵,不再需要使用Connection、DataAdapter、DataSet/DataReader之類的對象了,下面繼續(xù)學(xué)習(xí)NHB中更為復(fù)雜的映射關(guān)系。
3. one-to-one
一對一是一種常見的數(shù)據(jù)模型,它有兩種情況:一種是主鍵(PrimaryKey)關(guān)聯(lián);另一種是外健(ForeignKey)關(guān)聯(lián),在使用外健的時候要保證其唯一性。
在主鍵關(guān)聯(lián)的情況下, 必須有一個主鍵是根據(jù)別一個主鍵而來的。NHB是通過一種特殊的方式來處理這種情況的, 要注意兩個主健名稱必須同名,而外健關(guān)聯(lián)需要在one-to-one配置中定義一個property-ref屬性, 這個屬性在當(dāng)前版本的NHB(這是指的是nhibernate 0.5.3,當(dāng)前的版本是什么情況我現(xiàn)在也沒有時間了解)中還沒有實(shí)現(xiàn)。
在羅斯文商貿(mào)應(yīng)用,不需要使用one-to-one映射,這里先不對其進(jìn)行講解,如欲了解one-to-one方面的應(yīng)用,請參考我網(wǎng)站上的文章。
4. many-to-one
many-to-one是描述多對一的一種數(shù)據(jù)模型,它指定many一方是不能獨(dú)立存在的,我個人認(rèn)為many-to-one是NHB中保證數(shù)據(jù)有效性的最有用的一種映射,通過使用many-to-one能有效的防治孤兒記錄被寫入到數(shù)據(jù)表中。
在羅斯文商貿(mào)數(shù)據(jù)中,Product(產(chǎn)品)與Category(類別)是多對一的關(guān)系。下面我們來處理這一映射關(guān)系,
首先要讓Category能實(shí)現(xiàn)基本的CRUD操作,步驟同上, 這里只列出測試用例,類和映射文件請參照上面的方式創(chuàng)建。
[TestFixture]
public class CategoryFixture
{
public CategoryFixture()
{
}
[Test] // 測試基本的CRUD操作。
public void TestCRUD()
{
Category c = new Category();
c.CategoryName = "category1";
c.Description = "category1";
c.Create();
Category c2 = new Category(c.CategoryId);
c2.CategoryName = "testupdated";
c2.Update();
Category c3 = new Category( c.CategoryId);
Assert.AreEqual( c3.CategoryName, "testupdated", "update fail! " );
c3.Delete();
}
}
上面的測試用例通過后,接著修改Product的各部分。
Product測試用例修改如下:
[Test] // 測試Product對象的CRUD操作。
public void TestCRUD()
{
Category c = null;
try
{
c = new Category();
c.CategoryName = "test";
c.Create();
Product p = new Product();
p.ProductName = "test";
p.Category = c;
p.SupplierId = 3;
p.QuantityPerUnit = "1箱10只";
p.UnitPrice = 10.5M;
p.Create();
Product p2 = new Product( p.ProductId );
p2.UnitPrice = 15.8M;
p2.Update();
Product p3 = new Product( p.ProductId );
Assert.AreEqual( p3.UnitPrice, 15.8M, "update fail! " );
p3.Delete();
}
finally
{
if ( c != null && c.CategoryId > 0 ) c.Delete();
}
}
Product類做如下修改:
1. 刪除categoryId 字段和CategoryId屬性;
2. 加入以下代碼:
public Category Category {
get { return _category; }
set { _category = value; }
}
private Category _category;
Product映射文件做如下修改:
class="NHibernateTest.Business.Category, NHibernateTest" />
這里用到了一個many-to-one標(biāo)簽,用于指定與one的一方進(jìn)行關(guān)聯(lián)的對象信息。
name指定one一方在對象中的名稱;
column指定映射數(shù)據(jù)列的名稱;
unique指定one一方是唯一的;
class 指定one一方類的全名,包括程序集名稱;
重新編譯程序,運(yùn)行測試用例, 看到綠條了嗎?沒有就根據(jù)異常去除錯吧!我已經(jīng)看到綠條了。:)
聲明: 因?yàn)檫^多的many-to-one使后面的測試代碼變得異常龐大(創(chuàng)建對象時要創(chuàng)建one一方的類),所以在后面的代碼中,我假定Product對象是沒有實(shí)現(xiàn)任何many-to-one映射的。如果不怕麻煩,請自行修改后面測試用例。
5. one-to-many
一對多也是一種常見的數(shù)據(jù)模型,在按范式設(shè)計的數(shù)據(jù)庫中隨處可見。在NHB中通過one-to-many可以非常方便的處理這種模型,同時NHB還提供了級聯(lián)更新和刪除的功能,以保證數(shù)據(jù)完整性。
在羅斯文商貿(mào)案例中,Customer與Order(訂單)、Order和OrderItem(訂單項(xiàng)目)就是一對多的關(guān)系,值得注意的是,并不是所有一對多關(guān)系都應(yīng)該在NHB中實(shí)現(xiàn),這取決于實(shí)際的需求情況,無謂的使用one-to-many映射只會降低NHB的使用性能。
下面先讓Order和OrderItem對象能單獨(dú)的完成CRUD操作,按上面處理Customer的方法來創(chuàng)建測試用例和類,
using System;
using NUnit.Framework;
using System.Collections;
namespace NHibernateTest
{
///
/// OrderFixture 的摘要說明。
///
[TestFixture]
public class OrderFixture
{
public OrderFixture()
{
}
[Test] // 測試Order對象的CRUD操作。
public void TestCRUD()
{
Order o = new Order();
o.CustomerId = "ALFKI";
o.EmployeeId = 1;
o.RequiredDate = new DateTime(2005,9,10);
o.ShippedDate = new DateTime(2005,8,28);
o.ShipVia = 1;
o.Freight = 20.5M;
o.ShipName = "test name";
o.ShipAddress = "test address";
o.Create();
Order o2 = new Order( o.OrderId );
o2.Freight = 21.5M;
o2.ShipAddress = "update address";
o2.Update();
Order o3 = new Order( o.OrderId );
Assert.AreEqual( o3.Freight, 21.5M, "update order fail! " );
Assert.AreEqual( o3.ShipAddress, "update address", "update order fail! " );
o3.Delete();
}
[Test] // 測試OrderItem對象的CRUD操作。
public void TestItemCRUD()
{
Order o = null;
Product p = null;
try
{
o = new Order();
o.RequiredDate = new DateTime(2005,9,10);
o.ShippedDate = new DateTime(2005,8,28);
o.CustomerId = "ALFKI";
o.EmployeeId = 1;
o.ShipVia = 1;
o.Freight = 20.5M;
o.ShipName = "test name";
o.ShipAddress = "test address";
o.Create();
p = new Product();
p.ProductName = "test";
p.UnitPrice = 11.1M;
p.SupplierId = 3;
p.Create();
OrderItem item = new OrderItem();
// item.OrderId = 10248;
item.Order = o;
// item.ProductId = 1;
item.Product = p;
item.UnitPrice = 10.5M;
item.Quantity = 12;
item.Discount = 1;
item.Create();
OrderItem item2 = new OrderItem( item.ItemId );
item2.Quantity = 13;
item2.Update();
OrderItem item3 = new OrderItem( item.ItemId );
Assert.AreEqual( item3.Quantity, 13, "update orderitem fail! " );
item3.Delete();
}
finally
{
if ( o != null && o.OrderId > 0 ) o.Delete();
if ( p != null && p.ProductId > 0 ) p.Delete();
}
}
[Test]
public void TestOrderItem()
{
Product p = null;
Product p2 = null;
try
{
p = new Product();
p.ProductName = "test";
p.UnitPrice = 11.1M;
p.SupplierId = 3;
p.Create();
p2 = new Product();
p2.ProductName = "test2";
p2.UnitPrice = 18.1M;
p2.SupplierId = 3;
p2.Create();
Order o = new Order();
o.CustomerId = "ALFKI";
o.EmployeeId = 1;
o.RequiredDate = new DateTime(2005,9,10);
o.ShippedDate = new DateTime(2005,8,28);
o.ShipVia = 1;
o.Freight = 20.5M;
o.ShipName = "test name";
o.ShipAddress = "test address";
o.Create();
OrderItem item = new OrderItem();
item.Product = p;
item.UnitPrice = 10;
item.Quantity = 5
OrderItem item2 = new OrderItem();
item2.Product = p2;
item2.UnitPrice = 11;
item2.Quantity = 4;
o.AddItem(item);
o.AddItem(item2);
o.Create();
Order o2 = new Order( o.OrderId );
Assert.IsNotNull( o2.Items, "add item fail! " );
Assert.AreEqual( o2.Items.Count, 2, "add item fail! " );
IEnumerator e = o2.Items.GetEnumerator();
e.MoveNext();
OrderItem item3 = e.Current as OrderItem;
o2.RemoveItem( item3 );
o2.Update();
item3.Delete();
Order o3 = new Order( o.OrderId );
Assert.AreEqual( o3.Items.Count, 2, "remove item fail! " );
o3.Delete();
}
finally
{
if (p!=null && p.ProductId > 0) p.Delete();
if (p2!=null && p2.ProductId >0) p2.Delete();
}
}
}
}
[Test] // 測試OrderItem對象的CRUD操作。
public void TestItemCRUD() {
OrderItem item = new OrderItem();
item.OrderId = 1;
item.ProductId = 1;
item.UnitPrice = 10.5M;
item.Quantity = 12;
item.Discount = 1;
item.Create();
OrderItem item2 = new OrderItem( item.ItemId );
item2.Quantity = 13;
item2.Update();
OrderItem item3 = new OrderItem( item.ItemId );
Assert.AreEqual( item3.Quantity, 13, "update orderitem fail! " );
item3.Delete();
}
}
// Order
這個太長了,不貼了,自己看代碼吧。
// Order映射文件
xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateTest.Order, NHibernateTest" table="Orders">
<id name="OrderId" column="orderId" type="Int32" unsaved-value="0">
<generator class="identity"/>
< SPAN>id>
<property name="OrderDate" column="orderDate" type="DateTime" />
<property name="RequiredDate" column="requiredDate" type="DateTime" />
<property name="ShippedDate" column="shippedDate" type="DateTime" />
<property name="Freight" column="freight" type="Decimal" />
<property name="ShipName" column="shipName" type="String" />
<property name="ShipAddress" column="shipAddress" type="String" />
<property name="ShipCity" column="shipCity" type="String" />
<property name="ShipRegion" column="shipRegion" type="String" />
<property name="ShipPostalCode" column="shipPostalCode" type="String" />
<property name="ShipCountry" column="shipCountry" type="String" />
<property name="CustomerId" column="customerId" type="String" />
<property name="EmployeeId" column="employeeId" type="Int32" />
<property name="ShipVia" column="shipVia" type="Int32" />
<bag name="_Items" cascade="all" inverse="true">
<key column="orderId"/>
<one-to-many class="NHibernateTest.OrderItem, NHibernateTest" />
< SPAN>bag>
< SPAN>class>
< SPAN>hibernate-mapping>
// OrderItem
using System;
namespace NHibernateTest
{
///
/// OrderItem 的摘要說明。
///
public class OrderItem : BizObject
{
public OrderItem() : base() { }
public OrderItem( int existingId ) : base( existingId ) { }
#region persistent properties
private int _itemId = 0;
// private int _orderId = 0; // many-to-one, 需要重構(gòu).
private Order _order;
// private int _productId = 0; // many-to-one, 需要重構(gòu).
private Product _product;
private decimal _unitPrice = 0;
private int _quantity = 0;
private double _discount = 0.0;
public int ItemId
{
get { return _itemId; }
set { _itemId = value; }
}
// public int OrderId
// {
// get { return _orderId; }
// set { _orderId = value; }
// }
// public int ProductId
// {
// get { return _productId; }
// set { _productId = value; }
// }
public Order Order
{
get { return _order; }
set { _order = value; }
}
public Product Product
{
get { return _product; }
set { _product = value; }
}
public decimal UnitPrice
{
get { return _unitPrice; }
set { _unitPrice = value; }
}
public int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}
public double Discount
{
get { return _discount; }
set { _discount = value; }
}
#endregion
}
}
// OrderItem映射文件
因?yàn)樵O(shè)計上的原因(業(yè)務(wù)對象必須為單主健),我們向OrderDetails加入一個ItemId字段,這是一個自增型的主健,以代替原來的聯(lián)合主健。
編譯并運(yùn)行測試,檢查錯誤直到單元測試全部通過。
注意,在數(shù)據(jù)庫中要進(jìn)行如下修改,測試用例才能通過
1.orders表與customers表、employeess表和shippers表有外鍵約束,必須暫時將它們刪除,后面我們再加上這三個約束;
2.將Order Details改名為OrderDetails,表名中出現(xiàn)空格將導(dǎo)致NHB無法解析;
3.orderDetails表與orders表、products表有外健約束,必須暫時將它們刪除,下面我們將加上這些約束。
現(xiàn)在開始重構(gòu)OrderItem的測試用例和對象,主要是加入many-to-one映射。
先將TestItemCRUD修改為如下:
[Test] // 測試OrderItem對象的CRUD操作。
public void TestItemCRUD() {
Order o = null;
Product p = null;
try {
Order o = new Order();
o.CustomerId = "AA001";
o.EmployeeId = 1;
o.Create();
Product p = new Product();
p.ProductName = "test";
p.UnitPrice = 11.1M;
p.Create();
OrderItem item = new OrderItem();
// item.OrderId = 1;
item.Order = o;
// item.ProductId = 1;
item.Product = p;
item.UnitPrice = 10.5M;
item.Quantity = 12;
item.Discount = 1;
item.Create();
OrderItem item2 = new OrderItem( item.ItemId );
item2.Quantity = 13;
item2.Update();
OrderItem item3 = new OrderItem( item.ItemId );
Assert.AreEqual( item3.Quantity, 13, "update orderitem fail! " );
item3.Delete();
}
finally {
if ( o != null && o.OrderId > 0 ) o.Delete();
if ( p != null && p.ProductId > 0 ) p.Delete();
}
}
接下來修改OrderItem對象,改動如下:
// private int _orderId = 0; // many-to-one, 需要重構(gòu).
private Order Order;
// private int _productId = 0; // many-to-one, 需要重構(gòu).
private Product Product;
刪除OrderId和ProductId屬性,并加入以下屬性:
public Order Order {
get { return _order; }
set { _order = value; }
}
public Product Product {
get { return _product; }
set { _product = value; }
}
編譯項(xiàng)目,確保其能通過,如出現(xiàn)錯誤,請檢查是否有拼寫錯誤。
最后修改OrderItem對象的映射文件,改動如下:
class=”NHibernateTest.Business.Order, NHibernateTest” />
class=”NHibernateTest.Business.Product, NHibernateTest” />
按many-to-one一節(jié)中的說明對Order和Product對象設(shè)置many-to-one關(guān)聯(lián)。
重新生成項(xiàng)目,以使改動的資源編譯進(jìn)程序集中,運(yùn)行TestItemCRUD測試用例,這時應(yīng)能得到一個綠條,如果是紅條,請根據(jù)錯誤信息進(jìn)行檢查。
接下來重構(gòu)Order測試用例和對象。
先添加一個TestOrderItem用例如下:
[Test]
public void TestOrderItem() {
Product p = null;
Product p2 = null;
try {
p = new Product();
p.ProductName = "test";
p.UnitPrice = 11.1M;
p.Create();
p2 = new Product();
p2.ProductName = "test2";
p2.UnitPrice = 12.2M;
p2.Create();
Order o = new Order();
o.CustomerId = "AA001";
o.EmployeeId = 1;
o.Create();
OrderItem item = new OrderItem();
item.Product = p;
item.UnitPrice = 10;
item.Quantity = 5;
OrderItem item2 = new OrderItem();
item2.Product = p2;
item2.UnitPrice = 11;
item2.Quantity = 4;
o.AddItem( item );
o.AddItem( item2 );
o.Create();
Order o2 = new Order( o.OrderId );
Assert.IsNotNull( o2.Items, "add item fail! " );
Assert.AreEqual( o2.Items.Count, 2, "add item fail! " );
IEnumerator e = o2.Items.GetEnumerator();
e.MoveNext();
OrderItem item3 = e.Current as OrderItem;
o2.RemoveItem( item3 );
o2.Update();
Order o3 = new Order( o.OrderId );
Assert.AreEqual( o3.Items.Count, 1, "remove item fail! " );
o3.Delete();
}
finally {
if (p!=null && p.ProductId > 0) p.Delete();
if (p2!=null && p2.ProductId >0) p2.Delete();
}
}
在這個測試用例中,我們在Order對象中封裝了對OrderItem的添加和移除操作,提供一個ICollection類型的屬性Items用于遍歷OrderItem。
接著修改Order對象,要添加兩個方法和一個屬性、一些輔助字段.
public void AddItem( OrderItem item ) {
if ( _items == null ) _items = new ArrayList();
item.Order = this;
_items.Add( item );
}
public void RemoveItem( OrderItem item ) {
_items.Remove( item );
}
public ICollection Items {
return _items;
}
protected IList _Items {
get { return _items; }
set { _items = value; }
}
private IList _items;
在上面加入的代碼中,有個protected修飾的_Items屬性,它是用于one-to-many映射的,由NHB使用。
最后修改Order對象的映射文件,加入以下one-to-many代碼:
這里又用到了一個新的標(biāo)簽bag, bag用于集合映射,在NHB中還有set, list等,它們的元素大致相同,但對應(yīng)的.NET集合對象卻是不一樣的,后面對它們進(jìn)行詳細(xì)的說明和比較。
bag屬性用于指定集合的名稱和級聯(lián)操作的類型;
key元素指定關(guān)聯(lián)的數(shù)據(jù)列名稱;
one-to-many指定many一方類的全名,包括程序集名稱。
再次編譯項(xiàng)目并運(yùn)行測試用例,我得到了一個這樣的診斷錯誤信息:
NHibernateTest.OrderFixture.TestOrderItem : NHibernate.ADOException : could not synchronize database state with session
----> System.Data.SqlClient.SqlException : DELETE 語句與 COLUMN REFERENCE 約束 'FK_Order_Details_Products' 沖突。該沖突發(fā)生于數(shù)據(jù)庫 'Northwind',表 'Order Details', column 'ProductID'。
語句已終止。
從源代碼可以得知,當(dāng)執(zhí)行Update操作時,級聯(lián)操作并不會刪除我們移除的子對象,必須自行刪除!級聯(lián)刪除只是指刪除父對象的時候同時刪除子對象。
修改TestOrderItem測試用例代碼如下:
o2.Update(); // 此行不變
item3.Delete(); // 加入此行代碼
6. element(這個我沒有做,自己加入調(diào)試吧!)
集合element是一種處理多對多的映射,多對多在數(shù)據(jù)庫中也是常見的數(shù)據(jù)模型,像用戶與組,用戶與權(quán)限等。多對多關(guān)系需要通過一個中間表實(shí)現(xiàn),element的就是讀取這個中間表中某列的值。
在羅斯文商貿(mào)應(yīng)用中,Employee和Territory之間是多對多的關(guān)系,它們通過EmployeeTerritories表進(jìn)行關(guān)聯(lián)。 有關(guān)Employee和Territory對象的代碼、測試用例和映射文件請自行參照上面的方法創(chuàng)建,這里就不列出代碼了,下面只列出測試element映射的部分。
[TestFixture]
public class EmployeeFixture() {
// other test....
[test]
public TerritoryElementTest() {
Employee e = new Employee();
e.FirstName = “first”;
e.LastName = “last”;
e.AddTerritory( 1000 );
e.AddTerritory( 1001 );
e.Create();
Employee e2 = new Employee( e.EmployeeId );
Assert.IsNotNull( e2.Territories, “add territory fail!” );
Assert.AreEqual( e2.Territories.Count, 2, “add territory fail!” );
e2.RemoveTerritory( 1000 );
e2.Update();
Employee e3 = new Employee( e.EmployeeId );
Assert.AreEqual( e3.Territories.Count, 1, “remove territory fail!” );
e3.Delete();
}
}
在上面的代碼中,我們給Employee添加兩個方法和一個屬性,這和one-to-many一節(jié)中介紹的處理方法是相似的。
在Employee類中,要添加如下代碼:
public class Employee {
// other fields , properties, method...
public void AddTerritory( int territoryId ) {
if ( _territory == null ) _territory = new ArrayList();
_territory.Add( territoryId );
}
public void RemoveTerritory( int territoryId ) {
_territory.Remove( teritoryId );
}
public ICollection Territories {
get { return _territory; }
}
protected IList _Territories {
get { return _territories; }
set { _territories = value; }
}
}
最后修改Employee對象的映射文件,加入以下內(nèi)容:
在bag標(biāo)簽中,加入了一個table屬性,它指定一個實(shí)現(xiàn)多對多的中間表。在element元素中,指定要讀取的列名及其類型。
四 數(shù)據(jù)加載
1. Expression
Expression數(shù)據(jù)加載由ICriteria接口實(shí)現(xiàn), ICriteria在程序中是無法直接構(gòu)造的,必須通過ISession.CreateCriteria(type)來獲得。ICriteria主要負(fù)責(zé)存儲一組Expression對象和一組Order對象,當(dāng)調(diào)用List執(zhí)行查詢時,ICriteria對Expression對象和Order對象進(jìn)行組合以產(chǎn)生NHB內(nèi)部的查詢語句,然后交由DataLoader(數(shù)據(jù)加載器)來讀取滿足條件的記錄。
下面列出ICriteria接口中的一些常用方法:
Add:加入條件表達(dá)式(Expression對象),此方法可多次調(diào)用以組合多個條件;
AddOrder:加入排序的字段(Order對象);
List:執(zhí)行查詢, 返回滿足條件的對象集合。
SetMaxResults:設(shè)置返回的最大結(jié)果數(shù),可用于分頁;
SetFirstResult:設(shè)置首個對象返回的位置,可用于分頁;
通過SetMaxResults和SetFirstResult方法,就可以取得指定范圍段的記錄,相當(dāng)于是分頁,
!!! 要說明的是,對于SQL Server數(shù)據(jù)庫,它是使用將DataReader指針移到firstResult位置,再讀取maxResults記錄的方式來實(shí)現(xiàn)分頁的,在數(shù)據(jù)量非常大(10w以上)的情況下,性能很難保證。
所有表達(dá)式對象都繼承之Expression類,這是一個抽象(abstract)類, 同時也是一個類工廠(Factory Method模式), 用于創(chuàng)建派生的Expression對象,這樣就隱藏了派生類的細(xì)節(jié)。(又學(xué)到一招了吧!)
下面列出幾個常用的Expression對象:
EqExpression :相等判斷的表達(dá)式, 等同于 propertyName = value,由Expression.Eq取得;
GtExpression :大于判斷的表達(dá)式, 等同于 propertyName > value,由Expression.Gt取得;
LikeExpression :相似判斷的表達(dá)式, 等同于 propertyName like value,由Expression.Like取得;
AndExpression :對兩個表達(dá)式進(jìn)行And操作, 等同于 expr1 and expr2,由Expression.And取得;
OrExpression :對兩個表達(dá)式進(jìn)行Or操作, 等同于 expr1 or expr2,由Expression.Or取得;
更多的Expression對象請參考相關(guān)文檔或源代碼。
Order對象用于向ICriteria接口提供排序信息,這個類提供了兩個靜態(tài)方法,分別是Asc和Desc,顧名思義就是創(chuàng)建升序和降序的Order對象,例如要取得一個按更新日期(Updated)降序的Order對象, 使用Order.Desc("Updated")就可以了。
下面以加載Customer數(shù)據(jù)為例來說明Expression的使用:
測試代碼如:
using System;
using NHibernate.Hql;
using NHibernate.Expression;
using NHibernate.Type;
using NUnit.Framework;
using System.Collections;
namespace NHibernateTest
{
///
/// CustomerSystemFixture 的摘要說明。
///
[TestFixture]
public class CustomerSystemFixture
{
public CustomerSystemFixture()
{
}
[Test]
public void LoadByNameTest()
{
ICriterion crit = Expression.Eq("CompanyName", "company name");
IList custs = ObjectLoader.Find(crit, typeof(Customer));
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadByNamePagerTest()
{
ICriterion crit = Expression.Eq("CompanyName", "company name");
PagerInfo pi = new PagerInfo(0, 5);
IList custs = ObjectLoader.Find(crit, typeof(Customer) , pi);
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadByNameAndAddressTest()
{
ICriterion crit = Expression.Eq("CompanyName", "company name");
ICriterion crit2 = Expression.Eq("Address", "address");
IList custs = ObjectLoader.Find(Expression.And(crit, crit2), typeof(Customer));
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadByNameOrAddressTest()
{
ICriterion crit = Expression.Eq("CompanyName", "company name");
ICriterion crit2 = Expression.Eq("Address", "address");
IList custs = ObjectLoader.Find(Expression.Or(crit, crit2), typeof(Customer));
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadAllTest ()
{
string query = " from Customer ";
IList custs = ObjectLoader.Find( query, null );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadPagerDataTest()
{
string query = " from Customer ";
PagerInfo pi = new PagerInfo( 0, 5 );
IList custs = ObjectLoader.Find( query, null, pi );
}
[Test]
public void LoadByName2Test()
{
IList paramInfos = new ArrayList();
string query = " from Customer c where c.CompanyName = :CompanyName ";
paramInfos.Add(new ParamInfo("CompanyName", "test name",TypeFactory.GetStringType()) );
IList custs = ObjectLoader.Find( query, paramInfos );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadByNameAndAddress2Test()
{
IList paramInfos = new ArrayList();
string query = " from Customer c where c.CompanyName = :CompanyName and c.Address = :Address";
paramInfos.Add( new ParamInfo("CompanyName", "test name",TypeFactory.GetStringType()));
paramInfos.Add( new ParamInfo("Address", "test address",TypeFactory.GetStringType()));
IList custs = ObjectLoader.Find( query, paramInfos );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
}
}
在上面的代碼中,給出了四個較簡單的表達(dá)式加載的測試用例,它們都通過調(diào)用ObjectLoader對象的Find方法來取得數(shù)據(jù),ObjectLoader是我們自己的數(shù)據(jù)加載器,它簡單的封裝了NHB中的數(shù)據(jù)加載功能。另外,我們還用一個PagerInfo類封裝了分頁數(shù)據(jù),以方便數(shù)據(jù)傳遞。
注意:以前代碼是這么寫的
[Test]
public void LoadByNameTest() {
Expression expr = Expression.Eq( “CompanyName”, “company name” );
IList custs = ObjectLoader.Find( expr, typeof(Customer) );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
對比下現(xiàn)在的寫法:
[Test]
public void LoadByNameTest()
{
ICriterion crit = Expression.Eq("CompanyName", "company name");
IList custs = ObjectLoader.Find(crit, typeof(Customer));
// 根據(jù)期望的結(jié)果集寫Assertion.
}
這個應(yīng)該是NHibernate的版本引起的變化。
2. HQL
HQL(Hibernate Query Language)是NHB的專用查詢語言,它完全面向?qū)ο螅【褪钦f只需要知道對象名和屬性名就可以生成HQL了,這樣就再也不用去理會數(shù)據(jù)表和列了,前面說的Expression查詢最終也會轉(zhuǎn)換為HQL。
有兩種方式來執(zhí)行HQL,一種是直接使用ISession的Find方法,另一種是使用IQuery接口。IQuery接口提供了一些額外的設(shè)置,最重要的就是分頁了,這個和ICriteria差不多,另外一些就是設(shè)置參數(shù)的值了。
NHB中有一組類專門用于完成數(shù)據(jù)加載,它們分別對應(yīng)不同的數(shù)據(jù)加載情況,如實(shí)體加載、Criteria加載、OneToMany加載等。
下面同樣以加載Customer數(shù)據(jù)為例來說明HQL的使用:
在上面的CustomerSystemFixture類中加入以下幾個測試用例:
public class CustomerSystemFixture {
[Test]
public void LoadAllTest () {
string query = “ from Customer “;
IList custs = ObjectLoader.Find( query, null );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadPagerDataTest() {
string query = “ from Customer “;
PagerInfo pi = new PagerInfo( 0, 5 );
IList custs = ObjectLoader.Find( query, null, pi );
}
[Test]
public void LoadByName2Test() {
string query = “ from Customer c where c.CompanyName = :CompanyName “;
paramInfos.Add( new ParameterInfo(“CompanyName”, “test name”, TypeFactory.GetStringType()) );
IList custs = ObjectLoader.Find( query, paramInfos );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
[Test]
public void LoadByNameAndAddress2Test() {
string query = “ from Customer c where c.CompanyName = :CompanyName and c.Address = :Address“;
paramInfos.Add( new ParameterInfo(“CompanyName”, “test name”, TypeFactory.GetStringType()) );
paramInfos.Add( new ParameterInfo(“Address”, “test address”, TypeFactory.GetStringType()) );
IList custs = ObjectLoader.Find( query, paramInfos );
// 根據(jù)期望的結(jié)果集寫Assertion.
}
}
在上面的測試用例中,我們同樣將數(shù)據(jù)加載交由ObjectLoader的Find方法來處理,F(xiàn)ind有很多重載的版本,都用于數(shù)據(jù)加載。另外還使用了一個ParameterInfo類來存儲HQL語句的參數(shù)信息。
// ParamInfo
public class ParamInfo {
private string name; // 參數(shù)名稱
private object value; // 參數(shù)值
private IType type; // 參數(shù)類型
public ParamInfo( string name, object value, IType type ) {
this.name = name;
this.value = value;
this.type = type;
}
public string Name {
get { return name; }
}
public object Value {
get { return value; }
}
public IType Type {
get { return type; }
}
} //class ParamInfo
向ObjectLoader類加入以下方法:
public class ObjectLoader {
// ....
public static IList Find( string query, ICollection paramInfos ) {
return Find( query, paramInfos, null );
}
public static IList Find( string query, ICollection paramInfos, PagerInfo pi ) {
ISession s = Sessions.GetSession();
try {
IQuery q = s.CreateQuery( query );
if ( paramInfos != null ) {
foreach ( ParamInfo info in paramInfos ) {
if ( info.Value is ICollection )
q.SetParameterList( info.Name, (ICollection)info.Value, info.Type );
else
q.SetParameter( info.Name, info.Value, info.Type );
}
}
if ( pi != null ) {
q.SetFirstResult( pi.FirstResult );
q.SetMaxResults( pi.MaxResults );
}
return q.List();
}
finally {
s.Close();
}
}
}
在上面的Find方法中,通過HQL語句創(chuàng)建一個IQuery, 然后加入?yún)?shù),接著設(shè)置分頁數(shù)據(jù),最后返回列出的數(shù)據(jù)。
五 事務(wù)(沒有做,自己加吧)
既然而數(shù)據(jù)庫打交道,那么事務(wù)處理就是必需的,事務(wù)能保整數(shù)據(jù)完整性。在NHB中,ITransaction對象只對.NET的事務(wù)對象(實(shí)現(xiàn)了IDbTransaction接口的對象)進(jìn)行了簡單的封裝。
使用NHB的典型事務(wù)處理看起來像下面這樣(見ISession.cs的注釋)
ISession sess = factory.OpenSession();
Transaction tx;
try {
tx = sess.BeginTransaction();
//do some work
//...
tx.Commit();
}
catch (Exception e) {
if (tx != null) tx.Rollback();
throw e;
}
finally {
sess.Close();
}
事務(wù)對象由ISession的BeginTransaction取得,同時事務(wù)開始,如果執(zhí)行順利則提交事務(wù),否則回滾事務(wù)。
當(dāng)實(shí)現(xiàn)一個業(yè)務(wù)規(guī)則時,而這一規(guī)則要改變多個業(yè)務(wù)對象狀態(tài)時,這時就需要使用事務(wù)了,事務(wù)能保證所有改變要么全部保存,要么全部不保存!
在羅斯文商貿(mào)案例中,有這樣一個業(yè)務(wù)規(guī)則:
如果客戶對某一產(chǎn)品下了訂單,那么被訂購產(chǎn)品的已訂購數(shù)量(UnitsOnOrder)應(yīng)該加上客戶的產(chǎn)品訂單量,根據(jù)這一業(yè)務(wù)規(guī)則,創(chuàng)建一個測試用例如下:
[TestFixture]
public class OrderFixture {
[Test]
public void OrderTest() {
Product p = new Product();
p.UnitsOnOdrer = 20;
p.Create();
Order o = new Order();
OrderItem item = new OrderItem();
item.Order = o;
item.Product = p;
item.OrderNum = 10;
OrderCO.Create( o );
Product p2 = new Product( p.ProductId );
Assert.AreEqual( p2.UnitsOnOrder, 30, “add to unitsonorder fail!” );
}
}