编辑 - 使用我的类的Equals和GetHasCode更新了问题。我在网上发现它是这种情况下的主要错误来源。但我想我确实正确地实现了这些方法。请验证
company (id (pk), Name)
CompanyDivision (CompanyId (pk)(fk), DivisionNbr (pk), Description) - This refrences Company using CompanyId. CompanyId & DivisionNbr forms composite key
CompanyDivisionDepartment (CompanyId (pk)(fk), DivisionNbr (pk)(fk), DepartmentNumber (pk), Description)
This table references CompanyDivision using CompanyId & DivisionNbr. CompanyId & DivisionNbr & DepartmentNumber forms composite key
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>();
这些是运行的查询,在运行该HQL后使用Nhibernate profiler找到它们。尝试了我在网上遇到的所有方式都没有运气。请帮助理解为什么最后2个查询已经运行,即使我通过加入
获取关联select department0_.CompanyId as CompanyId1_0_,
department0_.DivisionNbr as Division2_1_0_,
department0_.DepartmentNbr as Departme3_1_0_,
division1_.CompanyId as CompanyId2_1_,
division1_.DivisionNbr as Division2_2_1_,
department0_.Description as Descript4_1_0_,
division1_.Description as Descript3_2_1_
from CompanyDivisionDepartment department0_
inner join CompanyDivision division1_
on department0_.CompanyId = division1_.CompanyId
and department0_.DivisionNbr = division1_.DivisionNbr
where department0_.DepartmentNbr = 1 /* @p0 */
SELECT division0_.CompanyId as CompanyId2_0_,
division0_.DivisionNbr as Division2_2_0_,
division0_.Description as Descript3_2_0_
FROM CompanyDivision division0_
WHERE division0_.CompanyId = 1 /* @p0 */
and division0_.DivisionNbr = 1 /* @p1 */
SELECT division0_.CompanyId as CompanyId2_0_,
division0_.DivisionNbr as Division2_2_0_,
division0_.Description as Descript3_2_0_
FROM CompanyDivision division0_
WHERE division0_.CompanyId = 2 /* @p0 */
and division0_.DivisionNbr = 1 /* @p1 */
映射
公司
<?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;
}
}
答案 0 :(得分:0)
随机尝试改变这个:
<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>