-->
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.  [ 7 posts ] 
Author Message
 Post subject: equals() needed to use get() methods
PostPosted: Thu Aug 18, 2005 4:23 am 
Beginner
Beginner

Joined: Tue May 10, 2005 4:18 am
Posts: 29
Hello all
I tried looking for an explanations for this , but couldnt find.

Is it true that the only way my equals() method will work is when I compare fields using their "get.. ()" methods rather than the fields themselves ?
So this will NOT work :
Code:
public boolean equals(final Object other) {
      if (!(other instanceof Address))
         return false;
      MyObject castOther = (MyObject) other;
      return this.foo == castOther.foo;
}


and this will work :
Code:
public boolean equals(final Object other) {
      if (!(other instanceof Address))
         return false;
      MyObject castOther = (MyObject) other;
      return this.getFoo() == castOther.getFoo();

??

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 4:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes if your objects a proxies - proxies can only intercept methods not field access (unless you want to use build time byte code enhancement)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 4:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes if your objects a proxies - proxies can only intercept methods not field access (unless you want to use build time byte code enhancement)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: equals() on proxies
PostPosted: Thu Aug 18, 2005 7:38 am 
Beginner
Beginner

Joined: Tue May 10, 2005 4:18 am
Posts: 29
Thank you Max, for your reply.
I ask because I tried using both CodeSugar and Common4E plugins for Eclipse, to help me generate the equals and hashCode methods.

They both compare fields - which caused me some days of frustrated debugging to find the origin for some weird bugs...
(BTW , .getClass() comparison doesn't work as well... Same reason I guess)

Does the Hibernate community recommend on a tool to do this ? I already have my classes coded, so re-generating with hibernate-tools from the mapping files is a bit of a headache for me..

Or maybe me question should be - do you plan to have the hibernate-tools support just equals / hashCode method generation on an already built classes ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 8:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well - you could build it into the eclipse plugins i guess....wanna contribute it ? ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 9:29 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
Depends.

I feel that if you use tools to generate hashcode and equals you need a way of determining which properties to inspect. So annotations would seem the most likely way to go, or maybe inspecting the hbm files and using that.

As for how it would work, I'd expect it to be a simple case of right clicking and choosing "Insert Hashcode/Equals".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 2:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the hibernate eclipse tools knows which properties should be used in equals...

_________________
Max
Don't forget to rate


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