可以在NHibernate中加入1个表和1个表超过1次吗?

时间:2012-09-01 13:03:59

标签: c# nhibernate

当连接1个表与1个表超过1次时,我遇到了NHibernate的问题。 我使用ICriteria。 我有2张桌子。让我们看看:

tblEmployees:

EmployeeID (PK)
Gender (will join with table tblCommons) (this is Key data in tblCommons)
Status (will join with table tblCommons) (this is Key data in tblCommons)

注意:tblEmployees没有任何PK的tblCommons。

tblCommons:

ID (PK)
Code (will use to specific Gender data and Status data) (ex: 1 is Gender, 2 is Status)
Key
Value

这是tblEmployees中的数据:

EmployeeID                 Gender                  Status 
  1                          1                        1
  2                          1                        2
  3                          2                        1

这是tblCommons中的数据

ID                  Code                  Key                   Value
1                     1                     1                   Male
2                     1                     2                   Female
3                     2                     1                   Active
4                     2                     2                   Inactive

在Employees.hbm.xml文件中

<class name="Demo.Employees, Demo" table="tblEmployees">
    <id name="EmployeeID" column="EmployeeID" type="String">
    </id>
    <many-to-one class="Demo.Commons, Demo" name="Gender" column="Gender" />
    <many-to-one class="Demo.Commons, Demo" name="Status" column="Status" />
</class>

在员工班级

namespace Demo
{
    public class Employees
    {
        public Employees(){}
        public virtual string EmployeeID { get; set; }
        public virtual Commons Gender{ get; set; }
        public virtual Commons Status{ get; set; }
    }
}

在Commons.hbm.xml

<class name="Demo.Commons, Demo" table="tblCommons">
    <id name="Key" column="Key" type="String">
    </id>
    <property name="Code" column="Code" type="String" />
    <property name="Value" column="Value" type="String" />
    <set name="Genders" table="tblEmployees" generic="true" inverse="true">
      <key column="Gender" />
      <one-to-many class="Demo.Employees, Demo"/>
    </set>
    <set name="Status" table="tblEmployees" generic="true" inverse="true">
      <key column="Status" />
      <one-to-many class="Demo.Employees, Demo"/>
    </set>
</class>

注意:在xml中,我设置“id”是“Key”,因为我在NHibernate中看到,“id”是一个将与另一个表连接的数据。在这里,在tblEmployees中,tblEmployees中的Gender和Status是tblCommons中的“Key”数据。所以,我在xml中设置“id”是“Key”

在Commons课程中

namespace Demo
{
    public class Commons
    {
        public Commons(){}
        public virtual string Key { get; set; }
        public virtual string Code { get; set; }
        public virtual string Value { get; set; }
        public virtual Iesi.Collections.Generic.ISet<Employees> Genders { get; set; }
        public virtual Iesi.Collections.Generic.ISet<Employees> Status { get; set; }
    }
}

注意:我没有在xml文件和Commons Class中的tblCommons中设置“ID”,因为我的项目中没有使用“ID”数据

这是我用来获取员工数据的代码

ICriteria criteria = session.CreateCriteria(typeof(Employees));
criteria.CreateCriteria("Gender", "Gender", NHibernate.SqlCommand.JoinType.InnerJoin);
criteria.Add(Restrictions.Eq("Gender.Code", "1"));
criteria.CreateCriteria("Status", "Status", NHibernate.SqlCommand.JoinType.InnerJoin);
criteria.Add(Restrictions.Eq("Status.Code", "2"));
IList<Employees> list = new List<Employees>();
list = criteria.List<Employees>();

我有一个问题:

在“list”中,Employees对象中的“Gender”具有“Status”的数据 如果我改变如下:首先创建标准“状态”然后,创建标准“性别”,“状态”具有“性别”数据。

我不知道为什么。我刚刚使用NHibernate大约1个月。请帮助我为什么我有这个问题。非常感谢。

1 个答案:

答案 0 :(得分:0)

您的问题是您使用Key列作为Commons对象中的ID属性。 ID属性应该是唯一的,并且与任何“有意义”的数据分离。 您应该将ID属性添加到您的Commons类,并将Key转换为常规属性。 更改映射,注意“设置”映射 -

    <class name="Demo.Commons, Demo" table="tblCommons">
       <id name="ID" column="ID" type="String">
          <generator class="identity"/>
       </id>
       <property name="Key" column="Key" type="String" />
       <property name="Code" column="Code" type="String" />
       <property name="Value" column="Value" type="String" />
       <set name="Genders" table="tblEmployees" generic="true" inverse="true">
         <key column="Key" property-ref="Gender" />
         <one-to-many class="Demo.Employees, Demo"/>
       </set>
       <set name="Status" table="tblEmployees" generic="true" inverse="true">
          <key column="Key" property-ref="Status" />
          <one-to-many class="Demo.Employees, Demo"/>
       </set>
  </class>



  <class name="Demo.Employees, Demo" table="tblEmployees">
      <id name="EmployeeID" column="EmployeeID" type="String">
      </id>
      <many-to-one class="Demo.Commons, Demo" name="Gender" column="Gender" property-ref="Key"/>
      <many-to-one class="Demo.Commons, Demo" name="Status" column="Status" property-ref="Key"/>
  </class>
相关问题