-->
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.  [ 3 posts ] 
Author Message
 Post subject: Comparing Proxied objects with .equals
PostPosted: Mon Aug 09, 2004 5:02 pm 
Beginner
Beginner

Joined: Thu May 27, 2004 3:07 pm
Posts: 20
I have defined an interface and implementation class. The interface has
getters and setters for two properties: id, name

The implementation class has the implementation, and overrides the
equals, and hashCode method.

I am trying to implement a routine which will take an arbitrary loaded object, and use a completely seperate session to load a second instance
of the object (I don't show that code here, because it's irrelevant to the
discussion). Once I have both copies, I want to do a comparison using
either the .equals method, or a Comparator implementation. The problem
is this:

If using the equals method:
The object where the .equals method is called, ends up being the real
implementation class
However, the object which is passed to the .equals method ends up
being the Hibernate proxy object, and thus, the .equals method cannot
properly compare the two objects, since they are not of the same class.

If using the comparator:
I'll try to cast to the implementation class (or possibly the interface) and
compare the two objects that way. However, again they are Hibernate
proxy instances.

What I need is a way to compare two proxied instances that will actually
compare the two implementation classes.

Does anyone have any ideas, other than chaning the .equals method to check for the interface instead of the implementation class?


Hibernate version:
2.1

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="tests.MyObjectImpl"
proxy="tests.MyObject"
table="MY_OBJECTS">

<id name="id" column="ID" type="integer" unsaved-value="0">
<generator class="hilo"/>
</id>

<property name="name" column="NAME" not-null="true"/>
</class>

</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
MyObject o1 = (MyObject)s1.load(MyObjectImpl.class,
new Integer(65537));
MyObject o2 = (MyObject)s2.load(MyObjectImpl.class,
new Integer(65537));

System.out.println(o1);
System.out.println(o2);

boolean equals = o1.equals(o2);

Full stack trace of any exception that occurs:

Name and version of the database you are using:
SQLServer 2000

Debug level Hibernate log excerpt:
N/A



Class Definitions:

---------------------------
package tests;


public interface MyObject
{
int getId();
void setId(int id);
String getName();
void setName(String name);
}


---------------------------
package tests;


class MyObjectImpl implements MyObject
{
private int id;
private String name;

public MyObjectImpl()
{
}

public int getId()
{
return id;
}

public void setId(int id)
{
this.id = id;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public boolean equals(Object o)
{
if (this == o) return true;
if (!(o instanceof MyObjectImpl)) return false;

final MyObjectImpl myObject = (MyObjectImpl) o;

if (id != myObject.id) return false;
if (!name.equals(myObject.name)) return false;

return true;
}

public int hashCode()
{
int result;
result = id;
result = 29 * result + name.hashCode();
return result;
}

public String toString()
{
return "MyObjectImpl{" +
"id=" + id +
", name='" + name + "'" +
"}";
}


}


Top
 Profile  
 
 Post subject: Re: Comparing Proxied objects with .equals
PostPosted: Wed Aug 18, 2004 9:45 am 
Regular
Regular

Joined: Wed Aug 18, 2004 5:16 am
Posts: 69
Location: Modena, Italy
mattinger wrote:
Does anyone have any ideas, other than chaning the .equals method to check for the interface instead of the implementation class?


I think the only solution is to use the accessor methods (recommended by documentation) for the properties instead of direct acces to fields or to not using proxy


Top
 Profile  
 
 Post subject: An alternate solution
PostPosted: Wed Aug 18, 2004 9:48 am 
Beginner
Beginner

Joined: Thu May 27, 2004 3:07 pm
Posts: 20
Using the following, i've tried to force the object initialization:

if (dbObj instanceof HibernateProxy)
{
HibernateProxy proxy = (HibernateProxy) dbObj;
LazyInitializer initializer = HibernateProxyHelper.getLazyInitializer(proxy);
dbObj = initializer.getImplementation();
}

then, i compare the original object with the implementation object.
That may not however, take care of the comparison of subobjects
though.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.