码迷,mamicode.com
首页 > Web开发 > 详细

NHibernate 组件基础 (第六篇)

时间:2015-12-09 11:36:36      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

一、组件简介

  组件(Component)可以理解为被一个对象所包含的对象而持久化,而并非一个实体。简单说来,假如数据库有FirstName,LastName这两个字段,我们在C#中可以将这两个字段提取出来作为一个Name对象使用。

  示例,首先建一张表,并添加数据如下:

  技术分享

  Person.hbm.xml

技术分享
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Model.PersonModel, Model" table="Person2">
    <id name="Id" column="Id" type="Int32">
      <generator  class="native"/>
    </id>
    <component name="Name" class="Model.NameModel,Model">
      <property name="FirstName" column="FirstName" type="String"/>
      <property name="LastName" column="LastName" type="String"/>
      <property name="FullName" formula="FirstName + LastName" type="String"/>
    </component>
    <property name="Age" column="Age" type="Int32"/>
  </class>
</hibernate-mapping>
技术分享

  Model:

技术分享
namespace Model
{
    public class PersonModel
    {
        public virtual int Id { get; set; }
        public virtual NameModel Name { get; set; }
        public virtual int Age { get; set; }
    }

    public class NameModel
    {
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual string FullName { get; set; }
    }
}
技术分享

  Program.cs

技术分享
        static void Main(string[] args)
        {
            PersonDao dao = new PersonDao();
            PersonModel p = dao.GetPerson(1);
            Console.WriteLine(p.Name.FullName);

            Console.ReadKey();
        }
技术分享

  输出结果如下:

  技术分享

   1、每当重新加载一个包含组件的对象,如果组件的所有字段为空,那么NHibernate将假定整个组件为空。

   2、组件的属性可以是NHibernate类型(包括集合、多对一关联以及其它组件)。嵌套组件不应该作为特殊的应用被考虑。

   <parent>指定组件所属对象:

  我们修改Person.hbm.xml映射文件如下:

技术分享
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Model.PersonModel, Model" table="Person2">
    <id name="Id" column="Id" type="Int32">
      <generator  class="native"/>
    </id>
    <component name="Name" class="Model.NameModel,Model">
      <parent name="NamedPerson"/>      <!-- 引用所属Person对象 -->
      <property name="FirstName" column="FirstName" type="String"/>
      <property name="LastName" column="LastName" type="String"/>
      <property name="FullName" formula="FirstName + LastName" type="String"/>
    </component>
    <property name="Age" column="Age" type="Int32"/>
  </class>
</hibernate-mapping>
技术分享

  Name类修改如下:

技术分享
    public class NameModel
    {
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual string FullName { get; set; }
        public virtual PersonModel NamedPerson { get; set; }
    }
技术分享

  这样,我们就能够转来转去:

  Console.WriteLine(p.Name.NamedPerson.Id);

  由人获得姓名对象,从姓名对象又能够找到人。

  感觉文章至此,对组件的了解还是不够,但是又实在没有办法到哪里去找到好的资料。暂时搁置。

  

 

NHibernate 组件基础 (第六篇)

标签:

原文地址:http://www.cnblogs.com/liuguanghai/p/5031914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!