-->
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.  [ 6 posts ] 
Author Message
 Post subject: new hbm2java generated equals() method is completely broken
PostPosted: Wed Apr 21, 2004 3:38 pm 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
I just asked the new hbm2java (extensions 2.1) to generate equals:

Code:
<meta attribute="implement-equals">true</meta>

via a non-id property as recommended in the Hibernate pages: see "Seperating object id and business key" on http://hibernate.org/109.html

Code:
   <property name="name" type="java.lang.String" column="name" length="256">
      <meta attribute="field-description">Names are unique and so provide an alternate way to dsrcId for referring to a DataSlice.</meta>
      <meta attribute="use-in-equals">true</meta>
      <meta attribute="use-in-tostring">true</meta>
   </property>

Looking at the generated code I see:

Code:
    public boolean equals(Object other) {
        if ( (other == other ) ) return true;
        if ( !(other instanceof Persistent_DataSlice) ) return false;
        Persistent_DataSlice castOther = (Persistent_DataSlice) other;
        return new EqualsBuilder()
            .append(this.getName(), castOther.getName())
            .isEquals();
    }

And the very first line of code reads:
Code:
if ( (other == other ) ) return true;
Now my question: when will that not be true?! In other words the generated equals() method says this instance is equal to every other object!

Yeah, I know I can write my own Velocity template and guess I'll have to do so, but this looks like a bug to me.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 4:11 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
I figure it ought to be
Code:
if ( (this == other ) ) return true;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 4:22 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Quote:
Now my question: when will that not be true?!

Believe it or not, in an exceptionally rare case, it could be false. Equality testing is not atomic for object references, so if the reference to "other" were changed in another thread at exactly the right time, the test would fail. Granted, it couldn't happen in that particular code snippet (the "other" reference is passed by value to the method, and no threads affecting the reference are spawned before the test in the method, so it's safe) but reference equality tests can fail, even when testing a reference against itself, because reference equality is not thread safe.

Jeez, I'm bored. :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 6:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
please submit a jira for it if it really generates other==other

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 21, 2004 7:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
There allready is one at http://opensource.atlassian.com/projects/hibernate/browse/HB-900


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 3:48 am 
Regular
Regular

Joined: Tue Jan 13, 2004 4:57 am
Posts: 83
michael wrote:


Will there be another release soon or do we go with the patch?


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