博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nhibernate学习之集合组合依赖
阅读量:4513 次
发布时间:2019-06-08

本文共 3096 字,大约阅读时间需要 10 分钟。

1.学习目标
   还是学习compenent的用法,上节实现了简单字段的组合,这节中将讨论两个问题:1.依赖对象有一个指向容器对象的引用。2。集合依赖
2.开发环境和必要准备
   开发环境为:windows 2003,Visual studio .Net 2005,Sql server 2005 developer edition
  必要准备:学习前六篇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
效果图

转载于:https://www.cnblogs.com/jillzhang/archive/2007/04/04/700526.html

你可能感兴趣的文章
删除字符串中指定子串
查看>>
day40-socket编程
查看>>
SpringBoot里mybatis查询结果为null的列不返回问题的解决方案
查看>>
为什么留不住优秀的员工
查看>>
Django后台管理admin笔记
查看>>
JavaScript中的变量
查看>>
从零开始搭建系统1.5——Redis安装及配置
查看>>
multipart/form-data和application/x-www-form-urlencoded的区别
查看>>
python管理Windows服务
查看>>
Dynamic CRM 中修改实体中主字段的长度
查看>>
更新ruby
查看>>
css写的手机网站页面如何使网页左右固定不动?
查看>>
解析 Linux 中的 VFS 文件系统机制
查看>>
P1531 I Hate It
查看>>
ecshop调用指定商品分类下的商品
查看>>
iptables基本原理和规则配置
查看>>
黑马程序员——Objective-C——点语法、成员变量作用域、@property和@synthesize、id指针、构造方法...
查看>>
java调用matlab函数
查看>>
IOS自定义仪表盘
查看>>
第5次作业_078_刘玲志
查看>>