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

Nhibernate學(xué)習(xí)之many-to-many篇

系統(tǒng) 1828 0
  1. 學(xué)習(xí)目的:

通過進(jìn)一步學(xué)習(xí)Nhibernate基礎(chǔ)知識(shí),掌握用Nhiberate實(shí)現(xiàn)多對(duì)多的業(yè)務(wù)邏輯

  1. 開發(fā)環(huán)境+必要準(zhǔn)備

開發(fā)環(huán)境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition

前期準(zhǔn)備: 學(xué)習(xí)上兩篇 單表操作 many-to-one篇

3.對(duì)上篇文章的部分解釋

1)bag節(jié)點(diǎn):用于定義System.Collection.IList類型的集合元素。

屬性

用法

舉例

name

映射的屬性(必須)

name=”SalaryList”

table

映射的數(shù)據(jù)表(可選) table=”Salary”
lazy 延遲加載(可選) Lazy=true|false
cascade 指示級(jí)聯(lián)操作方式(可選) Cascade=all
inverse 關(guān)聯(lián)由誰負(fù)責(zé)維護(hù) Inverse=”true”

當(dāng)lazy=”true”,父類初始化的時(shí)候不會(huì)自動(dòng)加載子類集合

Cascade為級(jí)聯(lián)操作方式,包括:

屬性 用法說明
none 默認(rèn)值,不進(jìn)行級(jí)聯(lián)操作
save-update save和update級(jí)聯(lián)
delete 刪除級(jí)聯(lián)
delete-orphan 刪除不相關(guān)的父對(duì)象的子對(duì)象
all save/update/delete級(jí)聯(lián)
all-delete-orphan all+delete-arphan
當(dāng)inverse=”true”的時(shí)候代表由子類維護(hù)級(jí)聯(lián)關(guān)系。這時(shí)候如果只往父類中添加子類,但不設(shè)定子類的父類,是不能保存子類的 。

4.多對(duì)多業(yè)務(wù)模型

還是用戶系統(tǒng),1個(gè)用戶職員隸屬于多個(gè)部門,同時(shí)1個(gè)部門有多個(gè)不同的職員
用戶和部門之間的數(shù)據(jù)關(guān)系圖為:
Nhibernate學(xué)習(xí)之many-to-many篇
5. 實(shí)現(xiàn)步驟:
1)User.cs

User.cs
<!--<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;

namespace NhibernateSample1
{
public class User
{
private int _id;
private string _name;
private string _pwd;
private System.Collections.IList_departmentsList;
/**/ /// <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;
}

}

/**/ /// <summary>
/// 工資列表
/// </summary>

public System.Collections.IListDepartmentsList
{
get
{
return _departmentsList;
}

set
{
_departmentsList
= value;
}

}

}

}

2)User.hbm.xml

User.hbm.xml
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <? 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 >
< bagname = " DepartmentsList " table = " Users_Departments " inverse = " true " lazy = " false " cascade = " all " >
< keycolumn = " Id " />
< many - to - many class = " NhibernateSample1.Departments,NhibernateSample1 " column = " DepID " ></ many - to - many >
</ bag >
</ class >
</ hibernate - mapping >
3) Departments.cs
Departments
<!--<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;

namespace NhibernateSample1
{
public class Departments
{
int _depID;
string _name;
IList_usersList
= new ArrayList();
public virtual int DepID
{
get
{
return _depID;
}

set
{
_depID
= value;
}

}

public virtual string Name
{
get
{
return _name;
}

set
{
_name
= value;
}

}

public virtual IListUsersList
{
get
{
return _usersList;
}

set
{
_usersList
= value;
}

}

}

}

