2

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

tbl员工:

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 没有 tblCommons 的任何 PK。

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 中的“关键”数据。所以,我在xml中设置“id”是“Key”

在公共课上

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 类的 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>();

我这里有个问题:

在“列表”中,如果我这样更改,Employees 对象中的“性别”具有“状态”数据:首先是 CreateCriteria“状态”,然后是 CreateCriteria“性别”,“状态”具有“性别”数据。

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

4

1 回答 1

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>
于 2012-09-03T10:52:31.647 回答