customer = // get customer from the current hibernate session
// customer has a discount with database id of 1
一切都很好,直到這裏。但如果我打電話:Hibernate不可變值對象
discount = SpecialDiscount.create("10%");
customer.setDiscountTo(discount);
session.save(customer);
// customer has a discount with database id of 2 now
hibernate如何更新id爲2的同一折扣行,即使我已將其設置爲另一個折扣值對象?此外,我想通過分離前一個左右來防止「具有相同標識的對象已經存在」錯誤。你有什麼建議?
// An entity
class Customer {
// one-to-one mapped immutable value object
SpecialDiscount discount;
SpecialDiscount discount() {
return SpecialDiscount.create(this.discount);
}
void setDiscountTo(SpecialDiscount discount) {
this.discount = SpecialDiscount.create(discount);
}
}
? – Mark 2009-10-09 16:45:57