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 

Joined: Thu Jul 07, 2011 2:38 am
Posts: 3
Suppose I've got the following object

class User {
  String name;
  int locationId;
  Location location;
  // getters, setters etc.

and the following mapping for it:
<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 name="Location" table="tab_location">
        <id column="LOCATION_ID" name="locationId" type="int"/>
        ...some other properties ...

Now, if in my Java code I run:

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...


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.