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
|