In Java, hashCode()
and equals()
must be consistent :
Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().
See the javadoc for Object#equals(Object) and this StackOverflow question for more discussison and reasoning.
So, in your example you need to implement hashCode() on Customer using both name and id (my IDE generated this code).
public class Customer {
[...]
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}