nhibernate學習之三級聯(Ternary Associations)篇

1) 學習目標
通過進一步學習Nhibernate基礎知識,掌握用Nhiberate實現對級聯的支持,通過一個簡單的用戶角色權限系統來體驗nhibernate對級聯的強大支持。
?
2)開發環境和必要準備?
? 開發環境為:windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
? 必要準備:學習前三篇nhibernate學習系列Nhibernate學習之起步篇-1? ,Nhibernate學習起步之many-to-one篇 ,Nhibernate學習之many-to-many篇

3)示例
? 業務需求:實現一個用戶角色權限系統,一個用戶只有一個角色,一個角色下有多個用戶,一個角色下有多個權限,一個權限也對應多個角色
????????????????????? 要求: (1).創建一個角色 (2)在該角色上創建兩個個用戶3)創建兩個權限4)指定該角色上的權限列表5)獲得一個用戶的權限列表
? 首先看關系數據庫關系圖:
??
4)實現步驟:
? 1.User.cs
using?System;
using?System.Collections.Generic;
using?System.Text;

namespace?NhibernateSample1
{
????
public?class?User
????
{
????????
private?int?_id;
????????
private?string?_name;
????????
private?string?_pwd;
????????
private?Role?_role;
????????
/**////?<summary>
????????
///?編號
????????
///?</summary>

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

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

????????}


????????
/**////?<summary>
????????
///?名稱
????????
///?</summary>

????????public?virtual?string?Name
????????
{
????????????
get
????????????
{
????????????????
return?_name;
????????????}

????????????
set
????????????
{
????????????????_name?
=?value;
????????????}

????????}


????????
/**////?<summary>
????????
///?密碼
????????
///?</summary>

????????public?virtual?string?Pwd
????????
{
????????????
get
????????????
{
????????????????
return?_pwd;
????????????}

????????????
set
????????????
{
????????????????_pwd?
=?value;
????????????}

????????}

????????
public?virtual?Role?Role
????????
{
????????????
get
????????????
{
????????????????
return?_role;
????????????}

????????????
set
????????????
{
????????????????_role?
=?value;
????????????}

????????}

????}

}

?User.hbm.xml
<?xml?version="1.0"?encoding="utf-8"??>
<hibernate-mapping?xmlns="urn:nhibernate-mapping-2.2">
??
<class?name="NhibernateSample1.User,NhibernateSample1"?table="Users"?lazy="false">
????
<id?name="Id"?column="Id"?unsaved-value="0">
??????
<generator?class="native"?/>
????
</id>
????
<property?name="Name"?column="Name"?type="string"?length="64"?not-null="true"?unique="true"></property>
????
<property?name="Pwd"??column="Pwd"??type="string"?length="64"?not-null="true"></property>
????
<many-to-one?name="Role"??class="NhibernateSample1.Role,NhibernateSample1"?column="RoleID"></many-to-one>
???
</class>
</hibernate-mapping>
2.Role.cs
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Collections;

namespace?NhibernateSample1
{
????
public?class?Role
????
{
????????
int?_roleID;
????????
string?_roleName;
????????IList?_list?
=?new??ArrayList();
????????IList?_permissionList?
=?new?ArrayList();
????????
public?virtual?IList?PermissionList
????????
{
????????????
get
????????????
{
????????????????
return?_permissionList;
????????????}

????????????
set
????????????
{
????????????????_permissionList?
=?value;
????????????}

????????}

????????
public?virtual?int?RoleID
????????
{
????????????
get
????????????
{
????????????????
return?_roleID;
????????????}

????????????
set
????????????
{
????????????????_roleID?
=?value;
????????????}

????????}

????????
public?virtual?IList?UserList
????????
{
????????????
get
????????????
{
????????????????
return?_list;
????????????}

????????????
set
????????????
{
????????????????_list?
=?value;
????????????}

????????}

????????
public?virtual?string?RoleName
????????
{
????????????
get
????????????
{
????????????????
return?_roleName;
????????????}

????????????
set
????????????
{
????????????????_roleName?
=?value;
????????????}

????????}

????}

}

