A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Java SE 8 for the Really Impatient
this week in the
Object Relational Mapping
Update bidirectional relation without updating the objects
Joined: Jan 14, 2009
Jul 06, 2009 10:46:10
I have the following problem:
I have two tables
profile and profile history.
These tables have their own back reference tables to an area. (so a profile can have many areas and the history has to keep track of that)
for profile- area:
aac_profile_area_xref: profile_id(FK) and area_id(FK)
aac_profile_his_area_xref: profile_history_id(FK) and area_id(FK).
the area has a version column so hibernate keeps track of every update:
Here is the related part of the hbm files:
<set cascade="all,delete-orphan" name="areas" inverse="false" table="aac_profile_area_xref" lazy="false"> <synchronize table="aac_profile"/> <synchronize table="ac_area"/> <key> <column name="aac_profile_id" not-null="true" /> </key> <many-to-many entity-name="com.x.database.persistence.hb.Area"> <column name="ac_area_id" not-null="true" /> </many-to-many> </set>
for profile history:
<set cascade="save-update" name="areas" inverse="false" table="aac_profile_his_area_xref" lazy="false"> <synchronize table="aac_profile_history"/> <synchronize table="ac_area"/> <key> <column name="aac_profile_history_id" not-null="true" /> </key> <many-to-many entity-name="com.x.database.persistence.hb.Area" > <column name="ac_area_id" not-null="true" /> </many-to-many> </set>
Now here is the sequence of events that are happening during a profile save (assuming an area is already persisted):
create a profile object.
create a profile history object.
save the history (which updates the area!)
save the profile (which tries to update the area but I get StaleObjectStateException !)
(profile and history are saved in two separate transactions)
Now what I want is a way to force hibernate to not update the area when it is saving it. i.e. just put the area_id and profile_id in the back reference table of the history.
Is there anyway to do this?
I agree. Here's the link:
subject: Update bidirectional relation without updating the objects
Hibernate problem - creating duplicate foreign key index
Hibernate- one-to-many association on a foreign key
(N+1) selects � Hibernate bug?
hibernate creating duplicate ForeignKey index
hbm.xml file does an update instead of an insert- Helppppp
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014