Nhibernate 使用生成多个查询的复合键获取实体关联上的联接

Nhibernate 使用生成多个查询的复合键获取实体关联上的联接,nhibernate,nhibernate-mapping,Nhibernate,Nhibernate Mapping,编辑-用我的类的Equals和GetHasCode更新问题。我在网上发现这是本案例中错误的主要来源。但我想我确实正确地实现了这些方法。请核实 (由于声誉原因,无法添加图片)。下面是表结构的外观 HQL我过去经常得到部门 var q = @"from Department dept join fetch dept.Division where dept.DepartmentNumber=:id"; IList<Department> depts = session.Creat

编辑-用我的类的Equals和GetHasCode更新问题。我在网上发现这是本案例中错误的主要来源。但我想我确实正确地实现了这些方法。请核实

(由于声誉原因,无法添加图片)。下面是表结构的外观 HQL我过去经常得到部门

 var q = @"from Department dept join fetch dept.Division where dept.DepartmentNumber=:id";
    IList<Department> depts = session.CreateQuery(q).SetInt32("id", 1).List<Department>();
映射

公司

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NH" namespace="NH">
  <class name="Company">
    <id name="Id">
      <generator class="assigned"></generator>
    </id>
    <property name="Name"></property>
    <set name="Divisions" table="CompanyDivision">
      <key column="CompanyId"></key>
      <one-to-many class="Division"/>
    </set>
  </class>
</hibernate-mapping>

分部

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NH" namespace="NH">
  <class name="Division" table="CompanyDivision">
    <composite-id>
      <key-many-to-one name="Company" class="Company" column="CompanyId"></key-many-to-one>
      <key-property name="DivisionNumber" column="DivisionNbr"></key-property>
    </composite-id>
    <property name="Description" column="Description"></property>
    <set name="Departments" table="CompanyDivisionDepartment">
      <key>
        <column name="CompanyId"></column>
        <column name="DivisionNbr"></column>
      </key>
      <one-to-many class="Department"/>
    </set>
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NH" namespace="NH">
  <class name="Department" table="CompanyDivisionDepartment">
    <composite-id>
      <key-many-to-one name="Division" class="Division">
        <column name="CompanyId"></column>
        <column name="DivisionNbr"></column>
      </key-many-to-one>
      <key-property name="DepartmentNumber" column="DepartmentNbr"></key-property>
    </composite-id>
    <property name="Description"></property>
  </class>
</hibernate-mapping>

Equals和GetHashCode映射

public class Division
    {
        public virtual Company Company { get; set; }
        public virtual int DivisionNumber { get; set; }
        public virtual string Description { get; set; }
        public virtual Iesi.Collections.Generic.ISet<Department> Departments { get; set; }

        public override bool Equals(object obj)
        {
            if (object.ReferenceEquals(this, obj)) return true;
            Division that = obj as Division;
            if (that == null) return false;
            if (this.Company.Id != that.Company.Id) return false;
            if(this.DivisionNumber != this.DivisionNumber) return false;
            return true;
        }
        public override int GetHashCode()
        {
            var hash = 13;
            hash = (hash * 7) + this.Company.Id.GetHashCode();
            hash = (hash * 7) + this.DivisionNumber.GetHashCode();
            return hash;
        }
    }



public class Department
    {
        public virtual Division Division { get; set; }
        public virtual int DepartmentNumber { get; set; }
        public virtual string Description { get; set; }

        public override bool Equals(object obj)
        {
            if (Object.ReferenceEquals(this, obj)) return true;

            Department that = obj as Department;
            if (that == null) return false;
            if (this.Division.Company.Id != that.Division.Company.Id) return false;
            if (this.Division.DivisionNumber != that.Division.DivisionNumber) return false;
            if (this.DepartmentNumber != that.DepartmentNumber) return false;

            return true;
        }

        public override int GetHashCode()
        {
            var hash = 13;
            hash = (hash * 7) + this.DepartmentNumber.GetHashCode();
            hash = (hash * 7) + this.Division.Company.Id.GetHashCode();
            hash = (hash * 7) + this.Division.DivisionNumber.GetHashCode();
            return hash;
        }
    }
公共类划分
{
公共虚拟公司公司{get;set;}
公共虚拟整数{get;set;}
公共虚拟字符串描述{get;set;}
公共虚拟Iesi.Collections.Generic.ISet部门{get;set;}
公共覆盖布尔等于(对象对象对象)
{
if(object.ReferenceEquals(this,obj))返回true;
分部=作为分部的obj;
如果(that==null)返回false;
如果(this.Company.Id!=that.Company.Id)返回false;
如果(this.DivisionNumber!=this.DivisionNumber)返回false;
返回true;
}
公共覆盖int GetHashCode()
{
var-hash=13;
hash=(hash*7)+this.Company.Id.GetHashCode();
hash=(hash*7)+this.DivisionNumber.GetHashCode();
返回散列;
}
}
公共课系
{
公共虚拟除法{get;set;}
公共虚拟整数部门号{get;set;}
公共虚拟字符串描述{get;set;}
公共覆盖布尔等于(对象对象对象)
{
if(Object.ReferenceEquals(this,obj))返回true;
部门=作为部门的obj;
如果(that==null)返回false;
如果(this.Division.Company.Id!=that.Division.Company.Id)返回false;
如果(this.Division.DivisionNumber!=that.Division.DivisionNumber)返回false;
如果(this.DepartmentNumber!=that.DepartmentNumber)返回false;
返回true;
}
公共覆盖int GetHashCode()
{
var-hash=13;
hash=(hash*7)+this.DepartmentNumber.GetHashCode();
hash=(hash*7)+this.Division.Company.Id.GetHashCode();
hash=(hash*7)+this.Division.DivisionNumber.GetHashCode();
返回散列;
}
}

随机尝试更改以下内容:

<composite-id>
  <key-many-to-one name="Division" class="Division">
    <column name="CompanyId"></column>
    <column name="DivisionNbr"></column>
  </key-many-to-one>
  <key-property name="DepartmentNumber" column="DepartmentNbr"></key-property>
</composite-id>

为此:

<composite-id>
  <key-many-to-one name="Division" class="Division">
    <column name="CompanyId"></column>
    <column name="DivisionNbr"></column>
  </key-many-to-one>
</composite-id>
<key-property name="DepartmentNumber" column="DepartmentNbr"></key-property>

或者这个:

<composite-id>
  <key-many-to-one name="Division" class="Division">
    <column name="CompanyId"></column>
    <column name="DivisionNbr"></column>
    <column name="DepartmentNumber" column="DepartmentNbr"></column>
  </key-many-to-one>
</composite-id>

感谢您抽出时间仔细研究。映射更改对我不起作用
<composite-id>
  <key-many-to-one name="Division" class="Division">
    <column name="CompanyId"></column>
    <column name="DivisionNbr"></column>
  </key-many-to-one>
</composite-id>
<key-property name="DepartmentNumber" column="DepartmentNbr"></key-property>
<composite-id>
  <key-many-to-one name="Division" class="Division">
    <column name="CompanyId"></column>
    <column name="DivisionNbr"></column>
    <column name="DepartmentNumber" column="DepartmentNbr"></column>
  </key-many-to-one>
</composite-id>