Role.hbm.xml
<?xml?version="1.0"?encoding="utf-8"??>
<hibernate-mapping?xmlns="urn:nhibernate-mapping-2.2">
??
<class?name="NhibernateSample1.Role,NhibernateSample1"?table="Roles"?lazy="false">
????
<id?name="RoleID"?column="RoleID"?unsaved-value="0">
??????
<generator?class="native"?/>
????
</id>
????
<property?name="RoleName"??column="RoleName"??type="string"?length="64"?not-null="true"></property>
????
<bag?name="PermissionList"?table="Role_Permissions"?inverse="true"?lazy="false"?cascade="all">
??????
<key?column="RoleID"/>
??????
<many-to-many?class="NhibernateSample1.Permission,NhibernateSample1"?column="PermissionID"></many-to-many>
????
</bag>
????
<bag?name="UserList"?table="Users"?inverse="true"?lazy="false"?cascade="all">
??????
<key?column="RoleID"/>
??????
<one-to-many?class="NhibernateSample1.User,NhibernateSample1"></one-to-many>
????
</bag>
??
</class>
</hibernate-mapping>
3.Permission.cs
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Collections;

namespace?NhibernateSample1
{
????
public?class?Permission
????
{
????????
int?_permissionID;
????????
string?_permissionName;
????????IList?_roleList?
=?new?ArrayList();
????????
public?virtual?int?PermissionID
????????
{
????????????
get
????????????
{
????????????????
return?_permissionID;
????????????}

????????????
set
????????????
{
????????????????_permissionID?
=?value;
????????????}

????????}

????????
public?virtual?string?PermissionName
????????
{
????????????
get
????????????
{
????????????????
return?_permissionName;
????????????}

????????????
set
????????????
{
????????????????_permissionName
=value;
????????????}

????????}

????????
public?virtual?IList?RoleList
????????
{
????????????
get
????????????
{
????????????????
return?_roleList;
????????????}

????????????
set
????????????
{
????????????????_roleList?
=?value;
????????????}

????????}

????}

}

Permission.hbm.xml
<?xml?version="1.0"?encoding="utf-8"??>
<hibernate-mapping?xmlns="urn:nhibernate-mapping-2.2">
??
<class?name="NhibernateSample1.Permission,NhibernateSample1"?table="Permissions"?lazy="false">
????
<id?name="PermissionID"?column="PermissionID"?unsaved-value="0">
??????
<generator?class="native"?/>
????
</id>
????
<property?name="PermissionName"?column="PermissionName"?type="string"?length="64"?not-null="true"?unique="true"></property>
????
<bag?name="RoleList"?table="Role_Permissions"??lazy="true">
??????
<key?column="PermissionID"/>
??????
<many-to-many?class="NhibernateSample1.Role,NhibernateSample1"?column="RoleID"></many-to-many>
????
</bag>
??
</class>
</hibernate-mapping>
4。數據操作類
UserRolePermissionFixure
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?UserRolePermissionFixure
????
{
????????
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?Role?CreateRole(string?roleName)
????????
{
????????????Role?r?
=?new?Role();
????????????r.RoleName?
=?roleName;
????????????ISession?session?
=?_sessions.OpenSession();????????????
????????????ITransaction?tx?
=?null;
????????????
try
????????????
{
????????????????tx?
=?session.BeginTransaction();
????????????????session.Save(r);
????????????????tx.Commit();
????????????}

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

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

????????????
return?r;

????????}


????????
public?User?CreateUser(String?name,string?pwd,Role?r)
????????
{
????????????User?u?
=?new?User();
????????????u.Name?
=?name;
????????????u.Pwd?
=?pwd;
????????????u.Role?
=?r;
????????????
//r.UserList.Add(u);
????????????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?Permission?CreatePermission(Role?r,string?name)
????????
{
????????????Permission?p?
=?new?Permission();
????????????p.PermissionName?
=?name;
????????????r.PermissionList.Add(p);
????????????p.RoleList.Add(r);
????????????ISession?session?
=?_sessions.OpenSession();
????????????ITransaction?tx?
=?null;

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

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

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

????????????
return?p;
????????}

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

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

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

????????}


????}

}

