How does Hibernate recognize a change in a object? Is it inspecting the field directly or using the associated Getter-method?
Say i have a price column in the database. Under certain conditions the price shall be converted. Can i do this in the Getter-method and return the converted value without changing the value of this column if i persist this object? A change made by the Setter of this property should change the column though.
What is the default behaviour? Can it be changed? Is this a good practice or should it be avoided by using a “getConvertedPrice”-method, because the behaviour may change with a different configuration/version which yields to hard-to-find bugs?
Edit: Request Example code …
@Entity
@Table(name="items")
public class Item {
/*...*/
@Column(name="item_price")
private double price;
@Column(name="item_currency")
// More enum mapping ...
private Currency currency;
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
if(getCurrency()!=Currency.US_DOLLAR) {
return price * CurrencyService.getCurrentRate(getCurrency());
} else {
return price;
}
}
/*..*/
}
It depends on access type of an entity. By default it’s deduced from placement of mapping annotations (on fields or on properties).
You can override access type for specific field using
@Accessannotation (or@AccessTypefor old versions of Hibernate).