NHibernate: Kunci komposit, relasi satu-ke-satu dan Kesalahan saat melakukan LoadByUniqueKey

Saya punya dua kelas. Kelas 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;
        }
    }
}

Kelas 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;
        }
    }
}

Pemetaan adalah:

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

Dan

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

Kode:

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

memberi saya:

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

Saya khawatir saya melakukan kesalahan besar, tetapi saya tidak tahu apa. Saya dapat menambahkan bahwa database di sql server tidak memiliki kunci asing sehingga datanya tidak cukup koheren.


person cdbeelala89    schedule 03.12.2012    source sumber


Jawaban (1)


metode equal Anda salah. itu akan mengembalikan false ketika objek lain yang didapatnya bukan Einsatz yang seharusnya ProjNrPindex mengubahnya ke implementasi yang lebih mudah:

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

Gethashcode juga akan memunculkan pengecualian Overflow dalam situasi tertentu, gunakan tidak dicentang

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