Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Prevent update on cross-refrence table Hibernate

 
zabet tyan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
zabet tyan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used merge() and everything seems ok now. But the question remains...why does hibernate update something that hasn't changed?!
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!