-->
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: Proxies and equals()
PostPosted: Sun Sep 21, 2003 10:30 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I see really-really weird behavior of my code after I started using proxies. Yes, I have read FAQ and know I should use accessors instead of direct access to the members. But... I just want to understand HOW THIS CAN BE POSSIBLE!

public class Company
{
...
public void test(Company other)
{
System.err.println("---TEST---");
System.err.println("this==other => " + (this == other));
System.err.println("this=" + this + ", this.id=" + id + ", getId()=" + getId());
System.err.println("other=" + other + ", other.id=" + other.id + ", getId()=" + other.getId());
System.err.println("----------");
}
...
}

Somewhere in the test:

Company a = person.getCompany();

and then

a.test(a);

Company mapping specifies "proxy" attribute. So in the test 'a' is an instace of a proxy. The test above outputs

[junit] ---TEST---
[junit] this==other => false
[junit] this=eg.Company@19ecd80, this.id=383, getId()=383
[junit] other=eg.Company@19ecd80, other.id=0, getId()=383
[junit] ------

I can not understand this at all. Why this.id works while other.id does not.

How is it possible (this==other) check return false?

Why should I use accessors in my equals() implementation? Just because second object may not loaded yet or there are other reasons?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 21, 2003 11:06 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I have added couple of lines to the test():

System.err.println("this=" + this.getClass());
System.err.println("other=" + other.getClass());

log says

[junit] this=class eg.Company
[junit] other=class eg.Company$$EnhancedByCGLIB$$0

I have no idea how

a.test(a)

call may result in different 'this' and 'other'


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 21, 2003 11:28 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Ohh....
It seems I figured things out...

I have class MyClass, Hibernate creates proxy class MyClassProxy for it. The proxy class instace holds reference to the real object of class MyClass. (Or null)

When I call MyClass.method(), it Hibernate will eventually call the same method for the target object passing exactly the same arguments to it. Think must be look like:

class MyClass
{
public void test(MyClass other)
{
}
}

class MyClassProxy extends MyClass
{
MyClass target;
public void test(MyClass other)
{
target.test(other);
}
}

Well, this explains why I get different this and other in the test() method.

The question is: when implementing equals() method for proxy-aware classes my I assume it is safe to access this.* and use accessors only to work with other.* ?


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.