Tutorial :hibernate cascade deletion example



Question:

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.


Solution:1

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »