-->
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: hbm2java equals generation
PostPosted: Tue Jan 10, 2006 12:47 pm 
Newbie

Joined: Tue Oct 04, 2005 1:20 pm
Posts: 16
Location: The Netherlands
Hibernate version: 3.1-beta

Dear list,

When I trigger generation of the equals method in my mappings like this:
Code:
      <id name="id" type="java.lang.Long">
         <meta attribute="use-in-equals">true</meta>
         <generator class="native" />
      </id>

then hbm2java produces the following code:
Code:
   public boolean equals(Object other) {
                 if ( (this == other ) ) return true;
       if ( (other == null ) ) return false;
       if ( !(other instanceof BaseRelatie) ) return false;
       BaseRelatie castOther = ( BaseRelatie ) other;
         
       return ( (this.getId()==castOther.getId()) ||
                           ( this.getId()!=null && castOther.getId()!=null
                             && this.getId().equals(castOther.getId()) ) );
   }

The
Code:
(this.getId()==castOther.getId()) ||
fragment implies that 2 instances are equal when both ids are null. So when I add 2 (not yet persisted) instances to a Collection, this fragment will cause the first instance to be overwritten by the second. Therefore this fragment should be removed IMO.

Kind regards,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 1:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh - then don't use id in equals! (see hibernate.org about equals and hashcode)

_________________
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.  [ 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.