【发布时间】:2012-03-04 15:49:44
【问题描述】:
我正在使用休眠,需要重写 equals 和 hashCode()。我选择使用 google-guava 的 equals 和 hashCode 助手。
我想知道我是否在这里遗漏了什么。
我有 idImage 和 filePath 的 get/set 方法。
@Entity
@Table(name = "IMAGE")
public class ImageEntity {
private Integer idImage;
private String filePath;
@Override
public int hashCode() {
return Objects.hashCode(getFilePath());
}
@Override
public boolean equals(final Object obj) {
if(obj == this) return true;
if(obj == null) return false;
if(obj instanceof ImageEntity){
final ImageEntity otherImage = (ImageEntity) obj;
return Objects.equal(getFilePath(), otherImage.getFilePath());
}
return false;
}
}
编辑:
遇到继承并有一个样本here
【问题讨论】:
-
看起来不错...您是否遇到任何错误/不需要的行为?
-
@NimChimpsky idImage 是用于在数据库中自动递增主键的 id 映射(使用休眠)。
-
@Marcelo 不,目前还没有。一切都很好。我只是想知道我是否错过了什么。
标签: java equals guava hashcode