Hibernate version:1.2.0 GA
If you override the entity's Equals() and GetHashCode(), in a valid way of course, the proxy's self equality fails (object does not equal to itself) when you load it from another proxy. I think it's a major issue.
Entity:
public class Cat
{
private int id;
private ISet children = new HashedSet();
private Cat mom;
private string name;
public virtual string Name {
get { return name; }
set { name = value; }
}
protected Cat() {}
public Cat(string n) {
name = n;
}
public virtual Cat Mom {
get { return mom; }
set { mom = value; }
}
public virtual int Id
{
get { return id; }
set { id = value; }
}
public virtual ISet Children
{
get { return children; }
set { children = value; }
}
public override bool Equals(object obj)
{
return this.name.Equals(((Cat)obj).name);
}
public override int GetHashCode()
{
return name.GetHashCode();
}
}
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo" assembly="NHSessTest" default-lazy="true">
<class name="NHSessTest.Cat" >
<id name="Id" type="Int32" unsaved-value="0" >
<generator class="identity" />
</id>
<set name="Children" table="ChildrenParents" >
<key column="Mom" />
<one-to-many class="NHSessTest.Cat" />
</set>
<many-to-one name="Mom" class="NHSessTest.Cat" column="Mom" />
<property name="Name" />
</class>
</hibernate-mapping>
Test
[Test]
public void Test() {
Configuration cfg = new Configuration();
cfg.AddAssembly("NHSessTest");
ISessionFactory fct = cfg.BuildSessionFactory();
ISession sess = fct.OpenSession();
//Setup the test data
Cat mom = new Cat("mom");
Cat child = new Cat("child");
child.Mom = mom;
sess.Save(mom);
sess.Save(child);
mom.Children.Add(child);
sess.Flush();
sess.Clear();
child = sess.Get<Cat>(child.Id);
Assert.AreEqual(child.Mom, child.Mom); //test fails here
sess.Delete(mom);
sess.Delete(child);
sess.Flush();
sess.Close();
}
|