I have a parent object that has a lazily initialized collection of objects mapped with the <any> mapping. The cascade rule for this collection is set to "all-delete-orphan". When the parent object is deleted, an ObjectDeletedException occurs. The parent object also has a collection of many-to-one objects that is configured the same way. These objects are deleted successfully when the parent object is deleted.
In the likelyhood that I'm doing something stupid, I thought I'd post this on the forum. I do have a whittled down test case that I could submit to JIRA if desired.
Here are the mappings:
Foo (the parent object) ...
Code:
<hibernate-mapping>
<class name="test.Foo" table="FOO">
<id name="oid" column="OID" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="text" column="TEXT" type="string"/>
<bag name="oneToManyItems" inverse="true" cascade="all-delete-orphan" lazy="true" access="field">
<key column="FOO_OID"/>
<one-to-many class="test.OneToManyItem"/>
</bag>
<bag name="anyItems" inverse="true" cascade="all-delete-orphan" lazy="true" access="field"
where="ANCHOR_CLASS = 'test.Foo'">
<key column="ANCHOR_OID"/>
<one-to-many class="test.AnyItem"/>
</bag>
</class>
</hibernate-mapping>
OneToManyItem ...
Code:
<hibernate-mapping>
<class name="test.OneToManyItem" table="ONE_TO_MANY_ITEM">
<id name="oid" column="OID" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<many-to-one name="foo" column="FOO_OID" class="test.Foo"/>
<property name="text" column="TEXT" type="string"/>
</class>
</hibernate-mapping>
AnyItem ...
Code:
<hibernate-mapping>
<class name="test.AnyItem" table="ANY_ITEM">
<id name="oid" column="OID" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<any name="anchor" id-type="long" meta-type="class">
<column name="ANCHOR_CLASS"/>
<column name="ANCHOR_OID"/>
</any>
<property name="text" column="TEXT" type="string"/>
</class>
</hibernate-mapping>
The test case code that generates the exception
Code:
System.out.println("testFooDeleteWithAnyItems()");
Session session = sf.openSession();
// create a Foo object and add an AnyItem to it
Foo foo = new Foo();
foo.setText("Testing Delete of Foo that has AnyItems");
AnyItem anyItem = new AnyItem();
anyItem.setAnchor(foo);
anyItem.setText("any item text");
foo.getAnyItems().add(anyItem);
session.save(foo);
session.flush();
Long fooOid = foo.getOid();
session.connection().commit();
session.close();
// Load the Foo object and delete it
session = sf.openSession();
System.out.println("Loading Foo");
foo = (Foo) session.load(test.Foo.class, fooOid);
System.out.println("Deleting Foo");
session.delete(foo);
System.out.println("Flushing Session");
session.flush();
session.connection().commit();
session.close();