-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Proxy fails self equality law
PostPosted: Tue Aug 07, 2007 11:35 pm 
Beginner
Beginner

Joined: Wed Dec 28, 2005 3:14 pm
Posts: 29
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();
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 08, 2007 3:43 am 
Newbie

Joined: Mon Jun 18, 2007 3:48 am
Posts: 7
Location: FRANCE
When you use a proxy, you should access properties by claaing public accessors.

Try this :

public override bool Equals(object obj)
{
//return this.name.Equals(((Cat)obj).name);
return this.Name.Equals(((Cat)obj).Name);
}

public override int GetHashCode()
{
//return name.GetHashCode();
return this.Name.GetHashCode();
}


wish it helps you.

Laurent MONDEIL


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.