Suppose I've got the following object
Code:
class User {
String name;
int locationId;
Location location;
// getters, setters etc.
}
and the following mapping for it:
Code:
<class name="User" table="tab_user">
<property column="NAME" name="name" type="string"/>
<property column="LOCATION_ID" name="locationId" type="int"/>
<many-to-one column="location_id" insert="false" lazy="false" name="location" not-found="ignore" not-null="true" update="false"/>
</class>
<class name="Location" table="tab_location">
<id column="LOCATION_ID" name="locationId" type="int"/>
...some other properties ...
</class>
Now, if in my Java code I run:
Code:
user.setLocationId(123);
dao.save(user);
I'm left with a user object that has an obsolete location object. If I forget about it I might be getting information about the old location.
What are the good practices on how to keep such object state coherent and up-to-date?
Of course I could reread the user from the DB or set the location object manually, but I'm wondering if someone has any better ideas...
Thanks,
Piotr