2012-12-03 2 views
0

두 클래스가 있습니다. 1 등급 :NHibernate : 복합 키, 일대일 관계 및 "LoadByUniqueKey를 수행하는 중 오류"

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 서버의 데이터베이스에 외래 키가 없으므로 데이터가 일관성이 없다고 덧붙일 수 있습니다.

답변

1

귀하의 평등 방법에 결함이 있습니다. 그것을 얻는 다른 개체가 훨씬 쉽게 구현 ProjNrPindex 변화를해야 Einsatz이 아닌 경우는 false를 반환합니다 : 특정 상황이 체크되지 않은 사용에

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