tags:

views:

228

answers:

1

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.

A: 

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"&gt;
<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.

Pascal Lindelauf