4) Departments.hbm.xml
Departments.hbm.xml
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
< hibernate - mappingxmlns = " urn:nhibernate-mapping-2.2 " >
< class name = " NhibernateSample1.Departments,NhibernateSample1 " table = " Departments " lazy = " false " >
< idname = " DepID " column = " DepID " unsaved - value = " 0 " >
< generator class = " native " />
</ id >
< propertyname = " Name " column = " Name " type = " string " length = " 64 " not - null = " true " unique = " true " ></ property >
< bagname = " UsersList " table = " Users_Departments " lazy = " true " >
< keycolumn = " DepID " />
< many - to - many class = " NhibernateSample1.User,NhibernateSample1 " column = " Id " ></ many - to - many >
</ bag >
</ class >
</ hibernate - mapping >
5) 數(shù)據(jù)操作類
UserDepartmentFixure.cs
<!--<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 UserDepartmentFixure
{
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 UserCreateUser(Stringname, string pwd)
{
Useru
= new User();
u.Name
= name;
u.Pwd
= pwd;
u.DepartmentsList
= new ArrayList();

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 DepartmentsCreateDepartments(Useru, string name)
{
Departmentsitem
= new Departments();
item.Name
= name;
u.DepartmentsList.Add(item);
item.UsersList.Add(u);
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;

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

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

finally
{
session.Close();
}

return item;
}

public DepartmentsGetDepartments( int depID)
{
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;
try
{
tx
= session.BeginTransaction();
Departmentsitem
= (Departments)session.Load( typeof (Departments),
depID);
tx.Commit();
return item;
}

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

finally
{
session.Close();
}

return null ;
}

public UserGetUser( int uid)
{
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;
try
{
tx
= session.BeginTransaction();
Useritem
= (User)session.Load( typeof (User),
uid);
tx.Commit();
return item;
}

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

finally
{
session.Close();
}

return null ;
}


public void Delete( int uid)
{
ISessionsession
= _sessions.OpenSession();
ITransactiontx
= null ;
try
{
tx
= session.BeginTransaction();
Departmentsitem
= session.Load( typeof (Departments),uid) as Departments;
session.Delete(item);
tx.Commit();
}

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

finally
{
session.Close();
}

}


}

}

6)單元測(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的摘要說明
/// </summary>

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

NhibernateSample1.UserDepartmentFixureusf
= new UserDepartmentFixure();
其他測(cè)試屬性 #region 其他測(cè)試屬性
//
// 您可以在編寫測(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();
Useru
= usf.CreateUser(Guid.NewGuid().ToString(), " ds " );
Assert.IsTrue(u.Id
> 0 );
Departmentss
= usf.CreateDepartments(u, " 政治部 " );
Assert.IsTrue(s.DepID
> 0 );
Departmentss1
= usf.CreateDepartments(u, " 事業(yè)部 " );
Assert.IsTrue(s1.DepID
> 0 );
usf.Delete(s1.DepID);
s1
= usf.GetDepartments(s1.DepID);
Assert.IsNull(s1);
Useru1
= usf.GetUser( 1 );
Assert.IsTrue(u1.DepartmentsList.Count
> 0 );
}


}

}

到現(xiàn)在為止,終于更加體會(huì)到nhibernate的強(qiáng)大了。繼續(xù)努力,fight!
files: /Files/jillzhang/simple3.rar
上幾篇文章: Nhibernate學(xué)習(xí)之起步篇-1
Nhibernate分析之華山論劍篇
Nhibernate學(xué)習(xí)起步之many-to-one篇

Nhibernate學(xué)習(xí)之many-to-many篇


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

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

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

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

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 九九九久久国产免费 | 国产精品区免费视频 | 一区二区三区免费看 | 亚洲精品视 | 亚洲国产精品欧美综合 | 欧美精品1区2区3区 国产午夜精品理论片影院 亚洲精品不卡久久久久久 三级网站免费观看 | 成人免费一区二区三区视频软件 | 欧美三级短视频 | 亚洲一区在线播放 | 霍元甲之精武天下 | 凹凸日日摸日日碰夜夜爽孕妇 | 99久久久国产精品 | 在线中文字幕日韩 | 一区二区三区欧美在线观看 | 免费男女视频 | 中文字幕三区 | 大吊一区二区 | 一区二区三区在线电影 | 丁香婷婷色综合亚洲小说 | 91网站免费观看 | 亚洲人人| 午夜草逼 | 成年人在线视频网站 | 色噜噜狠狠色综合欧洲selulu | 久久不射网| 久久久精品午夜免费不卡 | 黄色网址入口 | 免费精品美女久久久久久久久久 | 日韩亚洲第一页 | 亚洲日韩精品AV无码富二代 | 国产午夜三级一区二区三桃花影视 | 久久久精品在线观看 | 日本欧美日韩 | 精品成人一区二区三区 | 欧美日韩三级在线观看 | 欧美视频三区 | 午夜视频网 | 一区中文| 国产97在线看| 日本精品久久久久护士 | 国产精品一二区 |