Hi
I have a Comment class, which has one or more child comments:
<class name="com.fi.cruds.api.comment.Comment" table="Comment" dynamic-update="true" lazy="false">
<id name="id" access="field" type="java.lang.Long" unsaved-value="null">
<generator class="native"/>
</id>
<version access="field" name="version" unsaved-value="null"/>
<many-to-one name="parentComment"
column="parentId"
access="field"
class="com.fi.cruds.api.comment.Comment"
not-null="false"
cascade="none"
lazy="false"/>
<set name="childComments"
inverse="false"
cascade="all-delete-orphan"
access="field"
lazy="false">
<key column="parentId"/>
<one-to-many class="com.fi.cruds.api.comment.Comment"/>
</set>
</class>
As you see I have cascade="all-delete-orphan" on the children, which means that if I delete a comment all children should be removed. Also I would like to have inverse="true" to have the parent own the relationship but I dont have it here because of reasons further down.
Ok so evrything connected to saving works perfect but the delete does not when I have invers="true". I get a database constraint on the parentId, which for me is strange. If idelete a Comment shouldn hibernate remove all children automatically also? Or do I have to get the parent and remove the comment when deleting OR session.delete if it is a root comment.
There are no really good input on having parent/children for same class in same table so I hope someone has a comprehensive examples of this.
Cheers
Magnus
|