I have an application that uses a Hierarchy of objects in a parent-child structure. I am trying to improve the performance of a cascaded delete which is currently very slow. I think I can do this if I set inverse="true" on the one-to-many side of the relationship, but when I do the relationship breaks and my parentId and idx fields are always set to null in mysql.
This is how the relationship is set up:
Code:
<class name="MyObject" table="MyTable">
<id name="id" column="id" type="java.lang.Long">
<generator class="increment"/>
</id>
<property name="parentId" />
<property name="name" />
<many-to-one name="parent"
column="parentId" insert="false" update="false"/>
<list name="col" inverse="true" cascade="all-delete-orphan">
<key column="parentId"/>
<index column="colIdx"/>
<one-to-many class="MyObject"/>
</list>
</class>
And I am adding objects like this:
Code:
MyObject parent = new MyObject();
parent.setName("Parent");
transaction = session.beginTransaction();
session.save(parent);
transaction.commit();
...
MyObject child = new MyObject();
child.setName("Child");
transaction = session.beginTransaction();
parent.addChild(child);
transaction.commit();
The instructions I am following indicate that I should do this:
Code:
<key column="parentId" not-null="true"/>
However I can't because my root object does not have a parent.
I've been trying different things all day and I'm stuck. It works fine without inverse="true".