nhibernate学习之简单组合的映射

1.学习目标
   通过学习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学习之性能改善1nhibernate性能之二级缓存篇
3.分析
   在关系数据库中,为了减少冗余,每个字段会被设计的更加独立。比如一个人的名字,包括名(FirstName),姓(LastName),通常在数据库中要设计为两个字段,这样便于检索所有姓张的用户,而在对象中,一个人的姓名往往是一个组合元素,nhibernate对组合属性也有很好的支持
4.实现方法
   用于组织姓名的对象UserName  
None.gif public   class  UserName
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private string _firstName;
InBlock.gif        
private string _lastName;
InBlock.gif        
public string FirstName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _firstName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _firstName
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string LastName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _lastName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _lastName 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

用户对象:CompositeUser.cs
None.gif   public   class  CompositeUser
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
int _uid;
InBlock.gif        UserName _name;
InBlock.gif        
public int Uid
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _uid;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _uid 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public UserName Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _name;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _name 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
用户对象的映射文件.CompositeUser.hbm.xml
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.2" >
None.gif    
< class  name ="NhibernateSample1.CompositeUser,NhibernateSample1"  table ="CompostName"  lazy ="false" >
None.gif        
< id  name ="Uid"  column ="Uid"  unsaved-value ="0" >
None.gif            
< generator  class ="native"   />
None.gif        
</ id >
None.gif        
< component  name ="Name"  class ="NhibernateSample1.UserName,NhibernateSample1" >
None.gif            
< property  name ="FirstName"  column ="FirstName" ></ property >
None.gif            
< property  name ="LastName"  column ="LastName" ></ property >
None.gif        
</ component >
None.gif    
</ class >
None.gif
</ hibernate-mapping >
注意,compenent就是映射组合属性的节点,它的子节点property代表其组合属性
CompositeUser数据表的表结构为

新增一个CompositeUser对象
None.gif   public   int  AddCompositeUser(CompositeUser u)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            session 
= NhibernateSample1.NHibernateHelper.GetCurrentSession();
InBlock.gif            ITransaction tra 
= session.BeginTransaction();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int res =(int)session.Save(u);
InBlock.gif                tra.Commit();
InBlock.gif                
return res;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tra.Rollback();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session.Close();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return -1;
ExpandedBlockEnd.gif        }
测试代码
None.gif   [TestMethod]
None.gif        
public   void  Test1()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            usf.Configure();
InBlock.gif            usf.ExportTables();
InBlock.gif            CompositeUser u 
= new CompositeUser();
InBlock.gif            UserName name 
= new UserName();
InBlock.gif            name.FirstName 
= "zhang";
InBlock.gif            name.LastName 
= "jill";
InBlock.gif            u.Name 
= name;
InBlock.gif            
int res =new NhibernateSample1.UserFixure().AddCompositeUser(u);
InBlock.gif            Assert.IsTrue(res
>0);
ExpandedBlockEnd.gif        }
nhibernate真的是非常强大的一个框架,下篇将学习复杂组合的使用方法。今天先到这。睡了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值