日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Nhibernate学习起步之many-to-one篇(转)

發布時間:2023/12/9 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nhibernate学习起步之many-to-one篇(转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.???? 學習目的:

通過進一步學習nhibernate基礎知識,在實現單表CRUD的基礎上,實現兩表之間one-to-many的關系.

2.???? 開發環境+必要準備

開發環境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition

必要準備: 學習上篇文章單表操作???

3. 對上篇文章中部分解釋

?1)在User.hbm.xml中class節點中有一個lazy的屬性,這個屬性用于指定是否需要延遲加載(lazy loading),在官方文檔中稱為:lazy fecting.可以說延遲加載是nhibernate最好的特點,因為它可以在父類中透明的加載子類集合,這對于many-to-one的業務邏輯中,真是方便極了。但是有些時候,父類是不需要攜帶子類信息的。這時候如果也加載,無疑對性能是一種損失。在映射文件的class節點中可以通過配置lazy屬性來指定是否支持延遲加載,這就更靈活多了。?

?2) 在User.hbm.xml中generate節點,代表的是主鍵的生成方式,上個例子中的”native”根據底層數據庫的能力選擇identity,hilo,sequence中的一個,比如在MS Sql中,使我們最經常使用的自動增長字段,每次加1.?

3) 在NHibernateHelper.cs中,創建Configuration對象的代碼:new Configuration().Configure(@"E:\myproject\nhibernatestudy\simle1\NHibernateStudy1\NhibernateSample1\hibernate.cfg.xml");因為我是在單元測試中調試,所以將絕對路徑的配置文件傳遞給構造函數。如果在windows app或者web app可以不用傳遞該參數。?

4. 實現步驟

?1)確定實現的業務需求:用戶工資管理系統

?2) 打開上篇文章中的NHibernateStudy1解決方案。向項目NhibernateSample1添加類Salary;代碼如下

Salary.cs
using?System;
using?System.Collections.Generic;
using?System.Text;

namespace?NhibernateSample1
{
????
public?partial?class?Salary
????
{
????????
int?_id;
????????User?_user;
????????
int?_year;
????????
int?_month;
????????
int?_envy;
????????
decimal?_money;
????????
/**////?<summary>
????????
///?工資編號
????????
///?</summary>

????????public?virtual?int?Id
????????
{
????????????
get
????????????
{
????????????????
return?_id;
????????????}

????????????
set
????????????
{
????????????????_id?
=?value;
????????????}

????????}

????????
/**////?<summary>
????????
///?雇員
????????
///?</summary>

????????public?virtual?User?Employee
????????
{
????????????
get
????????????
{
????????????????
return?_user;
????????????}

????????????
set
????????????
{
????????????????_user?
=?value;
????????????}

????????}

????????
/**////?<summary>
????????
///?年度
????????
///?</summary>

????????public?int?Year
????????
{
????????????
get
????????????
{
????????????????
return?_year;
????????????}

????????????
set
????????????
{
????????????????_year?
=?value;
????????????}

????????}

????????
/**////?<summary>
????????
///?月份
????????
///?</summary>

????????public?int?Month
????????
{
????????????
get
????????????
{
????????????????
return?_month;
????????????}

????????????
set
????????????
{
????????????????_month?
=?value;
????????????}

????????}

????????
/**////?<summary>
????????
///?季度
????????
///?</summary>

????????public?int?Envy
????????
{
????????????
get
????????????
{
????????????????
return?_envy;
????????????}

????????????
set
????????????
{
????????????????_envy?
=?value;
????????????}

????????}

????????
/**////?<summary>
????????
///?工資
????????
///?</summary>

????????public?decimal?Money
????????
{
????????????
get
????????????
{
????????????????
return?_money;
????????????}

????????????
set
????????????
{
????????????????_money?
=?value;
????????????}

????????}

????}

}

3) 更改User.cs,在User里面添加SalaryList屬性:

User.cs
?1private?System.Collections.IList?_salaryList;
?2?/**////?<summary>
?3????????///?工資列表
?4????????///?</summary>

?5????????public?System.Collections.IList?SalaryList
?6????????{
?7????????????get
?8????????????{
?9????????????????return?_salaryList;
10????????????}

11????????????set
12????????????{
13????????????????_salaryList?=?value;
14????????????}

15????????}
4)修改User.hbm.xml,加入bag節點
User.hbm.xml
<bag?name="SalaryList"?table="Salary"?inverse="true"?lazy="true"?cascade="all">
??????
<key?column="Id"/>
??????
<one-to-many?class="NhibernateSample1.Salary,NhibernateSample1"></one-to-many>
????
</bag>

