Ask the question "does the relating object allways exists in the database prior to saving this object?". If the answer is yes then you can use the relating objects id to implement equals and hashCode. If the answer is no, then you will have to use the relating objects equals and hashCode method instead.
yes:
Code:
public int hashCode() {
int result = 14;
result = 29 * result + getBidAmount().hashCode();
result = 29 * result + getItem().getId().hashCode();
return result;
}
no:Code:
public int hashCode() {
int result = 14;
result = 29 * result + getBidAmount().hashCode();
result = 29 * result + getItem().hashCode();
return result;
}
You can allways use the relating objects equals and hashCode method instead of the id but the result will be that the relating object (the item for the bid object) will have to be loaded into the session to use equals and hashCode. If the relating object equals and hashCode relies on a relating object, the result can be that a large number of relating objects will have to be loaded to calculate a single equals or hashCode. This will of cause effect performance.
Peter