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!