I tried this with one-to-many associations, it may still provide a hint for the map problem:
Suppose a class Shelf that contains a set of Books.
Code:
<class name="Shelf" table="SHELF">
<id name="shelfID" type="int" column="SHELF_ID">
<meta attribute="scope-set">protected</meta>
<generator class="my.IDGenerator"/>
</id>
<!-- Cascaded delete is performed by foreign key integrity constraint
and must be declared for the key column-->
<set name="books"
cascade="save-update"
inverse="true"
>
<key column="SHELF_ID" on-delete="cascade"/>
<one-to-many class="Book"/>
</set>
</class>
If there is a foreign key constraint on the BOOK table
ALTER TABLE BOOK ADD CONSTRAINT FK_BOOK_SHELF FOREIGN KEY (SHELF_ID) REFERENCES SHELF(SHELF_ID) ON DELETE CASCADE;
the database takes care of deleting the books if the referenced shelf is taken out. As I understand it, you tell Hibernate with cascade="save-update" and on-delete="cascade" that it does not need to delete books prior to removing a shelf.
However, I stepped on a similar problem as you when I try to change the set into an ordered list (as my books are usually not placed onto the shelf in an orderly manner, sorting by author or title does not preserve my disorder ;-) ):
Code:
<class name="Shelf" table="SHELF">
<id name="shelfID" type="int" column="SHELF_ID">
<meta attribute="scope-set">protected</meta>
<generator class="my.IDGenerator"/>
</id>
<!-- inverse must be false for indexed collections -->
<list name="books" cascade="all" inverse="false">
<key column="SHELF_ID"/>
<list-index column="BOOK_ORDER"/>
<one-to-many class="Book"/>
</list>
</class>
Inverse needs be true for on-delete="cascade", which is not possible for indexed lists. In the above example above, Hibernate deletes all books when deleting a shelf because of cascade="all". This only works when there is no "ON DELETE CASCADE" foreign key constraint on BOOK.
So the answer for you may be: just leave out the on-delete="cascade" and Hibernate will not generate a foreign key database constraint but still delete the map entries, given appropriate cascade="..." settings.
My Question: is there a way in Hibernate to use indexed lists with an "ON DELETE CASCADE" (on-delete="cascade") foreign key constraint?
Fritz