-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Double foreign key
PostPosted: Tue Jan 17, 2006 1:50 pm 
Newbie

Joined: Tue Jan 17, 2006 1:43 pm
Posts: 4
Location: Brussels, Belgium
Hello all,

has anyone ever faced the problem "HibernateSystemException: a different object with the same identifier value was already associated with the session"?
We have a double relation Claim - Event and Item - Event, and also a one-to-many relationship between Claim and Item. So in this case, an event belongs to a claim while the same event may also belong to an item.
Hibernate 2 does not seem to be able to handle this situation, or are we missing something?
Is there any workaround available?

Thank your for your help
Thibaut


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 5:10 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Hibernate handles it. You just have to be fully au fait with the ideas of transient, detached and persistant objects. The problem you're encountering is that there is a detached object and a separate persistant object in memory representing the same DB row. You need to attach the detached object so that hibernate knows to overwrite the persistant object with it.

Session.lock() and Session.replicate() both allow this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 5:30 am 
Newbie

Joined: Tue Jan 17, 2006 1:43 pm
Posts: 4
Location: Brussels, Belgium
Hi and thanks for your reply,

I think I did not explain our problem correctly.
The Event, Claim and Item objects are all persistent. Both Claim and Item objects have a set of Events (one-to-many relationship with cascade=all), Claim has also a set of Items (one-to-many relationship with cascade=save-update).
So, the problem seems to come from the fact that an Event has (or may have) a double relationship with a Claim and an Item. At the db level, a row in the event table has 2 foreign keys, one pointing to the Claim table and the other to the Item table.
When we try to save a Claim, Hibernate tries to save the whole set of objects related to the Claim (set of items, set of events) but also the set of events related to each item. Hibernate raises an exception while trying to save an event that has the 2 relationships.
We use CBKID as business key, all the equals methods are based on that field. Do we need anything else to tell Hibernate that an event belonging to a claim may belong to an item too.
Any suggestions are welcome :)

Our mappings:
Code:
<class name="Claim" table="CBK_CLAIM">
   <id name="id" column="ID" type="java.lang.Long" length="7">
      <generator class="native"></generator>
   </id>
   <property name="cbkId" type="java.lang.Long" update="true" insert="true" column="CBKID" length="12" not-null="true" unique="true"/>
   ...
   <set name="events" lazy="true" cascade="all" sort="unsorted">
      <key column="CBK_CLAIMID" foreign-key="CBK_EVENT_CBK_CLAIMID_FK"></key>
      <one-to-many class="Event"/>
   </set>
   <set name="items" lazy="true" cascade="save-update" sort="unsorted">
      <key column="CBK_CLAIMID" foreign-key="CBK_ITEM_CBK_CLAIMID_FK"></key>
      <one-to-many class="Item"/>
   </set>
   ...
</class>


<class name="Item" table="CBK_ITEM">
   <id name="id" column="ID" type="java.lang.Long" length="10">
      <generator class="native"></generator>
   </id>
        <property name="cbkId" type="java.lang.Long" update="true" insert="true" column="CBKID" length="12" not-null="true" unique="true"/>
   ...
   <set name="events" lazy="true" cascade="all" sort="unsorted">
      <key column="CBK_ITEMID" foreign-key="CBK_EVENT_CBK_ITEMID_FK"></key>
      <one-to-many class="Event"/>
   </set>
   ...
</class>

<class name="Event" table="CBK_EVENT">
   <id name="id" column="ID" type="java.lang.Long" length="7">
      <generator class="native"></generator>
   </id>
   ...
   <property name="cbkId" type="java.lang.Long" update="true" insert="true" column="CBKID" length="12" not-null="true" unique="true"/>
   ...
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 18, 2006 4:37 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Quote:
Do we need anything else to tell Hibernate that an event belonging to a claim may belong to an item too.


I see the problem. You do need to tell hibernate about this. The default <set> (and list, map, etc.) behaviour is to treat the nested items as fully dependent on the encapsulating object. To stop that behaviour, you set inverse="true" on the set. If you have the same item in two inverse="false" (or default) sets, then both sets will believe that they have full control over the object.

You'll have to either

- Manage events on your own, and have both sets be inverse="true", or

- put the event into the "master" set (probably the one in Claim?), save Claim (so that the Event is persisted, gets an id, etc.), then add it to the inverse="true" set in Item and save the Item.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.