In my application all the objects stored in the database have an integer code. I've developed the following functions:
Code:
@Override
public int hashCode() {
//This method has been reimplemented because of Hibernate. See Hibernate tutorials for more info.
final int prime = 31;
int result = 1;
result = prime * result + code;
// This is done to make the same as the equals method
if (code == 0)
result = prime * result + super.hashCode();
else
result = prime * result;
return result;
}
@Override
public boolean equals(Object obj) {
//This method has been reimplemented because of Hibernate. See Hibernate tutorials for more info.
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final BusinessObject other = (BusinessObject) obj;
// the following is done in order to use objects which have not been
// stored yet to the db.
if (code == 0) {
if (other.code != 0)
return false;
super.equals(obj);
}
if (code != other.code)
return false;
return true;
}
With this functions I say that two objects are equal if the code is equal, or, in case there is no code, that they are the same instance.
When I use this code my application does not work properly. Anyone has any ideas?
Thanks