首页 > 代码库 > 顺藤摸瓜【Nhibernate 关联映射--多对一】

顺藤摸瓜【Nhibernate 关联映射--多对一】

现有两个实体,Dog Master,映射到数据库表中如上图所示。一个Dog只允许对应一个Master,但一个Master可以有多个Dog。我们在查询Dog的时候,往往还需要知道其主人Master的信息,也就是说,如果已知一个Dog的信息,想一次来顺藤摸瓜找到Master的信息,此时用Nhibernate如何去实现呐?这里就用到了多对一的关联映射方法。看我是如何实现的:

 

Nhibernate的整个搭建过程就不在赘述了,前面的两篇文章都已经详细展示了一下,下面仅仅展示多对一的实现方法。

首先构建实体类DogMaster

Master
    public class Master
    {
        private string id;
        private string name;

        public Master()
        {
        }

        public virtual string Id
        {
            get { return id; }
            set { id = value; }
        }

        public virtual string Name
        {
            get { return name; }
            set { name = value; }
Dog
    public class Dog
    {
        private string id;
        private string name;
        private string sex;
        private string weight;
        private Master master;

        public Dog()
        {
        }

        public virtual string Id
        {
            get { return id; }
            set { id = value; }
        }

        public virtual string Name
        {
            get { return name; }
            set { name = value; }
        }

        public virtual string Sex
        {
            get { return sex; }
            set { sex = value; }
        }

        public virtual string Weight
        {
            get { return weight; }
            set { weight = value; }
        }

        public virtual  Master Master   //这里除了dog的基本属性外,还添加了Master类这个属性
        {
            get{ return master ; } 
            set {  master=value ; } 
        }

继续构建.hbm.xml 

Master
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="Model" assembly="Model">

  <class name="Master" table="Master">

    <id name="Id">
      <column name="MasterId" sql-type="char(32)" />
      <generator class="assigned" />
    </id>

    <property name="Name">
      <column name="Name"  sql-type="nvarchar(16)" />
    </property>

  </class>
</hibernate-mapping>
Dog
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="Model" assembly="Model">

  <class name="Dog" table="Dog" lazy="true">

    <id name="Id">
      <column name="DogId" sql-type="char(32)" />
      <generator class="assigned" />
    </id>

    <property name="Name">
      <column name="Name"  sql-type="nvarchar(16)" />
    </property>
    
    <property name="Sex"  >
      <column name="Sex" sql-type="nvarchar(16)" />
    </property>
    
    <property name="Weight"  >
      <column name="Weight" sql-type="nvarchar(16)" />
    </property>

    <many-to-one name="Master" column="MasterId" />  <!-- 这里多了这个标签,指明对应表中外键“MasterID”对应的是实体Master所对应表记录-->
    
  </class>
</hibernate-mapping>
客户端实现

<span style="font-family:FangSong_GB2312;font-size:14px;"><strong>插入记录</strong></span>
            Master master = new Master();
            master.Id = "masterid";
            master.Name = txtMName.Text.ToString();
            session.Save(master);

            Dog prince = new Dog();
            prince.Id = "dogid";
            prince.Name = txtDName .Text .ToString ();
            prince.Sex = txtSex .Text .ToString();
            prince.Weight = txtWeight .Text .ToString ();
            prince.Master = master;
<span style="font-family:FangSong_GB2312;font-size:14px;"><strong>查询记录</strong></span>
            Dog prince = new Dog();
            prince = (Dog)session.Get ("Dog","dogid" );

            txtinformation.Text = "赤丸的主人是:"+prince.Master.Name; //查出dog对象的同时,其Master的信息也就查了出来。

这是多对一关联映射的实现。关联映射还有一对一、一对多、多对多。这些映射的出现是为了增强Nhibernate的查询功能。更多的关联映射,接下来会继续研究学习。。

顺藤摸瓜【Nhibernate 关联映射--多对一】