Hi,
Our datamodel requires us to write very strange hibernate mappings. Everything works so far. However, I'm curious about what the expected behaviour would be for the following mapping:
Code:
<hibernate-mapping>
<class name="my.Cell" table="CELL">
<id name="id" column="PK_CELL_ID" type="long"> ... </id>
<many-to-one name="col" column="FK_COL_ID" class="my.Col" casecade="save-update" />
<many-to-one name="row" column="FK_ROW_ID" class="my.Row" casecade="save-update" />
...
</class>
<class name="my.Col" table="COL">
<id name="id" column="PK_COL_ID" type="long"> ... </id>
<bag name="rows" table="CELL" inverse="true" cascade="save-update">
<key column="FK_COL_ID" />
<many-to-many column="FK_ROW_ID" class="my.Row" unique="true" />
</bag>
...
</class>
<class name="my.Row" table="ROW">
<id name="id" column="PK_ROW_ID" type="long"> ... </id>
<map name="cells" inverse="true" cascade="all-delete-orphan">
<key column="FK_ROW_ID" />
<map-key-many-to-many column="FK_COL_ID" class="my.Col" />
<one-to-many class="my.Cell" />
</map>
...
</class>
</hibernat-mapping>
A
Cell knows of its
Col and
Row -- easy. It manages the ternary
Col-
Cell-
Row relation-ship.
A
Col has a bag of
Rows, which is defined via the
Col-
Cell-
Row relation-ship in the
CELL table. I don't want hibernate to execute any insert/update/delete for this bag, so it is mapped with
inverse="true" (read-only).
Now the tricky part:
A
Row has a map of
Col to
Cell via the same
Col-
Cell-
Row relation-ship in the
CELL table, which is why this map is also mapped with
inverse="true" (read-only). This map is the only collection that can hold
Cells, i.e. the life of a
Cell is bound to this collection, which is why it is mapped with
cascade="all-delete-orphan". In our application, it is OK to simply delete
Rows (Hibernate is supposed to cascade-delete all referenced
Cells).
Now, the question is, what will Hibernate do on a
delete-orphan of a map-entry:
- Will it only delete the entry-value (i.e. the Cell) or
- will it also delete the entry-key (i.e. the Col).
Deleting the
Col is an error, which will be punished with a ReferentialIntegrityException -- there still might be other
Cells of different
Rows that reference a
Col.
In general: What is the expected behaviour of delete-orphan on a <map-key-many-to-many /> ?PS: This is only my curiosity and not a real problem. I could easly refactor the
map-of-Col-to-Cell to a
bag-of-Cell and extract the
Col from each
Cell, but it's nice to have a map that is managed by Hibernate.