?5)編寫類Salary的映射文件:Salary.hbm.xml
Salary.hbm.xml
<?xml?version="1.0"?encoding="utf-8"??>
<hibernate-mapping?xmlns="urn:nhibernate-mapping-2.2">
??
<class?name="NhibernateSample1.Salary,NhibernateSample1"?table="Salary"?lazy="false">
????
<id?name="Id"?column="Id"?unsaved-value="0">
??????
<generator?class="native"?/>
????
</id>
????
<property?name="Year"?column="Year"?type="Int32"?not-null="true"></property>
????
<property?name="Month"??column="Month"??type="Int32"?not-null="true"></property>
????
<property?name="Envy"??column="Envy"??type="Int32"?not-null="true"></property>
????
<property?name="Money"??column="Money"??type="Decimal"?not-null="true"></property>
????
<many-to-one?name="Employee"?column="Uid"?not-null="true"></many-to-one>
??
</class>
</hibernate-mapping>
6)編寫CRUD
UserSalaryFixure.cs
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Collections;
using?NHibernate;
using?NHibernate.Cfg;
using?NHibernate.Tool.hbm2ddl;

namespace?NhibernateSample1
{
????
public??class?UserSalaryFixure
????
{
????????
private?ISessionFactory?_sessions;?
????????
public?void?Configure()
????????
{
????????????Configuration?cfg?
=?GetConfiguration();??????
????????????_sessions?
=?cfg.BuildSessionFactory();
????????}

????????Configuration?GetConfiguration()
????????
{
????????????
string?cfgPath?=?@"E:\my?project\nhibernate?study\simle?1\NHibernateStudy1\NhibernateSample1\hibernate.cfg.xml";
????????????Configuration?cfg?
=?new?Configuration().Configure(cfgPath);
????????????
return?cfg;
????????}

????????
public?void?ExportTables()
????????
{
????????????Configuration?cfg?
=?GetConfiguration();???????????
????????????
new?SchemaExport(cfg).Create(true,?true);
????????}

????????
public?User?CreateUser(String?name,string?pwd)
????????
{
????????????User?u?
=?new?User();
????????????u.Name?
=?name;
????????????u.Pwd?
=?pwd;
????????????u.SalaryList?
=?new?ArrayList();

????????????ISession?session?
=?_sessions.OpenSession();

????????????ITransaction?tx?
=?null;

????????????
try
????????????
{
????????????????tx?
=?session.BeginTransaction();
????????????????session.Save(u);
????????????????tx.Commit();
????????????}

????????????
catch?(HibernateException?e)
????????????
{
????????????????
if?(tx?!=?null)?tx.Rollback();
????????????????
throw?e;
????????????}

????????????
finally
????????????
{
????????????????session.Close();
????????????}


????????????
return?u;
????????}

????????
public?Salary?CreateSalary(User?u,?int?year,int?month,int?envy,decimal?money)
????????
{
????????????Salary?item?
=?new?Salary();
????????????item.Year?
=?year;
????????????item.Money?
=?money;
????????????item.Envy?
=?envy;
????????????item.Month?
=?month;
????????????item.Employee?
=?u;
????????????u.SalaryList.Add(item);
????????????ISession?session?
=?_sessions.OpenSession();
????????????ITransaction?tx?
=?null;

????????????
try
????????????
{
????????????????tx?
=?session.BeginTransaction();
????????????????session.Update(u);
????????????????tx.Commit();
????????????}

????????????
catch?(HibernateException?e)
????????????
{
????????????????
if?(tx?!=?null)?tx.Rollback();
????????????????
throw?e;
????????????}

????????????
finally
????????????
{
????????????????session.Close();
????????????}

????????????
return?item;
????????}

????????
public?Salary?CreateSalary(int?uid,int?year,?int?month,?int?envy,?decimal?money)
????????
{
????????????Salary?item?
=?new?Salary();
????????????item.Year?
=?year;
????????????item.Money?
=?money;
????????????item.Envy?
=?envy;
????????????item.Month?
=?month;
????????????
????????????ISession?session?
=?_sessions.OpenSession();
????????????ITransaction?tx?
=?null;
????????????
try
????????????
{
????????????????tx?
=?session.BeginTransaction();
????????????????User?u?
=?(User)session.Load(typeof(User),?uid);
????????????????item.Employee?
=?u;
????????????????u.SalaryList.Add(item);
????????????????tx.Commit();
????????????}

????????????
catch?(HibernateException?e)
????????????
{
????????????????
if?(tx?!=?null)?tx.Rollback();
????????????????
throw?e;
????????????}

????????????
finally
????????????
{
????????????????session.Close();
????????????}

????????????
return?item;
????????}

????????
public?Salary?GetSalary(int?salaryID)
????????
{
????????????ISession?session?
=?_sessions.OpenSession();
????????????ITransaction?tx?
=?null;
????????????
try
????????????
{
????????????????tx?
=?session.BeginTransaction();
????????????????Salary?item?
=?(Salary)session.Load(typeof(Salary),
????????????????????salaryID);???????????????
????????????????tx.Commit();
????????????????
return?item;
????????????}

????????????
catch?(HibernateException?e)
????????????
{
????????????????
if?(tx?!=?null)?tx.Rollback();
????????????????
return?null;
????????????}

????????????
finally
????????????
{
????????????????session.Close();
????????????}

????????????
return?null;
????????}

????????
public?User?GetUser(int?uid)
????????
{
????????????ISession?session?
=?_sessions.OpenSession();
????????????ITransaction?tx?
=?null;
????????????
try
????????????
{
????????????????tx?
=?session.BeginTransaction();
????????????????User?item?
=?(User)session.Load(typeof(User),
????????????????????uid);
????????????????tx.Commit();
????????????????
return?item;
????????????}

????????????
catch?(HibernateException?e)
????????????
{
????????????????
if?(tx?!=?null)?tx.Rollback();
????????????????
return?null;
????????????}

????????????
finally
????????????
{
????????????????session.Close();
????????????}

????????????
return?null;
????????}

????????
public?void?UpdateSalary(int?salaryID,?decimal?money)
????????
{
????????????ISession?session?
=?_sessions.OpenSession();
????????????ITransaction?tx?
=?null;
????????????
try
????????????
{
????????????????tx?
=?session.BeginTransaction();
????????????????Salary?item?
=?(Salary)session.Load(typeof(Salary),
????????????????????salaryID);
????????????????item.Money?
=?money;
????????????????tx.Commit();
????????????}

????????????
catch?(HibernateException?e)
????????????
{
????????????????
if?(tx?!=?null)?tx.Rollback();
????????????????
throw?e;
????????????}

????????????
finally
????????????
{
????????????????session.Close();
????????????}

????????}


????????
public?void?Delete(int?uid)
????????
{
????????????ISession?session?
=?_sessions.OpenSession();
????????????ITransaction?tx?
=?null;
????????????
try
????????????
{
????????????????tx?
=?session.BeginTransaction();
????????????????Salary?item?
=?session.Load(typeof(Salary),?uid)?as?Salary;?;
????????????????session.Delete(item);
????????????????tx.Commit();
????????????}

????????????
catch?(HibernateException?e)
????????????
{
????????????????
if?(tx?!=?null)?tx.Rollback();
????????????????
throw?e;
????????????}

????????????
finally
????????????
{
????????????????session.Close();
????????????}

????????}


????}

}

