Hi,
I am using the schemaExportTask, and I am having difficulty working out how to get the foreign key constraints to specify ON DELETE CASCADE or ON DELETE SET NULL.
I looked at the following post
http://forum.hibernate.org/viewtopic.php?t=341&highlight=schemaexporttask+delete+cascade - and can see what the issues are from hibernate's perspective - i.e. if the database goes modifying stacks of records willy-nilly, then hibernate would have trouble keeping everything up to date. Perhaps this post is more about best practice to handle a situation like the following -
On the 'many' side -
Code:
<hibernate-mapping package="recipeManager.domain">
<class name="RecipeIngredient" table="recipeIngredient">
<id name="id" column="uid" type="long" unsaved-value="null">
<generator class="hilo">
<param name="table">keys</param>
<param name="column">nextKey</param>
<param name="max_lo">10</param>
</generator>
</id>
<many-to-one name="ingredient" cascade="none" class="Ingredient" column="ingredientId" not-null="true"/>
<many-to-one name="measurement" cascade="all" class="IngredientMeasurement" column="measurementId" not-null="true"/>
</class>
</hibernate-mapping>
On the 'one' side
Code:
<hibernate-mapping package="recipeManager.domain">
<class name="Ingredient" table="ingredient">
<id name="id" column="uid" type="long" unsaved-value="null">
<generator class="hilo">
<param name="table">keys</param>
<param name="column">nextKey</param>
<param name="max_lo">10</param>
</generator>
</id>
<property name="name" column="name" type="string"/>
</class>
</hibernate-mapping>
so basically the Ingredient has no idea what RecipeInredient[s] it is used in. This makes sense to me - I don't want the ingredient maintaining long lists of where it is used. The schemaExportTask generates a foreign key constraint on the recipeIngredient table as expected - but if I was just designing the database schema I would normally add ON DELETE CASCADE or ON DELETE SET NULL to specify what to do if an ingredient is deleted.
I can see that this would be fine for the database, but all the in-memory RecipeIngredient[s] would now be out of sync. unless there was some mechanism by which hibernate could determine where all the references are. Looking at the docs, it seems that hibernate's way of doing this would be for the Ingredient to maintan a collection of all the RecipeIngredient[s] that reference it, and then hibernate can use this to make sure everything remains consistent.
My situation is that a separate app. maintains the ingredients, so there is no chance that RecipeIngredient[s] are in memory when the Ingredient is deleted.
I would like the behaviour that when I delete an Ingredient, all RecipeIngredient[s] that use that Ingredient are also deleted. Is it possible to get schemaExportTask to generate the ON DELETE CASCADE/SET NULL, and leave the memory consistency problems to me?
caveat: Only been using hibernate for a couple of days, so there is every chance this is a dumb question. :) Thanks,
Colin