5。單元測試類
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.UserRolePermissionFixure?usf?
=?new?UserRolePermissionFixure();
????????
其他測試屬性#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();
????????????Role?r?
=?usf.CreateRole("test");
????????????Assert.IsTrue(r.RoleID?
>?0);
????????????User?u?
=?usf.CreateUser(Guid.NewGuid().ToString(),?"ds",?r);????????????
????????????Assert.IsTrue(u.Id?
>?0);
????????????Permission?p?
=?usf.CreatePermission(r,?"查詢");
????????????Assert.IsTrue(p.PermissionID?
>?0);?????????
????????}


????}

}

?? 通過本篇的學習,將充分理解到nhibernate對級聯支持的強大。另外除了支持三級聯之外,他還支持異類關聯(Heterogeneous Associations) .給開發帶來了更多的靈活性和實用性。而且考慮到性能的問題,還添加了lazy這樣的延遲加載的功能,加載父親不必要一定要加載他的兒子集合。通過集合類映射,nhinernate輕松實現級聯,這相比較代碼生成來說,無疑是一個優點。?

轉載于:https://www.cnblogs.com/hliq/archive/2008/02/21/2087242.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/380161.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/380161.shtml
英文地址,請注明出處:http://en.pswp.cn/news/380161.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【競賽題解】Codeforces Round #715 (Div. 2) C

C. The Sports Festival 題意&#xff1a;對于給定的整型數組aaa&#xff0c;每次選擇其中一個元素aia_iai?&#xff08;不能重復選擇同一元素&#xff09;&#xff0c;每次計算已選擇的元素的極差&#xff08;最大元素減最小元素的差&#xff09;&#xff0c;輸出最后極差和…

C和匯編---sizeof運算符和strlen函數

sizeof sizeof是C語言的內置運算符&#xff0c;以字節為單位給出指定類型的大小。 程序&#xff1a; #include <stdio.h>int main(void) {int a8;int b sizeof(a);//printf("a占用字節%u\n",sizeof(a));printf("a占用字節%d\n",b);return 0; }反匯…

Java接口程序練習

題目&#xff1a; 編寫一個接口程序&#xff0c;其中定義一個計算體積的方法。然后&#xff0c;在設計應用程序實現這個接口&#xff0c;分別計算矩形柱面體積和圓形柱面體積。 代碼如下&#xff1a; import java.util.*;//導入掃描儀&#xff1b; public class clown {publi…

[原]Asp.net替換不同版本的Dll文件碰到的問題以及解決辦法.

情景還原: 今天一個朋友說網站不能上傳圖片,我檢查后發現一直卡住在上傳頁面,一直滾動,是個Fckeditor控件2.6.3的. 經過google以后得到的結論是圖片上傳成功,但是沒有返回結果,在服務器上可以看到上傳的圖片. 說明是上傳控件有問題,程序不能返回結果. 再google以后發現有人已經…

疊筐

Problem Description 需要的時候&#xff0c;就把一個個大小差一圈的筐疊上去&#xff0c;使得從上往下看時&#xff0c;邊筐花色交錯。這個工作現在要讓計算機來完成&#xff0c;得看你的了。 Input 輸入是一個個的三元組&#xff0c;分別是&#xff0c;外筐尺寸n&#xff…

“Visual Studio.net已檢測到指定的Web服務器運行的不是Asp.net1.1版。您將無法運行Asp.net Web應用程序或服務”問題的解決方案...

解決方案一&#xff1a; 1.確定有安裝.net framework 1.1&#xff0c;可以查看目錄&#xff0c;c:\winnt\microsoft.net\framework重啟IIS&#xff0c;重啟計算機&#xff08;常規糾錯方法&#xff09; 2.如果你的Web服務器使用了固定IP&#xff1a;確定你的“Internet信息服務…

【桶】220.存在重復元素 III 【LeetCode】

220.存在重復元素 III 【LeetCode】 給你一個整數數組 nums 和兩個整數 k 和 t。請你判斷是否存在 兩個不同下標i和j&#xff0c;使得 abs(nums[i] - nums[j]) < t&#xff0c;同時又滿足 abs(i - j) < k。 如果存在則返回 true&#xff0c;不存在返回 false。 示例 1…

遠控免殺專題12--Green-Hat-Suite免殺

0x01 免殺能力一覽表 幾點說明&#xff1a; 1、上表中標識 √ 說明相應殺毒軟件未檢測出病毒&#xff0c;也就是代表了Bypass。 2、為了更好的對比效果&#xff0c;大部分測試payload均使用msf的windows/meterperter/reverse_tcp模塊生成。 3、由于本機測試時只是安裝了360全…

