Register / Login
Win a copy of
Re-engineering Legacy Software
this week in the
Docker in Action
Object Relational Mapping
Update bidirectional relation without updating the objects
posted 6 years ago
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?
Hibernate- one-to-many association on a foreign key
hbm.xml file does an update instead of an insert- Helppppp
hibernate creating duplicate ForeignKey index
(N+1) selects � Hibernate bug?
Hibernate problem - creating duplicate foreign key index