Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Keeping object state coherent when using many-to-one
PostPosted: Thu Jun 28, 2012 6:31 am 
Newbie

Joined: Thu Jul 07, 2011 2:38 am
Posts: 3
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.