I have a Name entity with first, middle, last and full persisted fields. The idea is that I need the full name automatically updated whenever the first, middle or last names are updated. They can never be out of sync. Am I required to use setFull() instead of full = in the calculateFullName() method? Is this the proper way to achieve what I need in hibernate?
Name table
------
Id
First
Middle
Last
Full
Code:
public void setFirst(String first) {
this.first = first;
calculateFullName();
}
public void setMiddle(String middle) {
this.middle = middle;
calculateFullName();
}
public void setLast(String last) {
this.last = last;
calculateFullName();
}
public void calculateFullName() {
full = first;
full = (middle == null ? full : full == null ? middle : " " + middle);
full = (last == null ? full : full == null ? last : " " + last);
}
so the setFull(String full) method should actually never be called, unless the calculateFullName() requires its use instead of full =. It's ok if hibernate sets the full field from the database. It should get the same value whether setFull or setFirst is called in either order.