1.学习目标
通过学习nhibernate基础知识,了解nhibernate对组合属性的用法。
2.开发环境和必要准备
开发环境为:windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
必要准备:学习前六篇nhibernate学习系列 , , , , ,
3.分析
在关系数据库中,为了减少冗余,每个字段会被设计的更加独立。比如一个人的名字,包括名(FirstName),姓(LastName),通常在数据库中要设计为两个字段,这样便于检索所有姓张的用户,而在对象中,一个人的姓名往往是一个组合元素,nhibernate对组合属性也有很好的支持
4.实现方法
用于组织姓名的对象UserName
public class UserName![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string _firstName;
private string _lastName;
public string FirstName![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _firstName;
}
set![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_firstName=value;
}
}
public string LastName![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _lastName;
}
set![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_lastName = value;
}
}
} 用户对象:CompositeUser.cs
public class CompositeUser![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int _uid;
UserName _name;
public int Uid![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _uid;
}
set![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_uid = value;
}
}![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public UserName 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;
}
} 用户对象的映射文件.CompositeUser.hbm.xml
<? xml version="1.0" encoding="utf-8" ?>
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" >
< class name ="NhibernateSample1.CompositeUser,NhibernateSample1" table ="CompostName" lazy ="false" >
< id name ="Uid" column ="Uid" unsaved-value ="0" >
< generator class ="native" />
</ id >
< component name ="Name" class ="NhibernateSample1.UserName,NhibernateSample1" >
< property name ="FirstName" column ="FirstName" ></ property >
< property name ="LastName" column ="LastName" ></ property >
</ component >
</ class >
</ hibernate-mapping > 注意,compenent就是映射组合属性的节点,它的子节点property代表其组合属性
CompositeUser数据表的表结构为
新增一个CompositeUser对象
public int AddCompositeUser(CompositeUser u)![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
session = NhibernateSample1.NHibernateHelper.GetCurrentSession();
ITransaction tra = session.BeginTransaction();
try![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int res =(int)session.Save(u);
tra.Commit();
return res;
}
catch![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tra.Rollback();
}
finally![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
session.Close();
}
return -1;
} 测试代码
[TestMethod]
public void Test1()![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
usf.Configure();
usf.ExportTables();
CompositeUser u = new CompositeUser();
UserName name = new UserName();
name.FirstName = "zhang";
name.LastName = "jill";
u.Name = name;
int res =new NhibernateSample1.UserFixure().AddCompositeUser(u);
Assert.IsTrue(res>0);
} nhibernate真的是非常强大的一个框架,下篇将学习复杂组合的使用方法。今天先到这。睡了