I'm trying to figure out this mapping, and somehow it's just eluding me. It's irritating, because it seems like it should be a common case.
I have a basic Parent and Child class with a join table between them. The Child class is dependent on the Parent: if an Child is removed from the Parent, the Child ought to be deleted as well. The Hibernate docs say that this works by specifying cascade="all,delete-orphan".
But the schema won't allow it. If I use a < one-to-many >, I can't specify a column and the join won't work. If I use a < many-to-many unique="true" > per the Hibernate docs, I can't specify a cascade.
Here's what I have now, taken from the Hibernate docs:
<class name="Parent" table="parent_table">
<id name="id" column="id">
<ge开发者_如何学JAVAnerator class="assigned"/>
</id>
<property name="name" type="string"/>
<list table="my_join_table" name="children">
<key column="parent_id"/>
<list-index column="idx">
<!-- how do I put a cascade on this!? -->
<many-to-many column="child_id"
class="Child"
unique="true"/>
</list>
</class>
<class name="Child" table="child_table">
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="name" type="string"/>
<join table="my_join_table" inverse="true" optional="false">
<key column="child_id"/>
<many-to-one name="parent" column="parent_id" not-null="true"/>
</join>
</class>
Ok, I figured it out myself, finally.
The cascade attribute needs to go on the list element rather than the many-to-many:
<list table="my_join_table" name="children" cascade="all,delete-orphan">
<key column="parent_id"/>
<list-index column="idx">
<!-- how do I put a cascade on this!? -->
<many-to-many column="child_id"
class="Child"
unique="true"/>
</list>
精彩评论