NHibernate: คีย์ผสม ความสัมพันธ์แบบหนึ่งต่อหนึ่ง และข้อผิดพลาดในการดำเนินการ LoadByUniqueKey

ฉันมีสองชั้นเรียน คลาส 1:

public class Einsatz
{
    public virtual ProjNrPindex Id { get; set; }

    public virtual Int32? Knr { get; set; }
    public virtual String RessNr { get; set; }
    public virtual String AdrNr { get; set; }
    public virtual DateTime EndeIst { get; set; }
    public virtual DateTime StartIst { get; set; }
    public virtual DateTime ArbeitsbeginnIst { get; set; }
    public virtual String Kennwort { get; set; }

    public virtual Taetigkeit Taetigkeit { get; set; }

    public Einsatz()
    {
        this.Id = new ProjNrPindex();
    }

    public class ProjNrPindex
    {
        public virtual Int32? Pindex { get; set; }
        public virtual String ProjNr { get; set; }

        public override Boolean Equals(Object obj)
        {
            if (obj as Einsatz == null)
           {
               return(false);
           }

           if (Object.ReferenceEquals(this, obj) == true)
           {
               return(true);
           }

           ProjNrPindex other = obj as ProjNrPindex;

           if (Object.Equals(this.Pindex, other.Pindex) == false)
           {
               return(false);
           }          

           if (Object.Equals(this.ProjNr, other.ProjNr) == false)
           {
               return(false);
           }

           return(true);
       }

       public override Int32 GetHashCode()
       {
           Int32 hash = 0;

           hash += (this.Pindex != null) ? this.Pindex.GetHashCode() : 0;
           hash += 1000 * ((this.ProjNr != null) ? this.ProjNr.GetHashCode() : 0);

           return(hash);
       }
    }

    public override bool Equals(object obj)
    {
        var other = obj as Einsatz;

        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;

        return this.Id.Equals(other.Id);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int hash = GetType().GetHashCode();
            hash = (hash * 31) ^ this.Id.GetHashCode();

            return hash;
        }
    }
}

คลาส 2:

public class Taetigkeit
{
    public virtual ProjNrPindex Id { get; set; }

    public virtual string Text { get; set; }
    public virtual string RessNr { get; set; }

    public virtual Einsatz Einsatz { get; set; }

    public Taetigkeit()
    {
        this.Id = new ProjNrPindex();
    }

    public class ProjNrPindex
    {
        public virtual Int32? Pindex { get; set; }
        public virtual String ProjNr { get; set; }

        public override Boolean Equals(Object obj)
        {
            if (obj as Einsatz == null)
            {
                return (false);
            }

            if (Object.ReferenceEquals(this, obj) == true)
            {
                return (true);
            }

            ProjNrPindex other = obj as ProjNrPindex;

            if (Object.Equals(this.Pindex, other.Pindex) == false)
            {
                return (false);
            }

            if (Object.Equals(this.ProjNr, other.ProjNr) == false)
            {
                return (false);
            }

            return (true);
        }

        public override Int32 GetHashCode()
        {
            Int32 hash = 0;

            hash += (this.Pindex != null) ? this.Pindex.GetHashCode() : 0;
            hash += 1000 * ((this.ProjNr != null) ? this.ProjNr.GetHashCode() : 0);

            return (hash);
        }
    }

    public override bool Equals(object obj)
    {
        var other = obj as Einsatz;

        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;

        return this.Id.Equals(other.Id);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int hash = GetType().GetHashCode();
            hash = (hash * 31) ^ this.Id.GetHashCode();

            return hash;
        }
    }
}

การแมปคือ:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="RestService"
               namespace="RestService">

<class name="Einsatz" table="PLANUNG">
    <composite-id name="Id">
      <key-property name="ProjNr">
        <column name="ProjNr"  />
      </key-property>
      <key-property name="Pindex">
        <column name="Pindex" />
      </key-property>


  </composite-id>


    <property name="Knr" column="Knr" />
    <property name="AdrNr" column="AdrNr" />
    <property name="RessNr" column="RessNr" />
    <property name="EndeIst" column="EndeIst" />
    <property name="StartIst" column="StartIst" />
    <property name="ArbeitsbeginnIst" column="ArbeitsbeginnIst" />
    <property name="Kennwort" column="Kennwort" />

    <one-to-one name="Taetigkeit" class="Taetigkeit" property-ref="Id"/>

</class>

</hibernate-mapping>

และ

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="RestService"
               namespace="RestService">

<class name="Taetigkeit" table="PLANBER">
    <composite-id name="Id">
        <key-property name="ProjNr">
            <column name="ProjNr"  />
        </key-property>
      <key-property name="Pindex">
            <column name="Pindex" />
        </key-property>
    </composite-id>

    <property name="RessNr" column="RessNr" />
    <property name="Text" column="Text" />

  <one-to-one name="Einsatz" class="Einsatz" property-ref="Id"/>

</class>

</hibernate-mapping>

รหัส:

var q = s.CreateQuery("from Einsatz e").List<Einsatz>();

ให้ฉัน:

Error performing LoadByUniqueKey[SQL: SQL not available]
"The given key was not present in the dictionary."

ฉันกลัวว่าฉันกำลังทำอะไรผิดร้ายแรง แต่ฉันไม่รู้ว่าอะไร ฉันอาจเสริมว่าฐานข้อมูลในเซิร์ฟเวอร์ sql ไม่มีคีย์ต่างประเทศดังนั้นข้อมูลจึงไม่สอดคล้องกัน


person cdbeelala89    schedule 03.12.2012    source แหล่งที่มา


คำตอบ (1)


วิธีการเท่าเทียมกันของคุณมีข้อบกพร่อง มันจะคืนค่าเท็จเมื่อวัตถุอื่นที่ได้รับไม่ใช่ Einsatz ซึ่งควรเป็น ProjNrPindex เปลี่ยนเป็นการใช้งานที่ง่ายกว่ามาก:

public override bool Equals(object obj)
{
    var other = obj as ProjNrPindex;
    return other != null &&
        Pindex == other.Pindex &&
        ProjNr == other.ProjNr;
}

นอกจากนี้ Gethashcode จะส่ง OverflowExceptionion ในบางสถานการณ์โดยไม่ได้เลือกการใช้งาน

public override Int32 GetHashCode()
{
    unchecked
    {
        return Pindex.GetOrDefault().GetHashCode() +
            1000 * ((this.ProjNr != null) ? this.ProjNr.GetHashCode() : 0);
    }
}
person Firo    schedule 11.12.2012