-->
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: equals implementation with proxies
PostPosted: Mon Oct 20, 2003 12:27 pm 
I have a root persistent class with a default implementation of the equals(...) method.
It assumes that two persistent objects are equals if:
- they have the same identifier value
- AND share the same class

Code:
public boolean equals(Object obj) {
    Persistent other = (Persistent) obj;
    boolean sameId = this.getId().equals( other.getId() );
    boolean sameClazz = this.getClass().equals( other.getClass() );
    return (sameId && sameClazz);
}


Problem: when working with proxies, the other object to compare with (obj) may sometimes be of a different "physical " class (e.g. a cglib
class) although they share the same "logical" class...
Therefore, equals return false :-(

Any idea/hint/recommendation for having a good implementation which works in all cases ?

Thanks in advance,

Bernard.


Top
  
 
 Post subject:
PostPosted: Mon Oct 20, 2003 5:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
try instanceof instead of getClass().equals

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 4:33 am 
Yes, it works fine !
(indeed CGLIB proxies are subclasses of the "concrete" class)

This gives:


Code:
public boolean equals(Object obj) {
    Entity other = (Entity) obj;
    boolean sameId = this.getId().equals( other.getId() );
    boolean sameClazz = ( this.getClass().isInstance(other) );
    return (sameId && sameClazz);
}



Thanks a lot.


Top
  
 
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.