欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

nhibernate學(xué)習(xí)之三級(jí)聯(lián)(Ternary Associations)

系統(tǒng) 1681 0
園子里面的兄弟們好,由于工作和身體的原因,幾天來(lái)都沒(méi)有寫(xiě)有關(guān)nhibernate學(xué)習(xí)系列了。看了看前幾篇大家的回復(fù),首先想要多謝兄弟們對(duì)小弟的關(guān)注和支持,可小弟水平有限,寫(xiě)出來(lái)的也只是入門(mén)級(jí)的心得。只是有一個(gè)心愿,那就是拋磚引玉,希望能和大家更多更好的互動(dòng)。技術(shù)無(wú)極限,而我更想要得是能在園子里面認(rèn)識(shí)更多的兄弟,更多的朋友。對(duì)了,忘記了一點(diǎn)事情,那就是,兄弟們,節(jié)日快樂(lè),哈哈哈。。。唧唧歪歪這么多,大家不要見(jiàn)笑,下面進(jìn)入正題
1) 學(xué)習(xí)目標(biāo)
通過(guò)進(jìn)一步學(xué)習(xí)Nhibernate基礎(chǔ)知識(shí),掌握用Nhiberate實(shí)現(xiàn)對(duì)級(jí)聯(lián)的支持,通過(guò)一個(gè)簡(jiǎn)單的用戶角色權(quán)限系統(tǒng)來(lái)體驗(yàn)nhibernate對(duì)級(jí)聯(lián)的強(qiáng)大支持。

2)開(kāi)發(fā)環(huán)境和必要準(zhǔn)備
開(kāi)發(fā)環(huán)境為:windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
必要準(zhǔn)備:學(xué)習(xí)前三篇nhibernate學(xué)習(xí)系列 Nhibernate學(xué)習(xí)之起步篇-1 , Nhibernate學(xué)習(xí)起步之many-to-one篇 , Nhibernate學(xué)習(xí)之many-to-many篇

3)示例
業(yè)務(wù)需求:實(shí)現(xiàn)一個(gè)用戶角色權(quán)限系統(tǒng),一個(gè)用戶只有一個(gè)角色,一個(gè)角色下有多個(gè)用戶,一個(gè)角色下有多個(gè)權(quán)限,一個(gè)權(quán)限也對(duì)應(yīng)多個(gè)角色
要求: (1).創(chuàng)建一個(gè)角色 (2)在該角色上創(chuàng)建兩個(gè)個(gè)用戶3)創(chuàng)建兩個(gè)權(quán)限4)指定該角色上的權(quán)限列表5)獲得一個(gè)用戶的權(quán)限列表
首先看關(guān)系數(shù)據(jù)庫(kù)關(guān)系圖:
nhibernate學(xué)習(xí)之三級(jí)聯(lián)(Ternary Associations)篇
4)實(shí)現(xiàn)步驟:
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>
/// 編號(hào)
/// </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。數(shù)據(jù)操作類
UserRolePermissionFixure
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 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()
{
Configurationcfg
= GetConfiguration();
_sessions
= cfg.BuildSessionFactory();
}

ConfigurationGetConfiguration()
{
string cfgPath = @" E:/myproject/nhibernatestudy/simle1/NHibernateStudy1/NhibernateSample1/hibernate.cfg.xml " ;
Configurationcfg
= new Configuration().Configure(cfgPath);
return cfg;
}

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

public RoleCreateRole( string roleName)
{
Roler
= new Role();
r.RoleName
= roleName;
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;
try
{
tx
= session.BeginTransaction();
session.Save(r);
tx.Commit();
}

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

finally
{
session.Close();
}

return r;

}


public UserCreateUser(Stringname, string pwd,Roler)
{
Useru
= new User();
u.Name
= name;
u.Pwd
= pwd;
u.Role
= r;
// r.UserList.Add(u);
ISessionsession = _sessions.OpenSession();

ITransactiontx
= null ;

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

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

finally
{
session.Close();
}


return u;
}

