-
1. Re: Deployer warning equals() and hashCode() must be overrid
davidjencks Sep 9, 2001 1:57 AM (in response to rakhbari)Are the following true?
1. two primary key objects A and B with the same constructor arguments satisfy A.equals(B)
2. if primary key objects A and B satisfy A.equals(B) then A.hashCode() == B.hashCode() -
2. Re: Deployer warning equals() and hashCode() must be overrid
cecchet Jan 9, 2002 6:00 PM (in response to rakhbari)> Are the following true?
>
> 1. two primary key objects A and B with the same
> constructor arguments satisfy A.equals(B)
I have a problem with JBoss about that. My beans are working fine but I get this warning telling me that I should override equals(). If you use the default constructor some instance variables may be null. Usually null should only be equal to null.
So I think JBoss reports this warning even with a correct implementation of the equals() function. Here is an example of a working code where JBoss 2.4.3 reports a 9.2.9 warning:
public class BidPK implements java.io.Serializable {
public Integer id;
public BidPK() {}
public BidPK(Integer uniqueId){
id = uniqueId;
}
public int hashCode() {
if (id == null)
return 0;
else
return id.intValue();
}
public boolean equals(Object other)
{
boolean isEqual = (id == other); // true if id and other are null
if ((other instanceof BidPK) && (id != null)) {
isEqual = (id.intValue() == ((BidPK)other).id.intValue()); // true only if both have same value
}
return isEqual;
}
}
Is there anything incorrect in this implementation of equals ?
Thanks for your help,
Emmanuel -
3. Re: Deployer warning equals() and hashCode() must be overrid
cecchet Jan 9, 2002 6:22 PM (in response to rakhbari)Never mind the previous message. I didn't 'cvs update' and I used the wrong version. The correct code is :
public boolean equals(Object other) {
boolean isEqual = false;
if (other instanceof BidPK) {
if (id == null)
isEqual = (id == ((BuyNowPK)other).id);
else
isEqual = (id.intValue() == ((BuyNowPK)other).id.intValue());
}
return isEqual;
}
This version works fine, don't worry!