- 学习目的:
通过进一步学习Nhibernate基础知识,掌握用Nhiberate实现多对多的业务逻辑
- 开发环境+必要准备
开发环境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
前期准备: 学习上两篇单表操作和many-to-one篇
3.对上篇文章的部分解释
1)bag节点:用于定义System.Collection.IList类型的集合元素。
属性 | 用法 | 举例 |
name | 映射的属性(必须) | name=”SalaryList” |
table | 映射的数据表(可选) | table=”Salary” |
lazy | 延迟加载(可选) | Lazy=true|false |
cascade | 指示级联操作方式(可选) | Cascade=all |
inverse | 关联由谁负责维护 | Inverse=”true” |
当lazy=”true”,父类初始化的时候不会自动加载子类集合
Cascade为级联操作方式,包括:
属性 | 用法说明 |
none | 默认值,不进行级联操作 |
save-update | save和update级联 |
delete | 删除级联 |
delete-orphan | 删除不相关的父对象的子对象 |
all | save/update/delete级联 |
all-delete-orphan | all+delete-arphan |
当inverse=”true”的时候代表由子类维护级联关系。这时候如果只往父类中添加子类,但不设定子类的父类,是不能保存子类的。
4.多对多业务模型
还是用户系统,1个用户职员隶属于多个部门,同时1个部门有多个不同的职员
用户和部门之间的数据关系图为:
![](https://i-blog.csdnimg.cn/blog_migrate/573c3a16cee464dc3d8c61c0b9f00106.jpeg)
5. 实现步骤:
1)User.cs
User.cs
using System;
using System.Collections.Generic;
using System.Text;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace NhibernateSample1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class User
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private int _id;
private string _name;
private string _pwd;
private System.Collections.IList _departmentsList;
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 编号
/// </summary>
public virtual int Id
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _id;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_id = value;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 名称
/// </summary>
public virtual string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _name;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_name = value;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 密码
/// </summary>
public virtual string Pwd
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _pwd;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_pwd = value;
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 工资列表
/// </summary>
public System.Collections.IList DepartmentsList
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _departmentsList;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_departmentsList = value;
}
}
}
}
2)User.hbm.xml
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>
<bag name="DepartmentsList" table="Users_Departments" inverse="true" lazy="false" cascade="all">
<key column="Id"/>
<many-to-many class="NhibernateSample1.Departments,NhibernateSample1" column="DepID"></many-to-many>
</bag>
</class>
</hibernate-mapping>
3) Departments.cs
Departments
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace NhibernateSample1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class Departments
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int _depID;
string _name;
IList _usersList= new ArrayList();
public virtual int DepID
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _depID;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_depID = value;
}
}
public virtual string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _name;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_name = value;
}
}
public virtual IList UsersList
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _usersList;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_usersList = value;
}
}
}
}
4) Departments.hbm.xml
Departments.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NhibernateSample1.Departments,NhibernateSample1" table="Departments" lazy="false">
<id name="DepID" column="DepID" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" column="Name" type="string" length="64" not-null="true" unique="true"></property>
<bag name="UsersList" table="Users_Departments" lazy="true" >
<key column="DepID"/>
<many-to-many class="NhibernateSample1.User,NhibernateSample1" column="Id"></many-to-many>
</bag>
</class>
</hibernate-mapping>
5) 数据操作类
UserDepartmentFixure.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace NhibernateSample1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class UserDepartmentFixure
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private ISessionFactory _sessions;
public void Configure()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Configuration cfg = GetConfiguration();
_sessions = cfg.BuildSessionFactory();
}
Configuration GetConfiguration()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Configuration cfg = GetConfiguration();
new SchemaExport(cfg).Create(true, true);
}
public User CreateUser(String name,string pwd)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
User u = new User();
u.Name = name;
u.Pwd = pwd;
u.DepartmentsList = new ArrayList();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ISession session = _sessions.OpenSession();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ITransaction tx = null;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tx = session.BeginTransaction();
session.Save(u);
tx.Commit();
}
catch (HibernateException e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (tx != null) tx.Rollback();
throw e;
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
session.Close();
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return u;
}
public Departments CreateDepartments(User u, string name)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Departments item = new Departments();
item.Name=name;
u.DepartmentsList.Add(item);
item.UsersList.Add(u);
ISession session = _sessions.OpenSession();
ITransaction tx = null;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tx = session.BeginTransaction();
session.Save(item);
tx.Commit();
}
catch (HibernateException e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (tx != null) tx.Rollback();
throw e;
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
session.Close();
}
return item;
}
public Departments GetDepartments(int depID)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ISession session = _sessions.OpenSession();
ITransaction tx = null;
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tx = session.BeginTransaction();
Departments item = (Departments)session.Load(typeof(Departments),
depID);
tx.Commit();
return item;
}
catch (HibernateException e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (tx != null) tx.Rollback();
return null;
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
session.Close();
}
return null;
}
public User GetUser(int uid)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ISession session = _sessions.OpenSession();
ITransaction tx = null;
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tx = session.BeginTransaction();
User item = (User)session.Load(typeof(User),
uid);
tx.Commit();
return item;
}
catch (HibernateException e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (tx != null) tx.Rollback();
return null;
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
session.Close();
}
return null;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Delete(int uid)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ISession session = _sessions.OpenSession();
ITransaction tx = null;
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tx = session.BeginTransaction();
Departments item = session.Load(typeof(Departments), uid) as Departments;
session.Delete(item);
tx.Commit();
}
catch (HibernateException e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (tx != null) tx.Rollback();
throw e;
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
session.Close();
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
6)单元测试类
UnitTest1.cs
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NhibernateSample1;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace TestProject1
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// UnitTest1 的摘要说明
/// </summary>
[TestClass]
public class UnitTest1
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public UnitTest1()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
NhibernateSample1.UserDepartmentFixure usf = new UserDepartmentFixure();
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
其他测试属性#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
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[TestMethod]
public void Test1()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
usf.Configure();
usf.ExportTables();
User u = usf.CreateUser(Guid.NewGuid().ToString(), "ds");
Assert.IsTrue(u.Id>0);
Departments s = usf.CreateDepartments(u, "政治部");
Assert.IsTrue(s.DepID > 0);
Departments s1 = usf.CreateDepartments(u, "事业部");
Assert.IsTrue(s1.DepID > 0);
usf.Delete(s1.DepID);
s1 = usf.GetDepartments(s1.DepID);
Assert.IsNull(s1);
User u1 = usf.GetUser(1);
Assert.IsTrue(u1.DepartmentsList.Count>0);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
到现在为止,终于更加体会到nhibernate的强大了。继续努力,fight!
files:
/Files/jillzhang/simple3.rar
上几篇文章:
Nhibernate学习之起步篇-1
Nhibernate分析之华山论剑篇
Nhibernate学习起步之many-to-one篇