-->
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.  [ 2 posts ] 
Author Message
 Post subject: many to one twice referring to the same parent table
PostPosted: Tue Apr 10, 2012 3:17 am 
Newbie

Joined: Tue Apr 10, 2012 2:57 am
Posts: 1
Hi guyz,
Please let me know if you can help me for following problem:

Description:
We have defined hibernate mapping as follows,
"Base Table1" having unique key on column called as "code".
"Base Table2" have two foreign key based on "code" of "Base Table1".

we have defined many to one mapping in hbm file for "Base Table2" as follows:
<many-to-one name="alias1" entity-name="BaseEntity1" property-ref="UK_BaseEntity1" unique="false" fetch="join" update="true" insert="true" outer-join="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="Code1" not-null="true"/>
</many-to-one>
<many-to-one name="alias2" entity-name="BaseEntity1" property-ref="UK_BaseEntity1" unique="false" fetch="join" update="true" insert="true" outer-join="true" optimistic-lock="true" not-found="exception" embed-xml="true">
<column name="Code2" not-null="true"/>
</many-to-one>


Problem Faced:
1. Add record in "Base Table2" with Code1 = A1 & Code A2.
2. Fetch above record, then update above record with Code1= A3 & Code= A4.
In database, record is updated only with Code1=A4 & Code2=A4.


Observation:
We have observed that, when we fetch record from hibernate (session.get() method),
as Code1 & Code2 has same values, it gives same object reference for alias1 & alias2.
Hence, when we update values for alias1 and alias2, same value (i.e. A4) is updated in database.

Questions:
1. In above case, is there any configuration available in hibernate, by which even if foreign key values is same,
we will get different objects corresponding to that.
2. Any suggestion to handle the above case.


Thanks,
Suraj


Top
 Profile  
 
 Post subject: Re: many to one twice referring to the same parent table
PostPosted: Wed Apr 11, 2012 7:18 am 
Newbie

Joined: Wed Apr 11, 2012 7:01 am
Posts: 1
I've got the same problem. We have a class that has 2 user fields which are populated using a dropdown (which has a list of users from the users file) in our web app. Everything's fine until the user sets both of the fields to the same user. From that point on when hibernate loads the class, the same object reference is used for both users, which means a change to one of the users also changes the other user on an update.

I'm assuming it requires a change to the way Hibernate caches objects it's retrieved already from the db.

Hopefully someone knows how to change this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.