hibernate cascade deletion example

Go To StackoverFlow.com

0

can any one provide me full 2 mapping file which implements cascade delete. measn if A contasis a set of B then when A is deleted B shold automatically be deleted.

2009-06-16 14:52
by user93796
I can only suggest a book NHibernate In Action. They have an example in it - zvolkov 2009-06-16 15:14


0

You only need 1 mapping file: the one of the parent object. E.g. this example from Hibernate in action

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class name="hello.Message" table="MESSAGES">
    <id
        name="id"
        column="MESSAGE_ID">
        <generator class="increment"/>
    </id>
    <property
        name="text"
        column="MESSAGE_TEXT"/>
    <many-to-one
        name="nextMessage"
        cascade="all"
        column="NEXT_MESSAGE_ID"/>
    </class>
</hibernate-mapping>

The "cascade" parameter does the job. If you only want the cascading delete, then you should use cascade="delete". Other options are "all-delete-orphan" and "delete-orphan". Check out the Hibernate documentation for more info.

2009-07-03 12:48
by Pascal Lindelauf
Ads