-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: What's the expected behaviour of delete-orphan on a map-key
PostPosted: Wed Dec 02, 2009 7:11 am 
Newbie

Joined: Tue Aug 05, 2008 5:07 am
Posts: 7
Location: Dresden, Germany
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.