This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Prevent update on cross-refrence table Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Prevent update on cross-refrence table Hibernate" Watch "Prevent update on cross-refrence table Hibernate" New topic

Prevent update on cross-refrence table Hibernate

zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32

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.

zabet tyan
Ranch Hand

Joined: Jan 14, 2009
Posts: 32
I used merge() and everything seems ok now. But the question remains...why does hibernate update something that hasn't changed?!
I agree. Here's the link:
subject: Prevent update on cross-refrence table Hibernate
It's not a secret anymore!