園子里面的兄弟們好,由于工作和身體的原因,幾天來都沒有寫有關nhibernate學習系列了。看了看前幾篇大家的回復,首先想要多謝兄弟們對小弟的關注和支持,可小弟水平有限,寫出來的也只是入門級的心得。只是有一個心愿,那就是拋磚引玉,希望能和大家更多更好的互動。技術無極限,而我更想要得是能在園子里面認識更多的兄弟,更多的朋友。對了,忘記了一點事情,那就是,兄弟們,節日快樂,哈哈哈。。。唧唧歪歪這么多,大家不要見笑,下面進入正題
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
RoleRole
{
get
{
return
_role;
}
set
{
_role
=
value;
}
}
}
}
User.hbm.xml
<?
xmlversion
=
"
1.0
"
encoding
=
"
utf-8
"
?>
<
hibernate
-
mappingxmlns
=
"
urn:nhibernate-mapping-2.2
"
>
<
class
name
=
"
NhibernateSample1.User,NhibernateSample1
"
table
=
"
Users
"
lazy
=
"
false
"
>
<
idname
=
"
Id
"
column
=
"
Id
"
unsaved
-
value
=
"
0
"
>
<
generator
class
=
"
native
"
/>
</
id
>
<
propertyname
=
"
Name
"
column
=
"
Name
"
type
=
"
string
"
length
=
"
64
"
not
-
null
=
"
true
"
unique
=
"
true
"
></
property
>
<
propertyname
=
"
Pwd
"
column
=
"
Pwd
"
type
=
"
string
"
length
=
"
64
"
not
-
null
=
"
true
"
></
property
>
<
many
-
to
-
onename
=
"
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
IListPermissionList
{
get
{
return
_permissionList;
}
set
{
_permissionList
=
value;
}
}
public
virtual
int
RoleID
{
get
{
return
_roleID;
}
set
{
_roleID
=
value;
}
}
public
virtual
IListUserList
{
get
{
return
_list;
}
set
{
_list
=
value;
}
}
public
virtual
string
RoleName
{
get
{
return
_roleName;
}
set
{
_roleName
=
value;
}
}
}
}
Role.hbm.xml
<?
xmlversion
=
"
1.0
"
encoding
=
"
utf-8
"
?>
<
hibernate
-
mappingxmlns
=
"
urn:nhibernate-mapping-2.2
"
>
<
class
name
=
"
NhibernateSample1.Role,NhibernateSample1
"
table
=
"
Roles
"
lazy
=
"
false
"
>
<
idname
=
"
RoleID
"
column
=
"
RoleID
"
unsaved
-
value
=
"
0
"
>
<
generator
class
=
"
native
"
/>
</
id
>
<
propertyname
=
"
RoleName
"
column
=
"
RoleName
"
type
=
"
string
"
length
=
"
64
"
not
-
null
=
"
true
"
></
property
>
<
bagname
=
"
PermissionList
"
table
=
"
Role_Permissions
"
inverse
=
"
true
"
lazy
=
"
false
"
cascade
=
"
all
"
>
<
keycolumn
=
"
RoleID
"
/>
<
many
-
to
-
many
class
=
"
NhibernateSample1.Permission,NhibernateSample1
"
column
=
"
PermissionID
"
></
many
-
to
-
many
>
</
bag
>
<
bagname
=
"
UserList
"
table
=
"
Users
"
inverse
=
"
true
"
lazy
=
"
false
"
cascade
=
"
all
"
>
<
keycolumn
=
"
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
IListRoleList
{
get
{
return
_roleList;
}
set
{
_roleList
=
value;
}
}
}
}
Permission.hbm.xml
<?
xmlversion
=
"
1.0
"
encoding
=
"
utf-8
"
?>
<
hibernate
-
mappingxmlns
=
"
urn:nhibernate-mapping-2.2
"
>
<
class
name
=
"
NhibernateSample1.Permission,NhibernateSample1
"
table
=
"
Permissions
"
lazy
=
"
false
"
>
<
idname
=
"
PermissionID
"
column
=
"
PermissionID
"
unsaved
-
value
=
"
0
"
>
<
generator
class
=
"
native
"
/>
</
id
>
<
propertyname
=
"
PermissionName
"
column
=
"
PermissionName
"
type
=
"
string
"
length
=
"
64
"
not
-
null
=
"
true
"
unique
=
"
true
"
></
property
>
<
bagname
=
"
RoleList
"
table
=
"
Role_Permissions
"
lazy
=
"
true
"
>
<
keycolumn
=
"
PermissionID
"
/>
<
many
-
to
-
many
class
=
"
NhibernateSample1.Role,NhibernateSample1
"
column
=
"
RoleID
"
></
many
-
to
-
many
>
</
bag
>
</
class
>
</
hibernate
-
mapping
>
4。數據操作類
UserRolePermissionFixure
5。單元測試類
UnitTest1.cs
通過本篇的學習,將充分理解到nhibernate對級聯支持的強大。另外除了支持三級聯之外,他還支持異類關聯(Heterogeneous Associations) .給開發帶來了更多的靈活性和實用性。而且考慮到性能的問題,還添加了lazy這樣的延遲加載的功能,加載父親不必要一定要加載他的兒子集合。通過集合類映射,nhinernate輕松實現級聯,這相比較代碼生成來說,無疑是一個優點。
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





























































































































































































































更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
