私が豆を持っているとしましょう:
class File {
private id;
private String name;
private User author;
private List<User> users;
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false
}
if (!(obj instanceof File)) {
return false;
}
File other = (File) obj;
if (getId() == null) {
if (other.getId() != null) {
return false;
}
} else if (!getId().equals(other.getId())) {
return false;
}
if (getName() == null) {
if (other.getName() != null) {
return false;
}
} else if (!getName().equals(other.getName())) {
return false;
}
if (getAuthor() == null) {
if (other.getAuthor() != null) {
return false;
}
} else if (!getAuthor().equals(other.getAuthor())) {
return false;
}
if (getUsers() == null) {
if (other.getUsers() != null) {
return false;
}
} else if (!getUsers().equals(other.getUsers())) {
return false;
}
return true;
}
...
getters/setters
}
この Bean は、MyBatis またはその他の永続的なフレームワークを使用して、データベースとの間でマッピングされます。重要なのは、ユーザーが遅延ロードされることです ( getUsers() が初めて呼び出されたときに DB からロードされます)。
私の質問は、どの equals メソッドがこの Bean を持つべきかということです。
id フィールド (データベースの主キー) を含める必要がありますか?
ユーザーリストを含める必要がありますか? Equals は Java オブジェクトで頻繁に呼び出されるため (たとえば、コレクションに格納されている場合)、これにより遅延アプローチが無効になります。