public PermissionCreatePermission(Roler, string name)
{
Permissionp
= new Permission();
p.PermissionName
= name;
r.PermissionList.Add(p);
p.RoleList.Add(r);
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;

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

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

finally
{
session.Close();
}

return p;
}

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

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

finally
{
session.Close();
}

}


}

}

5。單元測(cè)試類
UnitTest1.cs
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NhibernateSample1;

namespace TestProject1
{
/**/ /// <summary>
/// UnitTest1的摘要說(shuō)明
/// </summary>

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

NhibernateSample1.UserRolePermissionFixureusf
= new UserRolePermissionFixure();
其他測(cè)試屬性 #region 其他測(cè)試屬性
//
// 您可以在編寫(xiě)測(cè)試時(shí)使用下列其他屬性:
//
// 在運(yùn)行類中的第一個(gè)測(cè)試之前使用ClassInitialize運(yùn)行代碼
// [ClassInitialize()]
// publicstaticvoidMyClassInitialize(TestContexttestContext){}
//
// 在類中的所有測(cè)試都已運(yùn)行之后使用ClassCleanup運(yùn)行代碼
// [ClassCleanup()]
// publicstaticvoidMyClassCleanup(){}
//
// 在運(yùn)行每個(gè)測(cè)試之前使用TestInitialize運(yùn)行代碼
// [TestInitialize()]
// publicvoidMyTestInitialize(){}
//
// 在運(yùn)行每個(gè)測(cè)試之后使用TestCleanup運(yùn)行代碼
// [TestCleanup()]
// publicvoidMyTestCleanup(){}
//
#endregion


[TestMethod]
public void Test1()
{
usf.Configure();
usf.ExportTables();
Roler
= usf.CreateRole( " test " );
Assert.IsTrue(r.RoleID
> 0 );
Useru
= usf.CreateUser(Guid.NewGuid().ToString(), " ds " ,r);
Assert.IsTrue(u.Id
> 0 );
Permissionp
= usf.CreatePermission(r, " 查詢 " );
Assert.IsTrue(p.PermissionID
> 0 );
}


}

}

通過(guò)本篇的學(xué)習(xí),將充分理解到nhibernate對(duì)級(jí)聯(lián)支持的強(qiáng)大。另外除了支持三級(jí)聯(lián)之外,他還支持異類關(guān)聯(lián)(Heterogeneous Associations) .給開(kāi)發(fā)帶來(lái)了更多的靈活性和實(shí)用性。而且考慮到性能的問(wèn)題,還添加了lazy這樣的延遲加載的功能,加載父親不必要一定要加載他的兒子集合。通過(guò)集合類映射,nhinernate輕松實(shí)現(xiàn)級(jí)聯(lián),這相比較代碼生成來(lái)說(shuō),無(wú)疑是一個(gè)優(yōu)點(diǎn)。

nhibernate學(xué)習(xí)之三級(jí)聯(lián)(Ternary Associations)篇


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 欧美精品一区二区三区蜜桃视频 | 日本三级全黄 | 奇米影视 首页 | 亚洲精品乱码久久久久久9色 | 久久美乳 | 热久久这里只有精品 | 欧美激情综合色综合啪啪五月 | 780pp亚洲情艺中心 | 国产精品外围在线观看 | 亚洲第一在线播放 | 国产精品天天天天影视 | 三级黄色毛片视频 | 欧美午夜激情影院 | 日韩视频免费 | 久久久一区二区 | 性做久久久久久免费观看欧美 | 超级碰碰碰频视频免费观看 | 久久99国产精品成人欧美 | 清草在线视频精品 | 日本在线观看视频网站 | 亚洲精品无码成人A片在线虐 | 91精品观看91久久久久久 | 2021国产精品成人免费视频 | www.久久| 991av| 天堂中文资源在线观看 | 国产福利小视频在线 | 国产99免费| 无码又黄又爽又舒服的A片 综合久久网 | 国产九九免费视频网站 | 视频精品一区 | 91最新免费观看在线 | 欧美精品综合在线 | 国产一区二区三区高清 | 草比网站 | 91网站入口 | 日韩男女做性高清在线观看 | av在线电影网 | 国产91在线观看 | 99久久产在线 | 国产偷久久一级精品60部 |