通过学习nhibernate基础知识,了解nhibernate对组合属性的用法。
2.开发环境和必要准备
开发环境为:windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
必要准备:学习前六篇nhibernate学习系列 Nhibernate学习之起步篇-1 , Nhibernate学习起步之many-to-one篇 , Nhibernate学习之many-to-many篇 , nhibernate学习之三级联(Ternary Associations)篇 , Nhibernate学习之性能改善1 , nhibernate性能之二级缓存篇
3.分析
在关系数据库中,为了减少冗余,每个字段会被设计的更加独立。比如一个人的名字,包括名(FirstName),姓(LastName),通常在数据库中要设计为两个字段,这样便于检索所有姓张的用户,而在对象中,一个人的姓名往往是一个组合元素,nhibernate对组合属性也有很好的支持
4.实现方法
用于组织姓名的对象UserName
public
class
UserName
{
private string _firstName;
private string _lastName;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName=value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
}
{
private string _firstName;
private string _lastName;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName=value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
}
用户对象:CompositeUser.cs
public
class
CompositeUser
{
int _uid;
UserName _name;
public int Uid
{
get
{
return _uid;
}
set
{
_uid = value;
}
}
public UserName Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
用户对象的映射文件.CompositeUser.hbm.xml
{
int _uid;
UserName _name;
public int Uid
{
get
{
return _uid;
}
set
{
_uid = value;
}
}
public UserName Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
<?
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代表其组合属性
< 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 >
CompositeUser数据表的表结构为
新增一个CompositeUser对象
public
int
AddCompositeUser(CompositeUser u)
{
session = NhibernateSample1.NHibernateHelper.GetCurrentSession();
ITransaction tra = session.BeginTransaction();
try
{
int res =(int)session.Save(u);
tra.Commit();
return res;
}
catch
{
tra.Rollback();
}
finally
{
session.Close();
}
return -1;
}
测试代码
{
session = NhibernateSample1.NHibernateHelper.GetCurrentSession();
ITransaction tra = session.BeginTransaction();
try
{
int res =(int)session.Save(u);
tra.Commit();
return res;
}
catch
{
tra.Rollback();
}
finally
{
session.Close();
}
return -1;
}
[TestMethod]
public void Test1()
{
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真的是非常强大的一个框架,下篇将学习复杂组合的使用方法。今天先到这。睡了
public void Test1()
{
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);
}