1

我正在使用旧数据库并具有以下映射:

个人基础:

<?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>

代码说明:

<?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>

但是当我试图让我的人找到代码描述时,它会为每行生成 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()
        {

        }

    }
4

1 回答 1

3

您可以仔细检查您的 euqals 和 gethashcode 是否在具有复合 ID 的类上正确实现?它通常是这种奇怪事情的来源。Equals并且GetHashcode应该模仿复合主键相等

于 2012-10-12T12:55:56.710 回答