0

私は2つのクラスを持っています。クラス 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サーバーのデータベースには外部キーがないため、データが完全に一貫していないことを付け加えるかもしれません。

4

1 に答える 1

1

equals メソッドに欠陥があります。取得した他のオブジェクトが、より簡単な実装に変更するEinsatz必要がない場合は、false を返します。ProjNrPindex

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

また、Gethashcode は特定の状況で Overflowexception をスローします。

public override Int32 GetHashCode()
{
    unchecked
    {
        return Pindex.GetOrDefault().GetHashCode() +
            1000 * ((this.ProjNr != null) ? this.ProjNr.GetHashCode() : 0);
    }
}
于 2012-12-11T11:30:55.983 に答える