-->
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.  [ 7 posts ] 
Author Message
 Post subject: Problem with cascading delete and NonUniqueObjectException
PostPosted: Thu Jan 22, 2004 9:30 am 
Newbie

Joined: Wed Jan 14, 2004 11:29 am
Posts: 19
I am having a bit of trouble with cascading deletes.

Here is an example bit of code:
Code:
public class MasterItem {
   String getMasterId();
   SubItem getSubItem();
   List getSharedItems();
   void addSharedItem(SharedItem item);
   void removeSharedItem(SharedItem item);
}

public class SubItem {
   String getSubId();
   List getSharedItems();
   void addSharedItem(SharedItem item);
   void removeSharedItem(SharedItem item);
}

public class SharedItem {
   String getSharedId();
   String getName();
}


Let's say this generates a database that looks like this:

Code:
Table:MasterItem
MasterId|Name
123|Master1

Table:SubItem
SubId|Name
234|Sub1

Table:SharedItem
SharedId|Name
345|Shared1
346|Shared2

Table:MasterShareds
MasterId|SharedId
123|345
123|346

Table:SubShareds
SubId|SharedId
234|345


Basically, I want the MasterItem to own the SharedItem's, and when you create a SubItem on the MasterItem, you can assign any number of SharedItem's onto the SubItem. The SubItem just links to the SharedItem's, it does not take a copy. My code lets me easily delete a SubItem and it correctly deletes the link from the SubItem to the SharedItem's.

However, if I want to delete entire Master object, I would want it to cascade down and delete all SubItem's and SharedItem's. What actually happens is this:

Code:
org.springframework.orm.hibernate.HibernateSystemException: a different object with the same identifier value was already associated with the session: 094d401bfa3dc18100fa3dc189610004, of class: my.model.SharedItem; nested exception is:
   net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 094d401bfa3dc18100fa3dc189610004, of class: my.model.SharedItem
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 094d401bfa3dc18100fa3dc189610004, of class: my.model.SharedItem
   at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1081)


What I suspect is happening is that it tries to load up the SharedItem from both the MasterItem and the SubItem, thus confusing it. What are the correct mappings for the behaviour I want, if it is even possible.

Many thanks in advance!


Top
 Profile  
 
 Post subject: Update
PostPosted: Thu Jan 22, 2004 9:58 am 
Newbie

Joined: Wed Jan 14, 2004 11:29 am
Posts: 19
I've changed the example above a bit, what I want to achieve is still the same, I've just got my example more accurate.

Code:
public class MasterItem {
   String getMasterId();
   List getSubItem();
   List getSharedItems();
   void addSharedItem(SharedItem item);
   void removeSharedItem(SharedItem item);
}

public class SubItem {
   String getSubId();
   List getSharedItems();
   void addSharedItem(SharedItem item);
   void removeSharedItem(SharedItem item);
}

public class SharedItem {
   String getSharedId();
   String getName();
}


Code:
Table:MasterItem
MasterId|Name
123|Master1

Table:SubItem
SubId|MasterId|Name
234|123|Sub1
235|123|Sub2

Table:SharedItem
SharedId|MasterId|Name
345|123|Shared1
346|123|Shared2

Table:SubShareds
SubId|SharedId
234|345
235|345
235|346



And what I think the mapping would approximately be:

Code:
         
           <class name="my.model.MasterItem" table="MasterItem">
      <id name="id" unsaved-value="null">
         <generator class="uuid.hex" />
      </id>
      <set
         name="sharedItems"
         lazy="false"
         inverse="false"
         cascade="all"
         sort="unsorted"
      >
         <key column="masterId" />
         <one-to-many class="my.model.SharedItem" />
          </set>
          <set
         name="subItems"
         lazy="true"
         inverse="false"
         cascade="all"
         sort="unsorted"
      >
           <key column="masterId" />
           <one-to-many class="my.model.SubItem"/>

       </set>
          
   </class>
      
      
   <class name="my.model.SubItem" table="SubItem">
      <id name="id" unsaved-value="null">
         <generator class="uuid.hex" />
      </id>
        <set
         name="sharedItems"
         table="SubShareds"
         lazy="false"
         inverse="false"
         cascade="none"
         sort="unsorted"
      >
           <key column="subId" />
           <many-to-many class="my.model.SharedItem" column="sharedId" />
      </set>
   </class>
   
      
   <class name="my.model.SharedItem" table="SharedItem">
      <id name="id" unsaved-value="null">
         <generator class="uuid.hex" />
      </id>
           <property name="name" type="java.lang.String" column="name" not-null="false" unique="false" />
   </class>



Hope this clarifies things a bit.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 10:33 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/117.html#A20

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 10:58 am 
Newbie

Joined: Wed Jan 14, 2004 11:29 am
Posts: 19
So, I can't do a full cascading delete like this?

Do I have to specifically delete the SubItem's first so that the SharedItem's don't get loaded by both the MasterItem and the SubItem into the same session?

What would your solution be?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 11:16 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
No object reference several times in tha graph isn't the problem : Hibernate use the same instance.
You probably load an object and saveOrUpdate the same object with a transiant instance

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 11:22 am 
Newbie

Joined: Wed Jan 14, 2004 11:29 am
Posts: 19
All I do is this basically.
Get the whole MasterItem object
Don't touch any other methods or do anything to it
Delete the MasterItem object

As you say, having the same object references multiple times doesn't seem to be a problem, I can do:
masterObject.getSharedItems();
and
((SubItem) masterObject.getSubItems().iterate().next()).getSharedItems()

And get back lists that both contain the same object. However, doing the big cascading delete is when it fails.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 1:27 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
viper wrote:
All I do is this basically.

Can you show it exactly

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.