nhibernate学习之集合组合依赖

1.学习目标
   还是学习compenent的用法,上节实现了简单字段的组合,这节中将讨论两个问题:1.依赖对象有一个指向容器对象的引用。2。集合依赖
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性能之二级缓存篇 , nhibernate学习之简单组合的映射
3.通过parent为依赖组合对象映射一个指向容器对象的引用
  CompositeUser.cs 
ContractedBlock.gif ExpandedBlockStart.gif
None.gifpublic 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        }

ExpandedBlockEnd.gif    }
  UserName.cs
ContractedBlock.gif ExpandedBlockStart.gif
None.gif public class UserName
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private string _firstName;
InBlock.gif        
private string _lastName;
InBlock.gif        CompositeUser _user;
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        }

InBlock.gif        
public CompositeUser User
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _user;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.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 ="CompositeUser"  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            
< parent  name ="User" ></ parent >
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 >
注意parent是指向容器对象的引用
加载一个CompositeUser对象,结果如图

4. 集合组合依赖
Composite.cs
ContractedBlock.gif ExpandedBlockStart.gif
None.gifpublic class CompositeUser
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
int _uid;
InBlock.gif        UserName _name;
InBlock.gif        ISet _userNames 
= new HashedSet();
InBlock.gif        DateTime _birthDay 
= DateTime.Now;
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        
public ISet UserNames
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _userNames;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public DateTime BirthDay
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _birthDay;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
UserName.cs
ContractedBlock.gif ExpandedBlockStart.gif
None.gifpublic class UserName
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
private string _firstName;
InBlock.gif        
private string _lastName;
InBlock.gif        CompositeUser _user;
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        }

InBlock.gif        
public CompositeUser User
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _user;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.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 ="CompositeUser"  lazy ="false" >
None.gif        
< id  name ="Uid"  column ="Uid"  unsaved-value ="0" >
None.gif            
< generator  class ="native"   />
None.gif        
</ id >
None.gif        
< set  name ="UserNames"  table ="UserNames"  lazy ="true" >
None.gif            
< key  column ="Uid" />
None.gif            
< composite-element   class ="NhibernateSample1.UserName,NhibernateSample1" >
None.gif                
< property  name ="FirstName"  column ="FirstName" ></ property >
None.gif                
< property  name ="LastName"  column ="LastName" ></ property >
None.gif            
</ composite-element  >
None.gif        
</ set >
None.gif        
< property  name ="BirthDay"  type ="DateTime" ></ property >
None.gif    
</ class >
None.gif
</ hibernate-mapping >
注意:Composite可以包含集合也可以不包含集合,上面这样的配置就不包括集合,映射的属性可以选择为List,map,bag,idbag
运行添加一个Composite的测试代码,会在数据库中建立两个数据表 CompositeUser和UserNames
效果图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值