Hibernate 3.0.5:
Below is a mapping for an entity Element Usages:
<hibernate-mapping package="gov.nysed.vrc.domain" schema="EAD">
<class name="ElementUsage" table="ELEMENT_USAGES" lazy="true">
......... . . . . . . ................ . . .. ............
<property name="modifiedBy" length="40" column="MODIFIED_BY"/>
<many-to-one name="elementTemplate" class="ElementTemplate" column="ELTMPL_ID" not-null="true"></many-to-one> <many-to-one name="parentElementUsage" class="ElementUsage" column="ELUSG_ID" ></many-to-one>
<set name="elements" table="ELEMENTS" inverse="true" cascade="all-delete-orphan"> <key column="ELUSG_ID"/> <one-to-many class="Element"/> </set> <set name="childElementUsages" table="ELEMENT_USAGES" inverse="true" cascade="all-delete-orphan"> <key column="ELUSG_ID"/> <one-to-many class="ElementUsage"/> </set>
</class>
</hibernate-mapping>
We have a table ELEMENT_USAGES which has a recursive Parent/Child relationship. Basically it is a Tree... As you can see from the mapping there are 2 objects (parentElementUsage and childElementUsages) which represent the parent/child relationship.
Issue: Below is the code I tried to use to delete a child record -
ElementUsage elementUsage = (ElementUsage) dc.getNodeObject(); AbstractVRCDAO.attach(elementUsage, false); // after creating childElementUsage from page
elementUsage.removeChildElementUsages(childElementUsage); addMessage("data.record.deleted", new String[]{childElementUsage.getElementTemplate().getTagName()}, request);
Here, the 'attach' method is nothing but a call to Session.lock(). 'removeChildElementUsages' removes the object from the Set. It does return 'true' but still it does not remove from the db. I am using the session-per-request pattern (using Filter), so the session is available for the entire request.
I had to force the delete operation by calling delete. On the other hand, using the similar code I was able to delete the 'ELEMENTS' set (declared in the mapping file) without calling the delete method. Both collections have the same Cascade settings, why doesn't the 'childElementUsages' get deleted but the 'elements' gets deleted. Any help would be appreciated.
|