-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Why different columns with same value refer to same instance
PostPosted: Fri May 29, 2009 5:26 am 
Newbie

Joined: Fri May 29, 2009 5:03 am
Posts: 2
Hi,

I am confused when using hibernate many-to-one mapping.

I have a table with two columns refer to a same table. The relations are like the following:
<class name="Position" table="Position">
...
<many-to-one name="currentDepartment" column="CurrentDepartment" class="Department" />
<many-to-one name="previousDepartment" column="PreviousDepartment" class="Department" />
</class>

<class name="Department" table="Department">
<id name="departmentId" type="java.lang.String">
<column name="DepartmentID" />
<generator class="assigned" />
</id>
....
</class>

When previousDepartment and currentDepartment has the same departmentId, I get the same instance
of Department. So when I try to update currentDepartment, the previousDepartment is also updated.
How can I set the hibernate config to avoid this situation which means I want to update data separately.


Thanks!


Top
 Profile  
 
 Post subject: Re: Why different columns with same value refer to same instance
PostPosted: Sun Aug 16, 2009 4:30 am 
Newbie

Joined: Fri May 29, 2009 5:03 am
Posts: 2
Anyone can help?


Top
 Profile  
 
 Post subject: Re: Why different columns with same value refer to same instance
PostPosted: Sun Aug 16, 2009 9:22 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
departmentId, I get the same instance
of Department.


Doesn't that mean they are the same department, from the same row in the database?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: Why different columns with same value refer to same instance
PostPosted: Tue Sep 01, 2009 10:39 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
That's object-oriented programming for you:
if currentDepartment and previousDepartment are (one and) the same, you can't change properties of the former without affecting the latter (they are the same).
So if you want to set a new currentDepartment, you have to create a new Department (via new Department()) or retrieve an existing Department from the DB and set currentDepartment to that new instance.


Top
 Profile  
 
 Post subject: Re: Why different columns with same value refer to same instance
PostPosted: Tue Sep 01, 2009 12:33 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
You could even load, evict, set the id to zero, and save again. :)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

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.