-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem deleting with one-to-many associations.
PostPosted: Wed Jan 11, 2006 2:37 pm 
Beginner
Beginner

Joined: Fri Jun 10, 2005 2:22 pm
Posts: 27
I am getting a data integrity violation exception when trying to delete the top-most object in an object graph. I haven't been able to find any examples that parallel my design, so I'll try to explain it here.

Hibernate version: 3.0.5

Consider four classes: E, ES, ER, and EI. E, ES, and ER all have collections of EI objects. So, while this is something of a parent-child relationship, EI does not contain an id for its parent. Instead, the mapping creates an intermediate (association) table. Here is a sample mapping from the E class for the specific collection:

Mapping documents:
<set name="items"
table="event_event_item"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted">
<key column="event_id" foreign-key="eei_event_fk"/>
<many-to-many class="com.enttek.ems.shared.model.event.EventItem"
column="event_item_id"
outer-join="auto"
foreign-key="eei_event_item_fk"/>
</set>

E also contains a collection of ES, and ES contains a collection of ER. EI also contains a reference to E (it's ultimate parent) as a many-to-one type relationship.

(I tried to draw a simple diagram here, but it wouldn't format well enough to make any sense.)

When I delete an instance of E, I was expecting that the cascading would traverse the object graph and remove objects beginning with the lowest level then proceed to the top. However, the data integrity exception I get indicates that an attemp is made to delete E before all of the EI instances have been removed because the reference in EI to E is throwing the exception.

Is there something special that must be done in the mapping to have hibernate delete the EI instances first using the association tables to find them?

Thanks,
David


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 6:18 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You need to check that you have the correct cascade and inverse mappings. Assuming that EI objects can be managed on their own (i.e. you can create one without a containing E, ES or ER), then you want to set inverse="true" on all the sets. If all EIs exist in an E object, whether or not they exist in any ES or ER object, then you want to remove the cascade="delete" from the ES and ER sets, and rely solely on the E set.


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