I have two classes. Class 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;
}
}
}
Class 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;
}
}
}
Mappings are:
<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>
and
<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>
The code:
var q = s.CreateQuery("from Einsatz e").List<Einsatz>();
gives me:
Error performing LoadByUniqueKey[SQL: SQL not available]
"The given key was not present in the dictionary."
I am afraid I am doing something horribly wrong but I don't know what. I might add that the database in sql server has no foreign keys so the data is not quite coherent.
your equals method is flawed. it will return false when the other object it gets is not Einsatz
which should be ProjNrPindex
change it to the much easier implementation:
public override bool Equals(object obj)
{
var other = obj as ProjNrPindex;
return other != null &&
Pindex == other.Pindex &&
ProjNr == other.ProjNr;
}
Also Gethashcode will throw Overflowexception in certain situations use unchecked
public override Int32 GetHashCode()
{
unchecked
{
return Pindex.GetOrDefault().GetHashCode() +
1000 * ((this.ProjNr != null) ? this.ProjNr.GetHashCode() : 0);
}
}