1.
学习目的:
通过进一步学习nhibernate基础知识,在实现单表CRUD的基础上,实现两表之间one-to-many的关系.
2. 开发环境+必要准备
开发环境: windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
必要准备: 学习上篇文章单表操作
3. 对上篇文章中部分解释
1)在User.hbm.xml中class节点中有一个lazy的属性,这个属性用于指定是否需要延迟加载(lazy loading),在官方文档中称为:lazy fecting.可以说延迟加载是nhibernate最好的特点,因为它可以在父类中透明的加载子类集合,这对于many-to-one的业务逻辑中,真是方便极了。但是有些时候,父类是不需要携带子类信息的。这时候如果也加载,无疑对性能是一种损失。在映射文件的class节点中可以通过配置lazy属性来指定是否支持延迟加载,这就更灵活多了。
2) 在User.hbm.xml中generate节点,代表的是主键的生成方式,上个例子中的”native”根据底层数据库的能力选择identity,hilo,sequence中的一个,比如在MS Sql中,使我们最经常使用的自动增长字段,每次加1.
3) 在NHibernateHelper.cs中,创建Configuration对象的代码:new Configuration().Configure(@"E:\myproject\nhibernatestudy\simle1\NHibernateStudy1\NhibernateSample1\hibernate.cfg.xml");因为我是在单元测试中调试,所以将绝对路径的配置文件传递给构造函数。如果在windows app或者web app可以不用传递该参数。
4. 实现步骤
1)确定实现的业务需求:用户工资管理系统
2) 打开上篇文章中的NHibernateStudy1解决方案。向项目NhibernateSample1添加类Salary;代码如下
Salary.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 partial class Salary
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int _id;
User _user;
int _year;
int _month;
int _envy;
decimal _money;
![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;
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 雇员
/// </summary>
public virtual User Employee
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _user;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_user = value;
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 年度
/// </summary>
public int Year
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _year;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_year = value;
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 月份
/// </summary>
public int Month
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _month;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_month = value;
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 季度
/// </summary>
public int Envy
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _envy;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_envy = value;
}
}
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 工资
/// </summary>
public decimal Money
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _money;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_money = value;
}
}
}
}
3) 更改User.cs,在User里面添加SalaryList属性:
User.cs
1
private System.Collections.IList _salaryList;
2![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
3
/// 工资列表
4
/// </summary>
5
public System.Collections.IList SalaryList
6![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
7
get
8![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
return _salaryList;
10
}
11
set
12![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
_salaryList = value;
14
}
15
}
4)修改User.hbm.xml,加入bag节点
User.hbm.xml
<bag name="SalaryList" table="Salary" inverse="true" lazy="true" cascade="all">
<key column="Id"/>
<one-to-many class="NhibernateSample1.Salary,NhibernateSample1"></one-to-many>
</bag>
5
)编写类Salary的映射文件:Salary.hbm.xml
Salary.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NhibernateSample1.Salary,NhibernateSample1" table="Salary" lazy="false">
<id name="Id" column="Id" unsaved-value="0">
<generator class="native" />
</id>
<property name="Year" column="Year" type="Int32" not-null="true"></property>
<property name="Month" column="Month" type="Int32" not-null="true"></property>
<property name="Envy" column="Envy" type="Int32" not-null="true"></property>
<property name="Money" column="Money" type="Decimal" not-null="true"></property>
<many-to-one name="Employee" column="Uid" not-null="true"></many-to-one>
</class>
</hibernate-mapping>
6
)编写CRUD类
UserSalaryFixure.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 UserSalaryFixure
![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.SalaryList = 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 Salary CreateSalary(User u, int year,int month,int envy,decimal money)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Salary item = new Salary();
item.Year = year;
item.Money = money;
item.Envy = envy;
item.Month = month;
item.Employee = u;
u.SalaryList.Add(item);
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.Update(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();
}
return item;
}
public Salary CreateSalary(int uid,int year, int month, int envy, decimal money)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Salary item = new Salary();
item.Year = year;
item.Money = money;
item.Envy = envy;
item.Month = month;
ISession session = _sessions.OpenSession();
ITransaction tx = null;
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tx = session.BeginTransaction();
User u = (User)session.Load(typeof(User), uid);
item.Employee = u;
u.SalaryList.Add(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 Salary GetSalary(int salaryID)
![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();
Salary item = (Salary)session.Load(typeof(Salary),
salaryID);
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;
}
public void UpdateSalary(int salaryID, decimal money)
![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();
Salary item = (Salary)session.Load(typeof(Salary),
salaryID);
item.Money = money;
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)
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();
Salary item = session.Load(typeof(Salary), uid) as Salary; ;
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)
}
}
7)
编写单元测试类:UnitTest1.cs
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.UserSalaryFixure usf = new UserSalaryFixure();
![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);
Salary s = usf.CreateSalary(u, 2007, 3, 1, (decimal)8000.00);
Assert.IsTrue(s.Id > 0);
Salary s1 = usf.CreateSalary(u.Id, 2007, 3, 1, (decimal)7500);
Assert.IsTrue(s1.Id>0);
usf.UpdateSalary(s1.Id, (decimal)6000);
s1 = usf.GetSalary(s1.Id);
Assert.IsTrue(s1.Money == (decimal)6000);
usf.Delete(s1.Id);
s1 = usf.GetSalary(s1.Id);
Assert.IsNull(s1);
User u1 = usf.GetUser(1);
Assert.IsTrue(u1.SalaryList.Count>0);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
加载测试元数据,直到Test()通过。
总结:通过进一步学习nhiberate,发现ORM框架真是非常强大。今天先到这里。明天继续。
项目文件:
/Files/jillzhang/simple2.rar