我正在使用旧数据库并进行以下映射:
PersonBase:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Example.Person" assembly="Example">
<class name="PersonBase" table="WN" lazy="false">
<id name="Id" column="`id`" type="int" >
<generator class="identity"/>
</id>
<property name="Name" column="`NAAM`" type="string" />
<property name="FirstName" column="`VOORNAAM`" type="string" />
<many-to-one name="Section" class="CodeDescription" lazy="false" not-found="ignore" fetch="join" outer-join="true">
<formula>'96'</formula>
<column name="SEKTIE" />
<formula>'1'</formula>
</many-to-one>
<many-to-one name="Country" class="CodeDescription" lazy="false" not-found="ignore" fetch="join" outer-join="true">
<formula>'25'</formula>
<column name="Per_countryCD" />
<formula>'1'</formula>
</many-to-one>
</class>
</hibernate-mapping>
CodeDescription:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Example.Person"
assembly="Example">
<class name="CodeDescription" table="Code" lazy="false">
<composite-id>
<key-property name="Kind" column="`CD_SRT`" type="string"/>
<key-property name="Code" column="`CD`" type="string" />
<key-property name="Language" column="`CD_TAAL`" type="string" />
</composite-id>
<property name="Description" column="`OMSCHR`" type="string" />
<property name="DescriptionShort" column="`OMSCHR_KORT`" type="string" />
<property name="ExternalCode" column="`ExternalCode`" type="string" />
<property name="ExternalCode2" column="`ExternalCode2`" type="string" />
</class>
</hibernate-mapping>
但是当我试图让我的人员为CodeDescriptions找到每行产生2个额外的查询时。
生成的查询:
SELECT
this_."NAAM" as y0_,
this_."id" as y1_,
this_."VOORNAAM" as y2_,
'96' as y3_,
this_.SEKTIE as y4_,
@p0 as y5_,
'25' as y6_,
this_.Per_countryCD as y7_,
@p0 as y8_
FROM
WN this_
ORDER BY
y0_ asc limit @p2;
@p0 = 1 [Type: Int32 (0)],
@p2 = 20 [Type: Int32 (0)]
NHibernate.SQL: 2012-10-08 16:07:24,013 [39] DEBUG NHibernate.SQL [(null)] -
SELECT
codedescri0_."CD_SRT" as CD1_33_0_,
codedescri0_."CD" as CD2_33_0_,
codedescri0_."CD_TAAL" as CD3_33_0_,
codedescri0_."OMSCHR" as OMSCHR5_33_0_,
codedescri0_."OMSCHR_KORT" as OMSCHR4_33_0_,
codedescri0_."ExternalCode" as External6_33_0_,
codedescri0_."ExternalCode2" as External7_33_0_
FROM
Code codedescri0_
WHERE
codedescri0_."CD_SRT"=@p0
and codedescri0_."CD"=@p1
and codedescri0_."CD_TAAL"=@p2;
@p0 = '96' [Type: String (0)], @p1 = '115' [Type: String (0)], @p2 = '1' [Type: String (0)]
NHibernate.SQL: 2012-10-08 16:07:24,015 [39] DEBUG NHibernate.SQL [(null)] -
SELECT
codedescri0_."CD_SRT" as CD1_33_0_,
codedescri0_."CD" as CD2_33_0_,
codedescri0_."CD_TAAL" as CD3_33_0_,
codedescri0_."OMSCHR" as OMSCHR5_33_0_,
codedescri0_."OMSCHR_KORT" as OMSCHR4_33_0_,
codedescri0_."ExternalCode" as External6_33_0_,
codedescri0_."ExternalCode2" as External7_33_0_
FROM
Code codedescri0_
WHERE
codedescri0_."CD_SRT"=@p0
and codedescri0_."CD"=@p1
and codedescri0_."CD_TAAL"=@p2;
@p0 = '96' [Type: String (0)], @p1 = '115' [Type: String (0)], @p2 = '1' [Type: String (0)]
NHibernate.SQL: 2012-10-08 16:07:24,016 [39] DEBUG NHibernate.SQL [(null)] -
SELECT
codedescri0_."CD_SRT" as CD1_33_0_,
codedescri0_."CD" as CD2_33_0_,
codedescri0_."CD_TAAL" as CD3_33_0_,
codedescri0_."OMSCHR" as OMSCHR5_33_0_,
codedescri0_."OMSCHR_KORT" as OMSCHR4_33_0_,
codedescri0_."ExternalCode" as External6_33_0_,
codedescri0_."ExternalCode2" as External7_33_0_
FROM
Code codedescri0_
WHERE
codedescri0_."CD_SRT"=@p0
and codedescri0_."CD"=@p1
and codedescri0_."CD_TAAL"=@p2;
@p0 = '96' [Type: String (0)], @p1 = '115' [Type: String (0)], @p2 = '1' [Type: String (0)]
And so on .... (for every row)
这是一个巨大的性能影响,因为如果我完全映射我的人,它将有20个CodeDescriptions。
编辑 - 我的复合ID类:
public class CodeDescription
{
public string Code { get; set; }
public string Description { get; set; }
public string DescriptionShort { get; set; }
public string ExternalCode { get; set; }
public string ExternalCode2 { get; set; }
public CodeDescriptionKind Kind { get; set; }
public string Language { get; set; }
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
return Description;
}
public CodeDescription()
{
}
}
答案 0 :(得分:3)
你可以仔细检查你的euqals和gethashcode是否在带有compositeid的类上正确实现了吗?它通常是这种奇怪事物的来源。 Equals
和GetHashcode
应模仿复合主键等式