7) 編寫單元測試類:UnitTest1.cs
UnitTest1.cs
using?System;
using?System.Text;
using?System.Collections.Generic;
using?Microsoft.VisualStudio.TestTools.UnitTesting;
using?NhibernateSample1;

namespace?TestProject1
{
????
/**////?<summary>
????
///?UnitTest1?的摘要說明
????
///?</summary>

????[TestClass]
????
public?class?UnitTest1
????
{
????????
public?UnitTest1()
????????
{
????????????
//
????????????
//?TODO:?在此處添加構造函數邏輯
????????????
//
????????}

????????NhibernateSample1.UserSalaryFixure?usf?
=?new?UserSalaryFixure();
????????
其他測試屬性#region?其他測試屬性
????????
//
????????
//?您可以在編寫測試時使用下列其他屬性:
????????
//
????????
//?在運行類中的第一個測試之前使用?ClassInitialize?運行代碼
????????
//?[ClassInitialize()]
????????
//?public?static?void?MyClassInitialize(TestContext?testContext)?{?}
????????
//
????????
//?在類中的所有測試都已運行之后使用?ClassCleanup?運行代碼
????????
//?[ClassCleanup()]
????????
//?public?static?void?MyClassCleanup()?{?}
????????
//
????????
//?在運行每個測試之前使用?TestInitialize?運行代碼?
????????
//?[TestInitialize()]
????????
//?public?void?MyTestInitialize()?{?}
????????
//
????????
//?在運行每個測試之后使用?TestCleanup?運行代碼
????????
//?[TestCleanup()]
????????
//?public?void?MyTestCleanup()?{?}
????????
//
????????#endregion


????????[TestMethod]
????????
public?void?Test1()
????????
{
????????????usf.Configure();
????????????usf.ExportTables();
????????????User?u?
=?usf.CreateUser(Guid.NewGuid().ToString(),?"ds");
????????????Assert.IsTrue(u.Id
>0);
????????????Salary?s?
=?usf.CreateSalary(u,?2007,?3,?1,?(decimal)8000.00);
????????????Assert.IsTrue(s.Id?
>?0);
????????????Salary?s1?
=?usf.CreateSalary(u.Id,?2007,?3,?1,?(decimal)7500);
????????????Assert.IsTrue(s1.Id
>0);
????????????usf.UpdateSalary(s1.Id,?(
decimal)6000);
????????????s1?
=?usf.GetSalary(s1.Id);
????????????Assert.IsTrue(s1.Money?
==?(decimal)6000);
????????????usf.Delete(s1.Id);
????????????s1?
=?usf.GetSalary(s1.Id);
????????????Assert.IsNull(s1);
????????????User?u1?
=?usf.GetUser(1);
????????????Assert.IsTrue(u1.SalaryList.Count
>0);
????????}


????}

}

加載測試元數據,直到Test()通過。
總結:通過進一步學習nhiberate,發現ORM框架真是非常強大。今天先到這里。明天繼續。
項目文件:/Files/jillzhang/simple2.rar

轉載于:https://www.cnblogs.com/erichzhou/archive/2007/03/29/692949.html

總結

以上是生活随笔為你收集整理的Nhibernate学习起步之many-to-one篇(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。