我有一個BaseEntity抽象id和版本屬性。這個類還實現了基於PK(id)屬性的hashcode和equals。休眠等於和代理
BaseEntity{
Long id;
Long version;
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BaseEntity other = (BaseEntity) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
現在二人實體A和B擴展BaseEntity如下
A extends BaseEntity{
`B b`
B getB(){return b;)
void setB(B b){this.b=b;}
}
B extends BaseEntity{
}
object b1;
object a1;
a1.set(b1);
session.save(a1) //cascade save;
關閉會話 負載懶惰B和嘗試a1.getB()。等於(B1),一個提供虛假 ,但如果我與a1.getB()。getId()。equals(b1.getId())比較然後給出真正的奇怪! 我認爲這是因爲java協助代理對象,無論如何解決這個問題?
謝謝,明白了。同意你對可能由於基於ID的equals()和hashcode()而引起的問題。我認爲現在我會選擇使用instanceof選項,因爲在這個階段在應用程序中引入自然編號會很困難。 –