-->
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: schemaExportTask & ON DELETE CASACDE/SET NULL
PostPosted: Sun Jan 18, 2004 11:26 am 
Newbie

Joined: Sun Jan 18, 2004 9:52 am
Posts: 11
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


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.