Forums Register Login

Prevent update on cross-refrence table Hibernate

+Pie Number of slices to send: Send
Hi

I have the following problem:

I have an Activity class that maps to table activity.


I have a Profile class that maps to profile table. Everytime the profile is updated I create a ProfileHistory object and save it in profile_history table.

The history should never be modified.

Now in special cases I want to associate the Activity with ProfileHistory in activity_profile_history_xref table which only saves the id of the activity and profile_history:



Here is the related hbm file in Activity :




Now here is the problem:

profileHistory has an attribute Facility. ProfileHistory objects could be pointing to the same Facility object.

Hence when I retrieve multiple profileHistory objects that point to the same Facility object and associate them to Activity and then save the Activity object, I get

a different object with the same identifier value was already associated with the session [for Facility object]


And the problem is that hibernate is trying to update the profileHistory, while nothing has changed on the profileHistory side ( I can tell from the hibernate sql statements).

Is there a way to force hibernate to just update the Activity object?

So far my only solution was to do something like this:



Could someone suggest a better way?


Thanks in advance.




+Pie Number of slices to send: Send
I used merge() and everything seems ok now. But the question remains...why does hibernate update something that hasn't changed?!
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1819 times.
Similar Threads
Hibernate- one-to-many association on a foreign key
Many-to-many composite ID
Update bidirectional relation without updating the objects
hibernate creating duplicate ForeignKey index
Hibernate - Force foreign key lookup on insert
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:05:07.