英語基礎語法(八)-時態

英語中&#xff0c;動詞時態的用法是尤其復雜和富于變化的。經常通過動詞詞尾、組動詞等的變化表明動作發生時間的先后順序&#xff0c;即時態。總的來說&#xff0c;英語中的動詞時態分為 三個基本類型&#xff1a; 現在、過去和將來。動詞時態的變化常常伴隨著相應的表示時間…

Java PushbackInputStream markSupported()方法與示例

PushbackInputStream類markSupported()方法 (PushbackInputStream Class markSupported() method) markSupported() method is available in java.io package. markSupported()方法在java.io包中可用。 markSupported() method is used to check whether this stream supports …

面型對象 (接口與類的區別)

public class Demo4_Interface {public static void main(String[] args) {某女星 clown new 某女星();clown.潛規則();clown.關系();} }/*親爹只有一個&#xff0c;是單繼承;干爹可以有很多個&#xff0c;是多實現;*/ interface 某干爹{public void 關系();public void 潛規…

遠控免殺專題 13----zirikatu免殺

0x01 免殺能力一覽表 幾點說明&#xff1a; 1、上表中標識 √ 說明相應殺毒軟件未檢測出病毒&#xff0c;也就是代表了Bypass。 2、為了更好的對比效果&#xff0c;大部分測試payload均使用msf的windows/meterperter/reverse_tcp模塊生成。 3、由于本機測試時只是安裝了360全…

UML 的九種模型圖

1. UML的模型圖 UML 的模型圖能夠將被建模的系統的某一個方面的某一部分以圖形的方式表示出來&#xff0c;不同的視圖通過將多個不同的模型圖有機組合在一起就能夠描述系統模型的某方面的特征。UML的模型圖是有模型元素構成的&#xff0c;模型元素以圖標的形式直觀形象的表達…

【莫隊】區間眾數(Codeforces Round #716 (Div. 2) D)

D. Cut and Stick &#xff08;賽后補題&#xff09;借本題學習莫隊算法以及區間眾數的求法 題意&#xff1a;對于整型數組&#xff0c;每次詢問[L,R][L,R][L,R]區間問最少分為多少個子序列&#xff0c;使得每個子序列的眾數xxx的個數cntxcnt_xcntx?不大于 ?len2?\left \l…

如何正確使用SqlConnection

以前曾見過有人這樣寫代碼&#xff1a; public class Service1 : IService1{private SqlConnection conn new SqlConnection();public void Method1(){//do something with conn;}public void Method2(){//do something with conn;}public void Method3(){//do something with…

關系代數基本運算_關系代數的基本和附加運算

關系代數基本運算Definition 定義 Every DBMS must define a query language to enable users to access the data which is stored in the database. Relational Algebra is a procedural query language. It is used to query the database tables in order to access data…

遠控免殺專題 14 ---AVIator

0x01 免殺能力一覽表 幾點說明&#xff1a; 1、上表中標識 √ 說明相應殺毒軟件未檢測出病毒&#xff0c;也就是代表了Bypass。 2、為了更好的對比效果&#xff0c;大部分測試payload均使用msf的windows/meterperter/reverse_tcp模塊生成。 3、由于本機測試時只是安裝了360全…

面型對象 (包package)

面向對象(package關鍵字的概述及作用) 為什么要有包 將字節碼(.class)進行分類存放 包其實就是文件夾 代碼如下&#xff1a; package beyond.hjj;//在當前運行目錄下創建一個子目錄結構beyond\hjj&#xff0c;在子目錄下存放已經編譯成字節碼文件的clown.class類。 class c…

【Web開發】級聯查詢(Ajax/ jQuery/ Servlet)

實現級聯查詢 共有兩個下拉框&#xff0c;第一級為學院&#xff0c;第二級為學院開設的科目。 實現的功能為&#xff1a;當改變學院的選擇&#xff0c;第二級下拉框需變為對應學院開設的科目內容。 結果預覽&#xff1a; jsp頁面 <% page contentType"text/html;…

asp.net treeView綁定

這個東西不是什么復雜的東西&#xff0c; 幫著小兄弟寫個Demo, 實現個Binding public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind(); } } priv…