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 

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;
            }

        }

    }
  UserName.cs

 public class UserName
    
{
        
private string _firstName;
        
private string _lastName;
        CompositeUser _user;
        
public string FirstName
        
{
            
get
            
{
                
return _firstName;
            }

            
set
            
{
                _firstName
=value;
            }

        }

        
public string LastName
        
{
            
get
            
{
                
return _lastName;
            }

            
set
            
{
                _lastName 
= value;
            }

        }

        
public CompositeUser User
        
{
            
get
            
{
                
return _user;
            }

            
set
            
{
                _user 
= 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="CompositeUser" lazy="false">
        
<id name="Uid" column="Uid" unsaved-value="0">
            
<generator class="native" />
        
</id>
        
<component name="Name" class="NhibernateSample1.UserName,NhibernateSample1">
            
<parent name="User"></parent>
            
<property name="FirstName" column="FirstName"></property>
            
<property name="LastName" column="LastName"></property>
        
</component>
    
</class>
</hibernate-mapping>
注意parent是指向容器对象的引用
加载一个CompositeUser对象,结果如图

4. 集合组合依赖
Composite.cs

public class CompositeUser
    
{
        
int _uid;
        UserName _name;
        ISet _userNames 
= new HashedSet();
        DateTime _birthDay 
= DateTime.Now;
        
public int Uid
        
{
            
get
            
{
                
return _uid;
            }

            
set
            
{
                _uid 
= value;
            }

        }

        
public ISet UserNames
        
{
            
get
            
{
                
return _userNames;
            }

            
set
            
{
                _userNames 
= value;
            }

        }

        
public DateTime BirthDay
        
{
            
get
            
{
                
return _birthDay;
            }

            
set
            
{
                _birthDay 
= value;
            }

        }

    }
UserName.cs

public class UserName
    
{
        
private string _firstName;
        
private string _lastName;
        CompositeUser _user;
        
public string FirstName
        
{
            
get
            
{
                
return _firstName;
            }

            
set
            
{
                _firstName
=value;
            }

        }

        
public string LastName
        
{
            
get
            
{
                
return _lastName;
            }

            
set
            
{
                _lastName 
= value;
            }

        }

        
public CompositeUser User
        
{
            
get
            
{
                
return _user;
            }

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

转载于:https://www.cnblogs.com/wangzhanjianshe/archive/2008/02/21/2326478.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/463988.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

追更这个做嵌入式的大佬

在知乎上看到一个做嵌入式91年小年轻&#xff0c;分享给大家在他看来&#xff0c;嵌入式也是一个很吃香的技术&#xff0c;在周末写这篇文章的时候&#xff0c;也刚收到一个朋友的微信消息&#xff0c;他说自己拿到了70多万的年包offer。大家想追更作者的原文&#xff0c;可以点…

CentOS6.5安装ElasticSearch6.2.3

CentOS6.5安装ElasticSearch6.2.3 1、Elastic 需要 Java 8 环境。&#xff08;安装步骤&#xff1a;http://www.cnblogs.com/hunttown/p/5450463.html&#xff09; 2、安装包下载&#xff1a; #官网地址 https://www.elastic.co/downloads/elasticsearch 3、新建用户 Elastic高…

C#中跨窗体操作(1)--事件

在应用C#过程中&#xff0c;经常会出现通过“窗口2”来处理“窗口1”上的控件和显示内容等。可以通过事件的处理方式来完成&#xff0c;具体步骤如下&#xff1a; 1、在“窗体2”中声明事件 public delegate void Change(); public event Change myChangeevent; 2、编写…

boost中bind的使用

最近对boost的bind部分比较感兴趣&#xff0c;对其背后的机制进行了简单的分析&#xff0c;和大家分享一下。 注&#xff0c;我所看的代码是boost_1_64_0&#xff0c; 想来各个版本的差异不大。 定义函数 [cpp] view plaincopy int f(int a, int b) { return a b; }…

这道字符串反转的题目,你能想到更好的方法吗?

周末有一个朋友问了一个笔试题目&#xff0c;当时还直播写了答案&#xff0c;但是总觉得写得不够好&#xff0c;现在把题目放出来。大家看看有没有什么更好的解法题目有一个字符串&#xff0c;如下&#xff0c;要求对字符串做反转后输出//input the sky is blue//output blue …

WinAPI: 钩子回调函数之 SysMsgFilterProc

SysMsgFilterProc(nCode: Integer; {}wParam: WPARAM; {}lParam: LPARAM {} ): LRESULT; {}//待续...转载于:https://www.cnblogs.com/del/archive/2008/02/25/1080722.html

流媒体服务器搭建实例——可实现录音,录像功能

由于我也是刚开始接触这个东东&#xff0c;原理什么的不是很清楚&#xff0c;这里我就不说了&#xff0c;免得误人子弟&#xff0c;嘿嘿&#xff01;第一步&#xff0c;下载FlashMediaServer3.5&#xff0c;网上有很多资源&#xff0c;这里就不提供了&#xff0c;大家google一下…

使用HanLP增强Elasticsearch分词功能

hanlp-ext 插件源码地址&#xff1a;http://git.oschina.net/hualongdata/hanlp-ext 或 https://github.com/hualongdata/hanlp-ext Elasticsearch 默认对中文分词是按“字”进行分词的&#xff0c;这是肯定不能达到我们进行分词搜索的要求的。官方有一个SmartCN 中文分词插件&…

boost::function的用法(二)

boost function是一组类和模板组合&#xff0c;用于包装各种函数。从功能上&#xff0c;它类似于函数指针&#xff0c;但是比函数指针的功能更强大。 使用boost function&#xff0c;必须包含头文件 [cpp] view plaincopy #include <boost/function.hpp> 除了头文件外&…

一个女孩子居然做了十年硬件。​。。

本文转自面包板社区。--正文--2011年&#xff0c;一个三本大学的电子信息专业的大三女学生跟2个通信专业的大二男生组成了一组代表学校参加2011年“瑞萨杯”全国大学生电子设计大赛&#xff0c;很意外的获得了湖北赛区省三等奖&#xff0c;虽然很意外&#xff0c;但还是挺高兴的…

自动备份多个MOSS站点集的脚本

写了个自动备份多个站点集的脚本&#xff08;backupscript.bat&#xff09;可以生成文件名如"Site80_20080227.bak"的备份文件。 echo off echo echo Backup of MOSS site! echo Written by WangWei(shangmeizhaihotmail.com) echo set yyyy%Date:~0,4% set mm%Dat…

Chrome Version 19.0.1055.1 dev Flash Missing plug-in的修复

linux mint12 64位&#xff0c;以前一直用chrome好好的&#xff0c;直到更新到 Version 19.0.1055.1 dev版本&#xff0c;问题一大堆&#xff0c;最纠结的是flash不能用了&#xff0c;更新前是正常的&#xff0c;一直也没有找到原因&#xff0c;通过命令行启动google-chrome发现…

记录上一个项目踩过的坑

1、objecthtmldivelement对象 var avc document.getElementById("div1"); alert(avc ); 为objecthtmldivelement对象&#xff0c;objecthtmldivelement对象相当于document.getElementById("div1")。2、获取img的src function imgsrc(){var y document.g…

之前字符串反转的题目

之前发的字符串反转的题目这道字符串反转的题目&#xff0c;你能想到更好的方法吗&#xff1f;有很多人评论了&#xff0c;有的人还写了自己的解题思路&#xff0c;还有人写了自己的代码还有其中呼声很高的压栈解法我相信很多人在笔试的时候一定会遇到这类题目&#xff0c;给你…

C++ 多重继承之内存存储

C 之多重继承 1. C中class与struct。 在C里面&#xff0c;class与struct没有本质的区别&#xff0c;只是class的默认权限是private&#xff0c;而struct则是public。这个概念也揭示了一点&#xff1a;class和struct在内部存储结构上是一致的。所以我们可以利用这一点来探讨clas…

hdu 3488

可以作为KM 二分图最大权匹配模板 View Code #include <stdio.h>#include <iostream>#include <string.h>using namespace std;const int N210;const int inf0x2fffffff;const int Max20000;int match[N],n,m,lack,w[N][N],lx[N],ly[N];bool vx[N],vy[N];bo…

Find The Multiple POJ - 1426 (BFS)

题目大意 给定一个整数&#xff0c;寻找一个只有0,1构成的十进制数使得这个数能够整除这个整数 解法 直接bfs第一位放入1&#xff0c;之后每一位放入1或者0 代码 #include <iostream> #include <queue> using namespace std; int n; void bfs() {queue<long lon…

心情不好,我就这样写代码

在 GitHub 上有一个项目&#xff0c;它描述了「最佳垃圾代码」的十九条关键准则。从变量命名到注释编写&#xff0c;这些准则将指导你写出最亮眼的烂代码。为了保持与原 GitHub 项目一致的风格&#xff0c;下文没有进行转换。读者们可以以相反的角度来理解所有观点&#xff0c;…

C++中默认构造函数使用时的要点

最近写代码的时候发现一个奇怪的现象&#xff1a;当我声明一个无参构造函数时&#xff0c;如果后面加上括号&#xff0c;声明出的对象就不能显示。比如下面的代码&#xff1a; [cpp] view plaincopy #include <stdio.h> class Test { public: Test() { …

一个立即关闭显示器的小软件(Masm开发,只有3KB大小)

我们在用电脑听歌或者下载网络资源时&#xff0c;经常都是不需要开着显示器的&#xff0c;这样不仅可以省电&#xff0c;最重要的是可以延长显示器的使用寿命&#xff0c;当然&#xff0c;对于笔记本电脑的电池省电也是很重要的。另外&#xff0c;对于液晶显示器&#xff0c;由…