hibernate delete orphan

In this section, you will learn to delete mapped record using cascade delete-orphan in Hibernate.

hibernate delete orphan

hibernate delete orphan

In this section, you will learn to delete mapped record using cascade delete-orphan in Hibernate.

First of all you need to understand delete cascade operation :

delete cascade / cascade delete

In delete cascade, if record of one table is deleted, its all related or referenced record will be deleted from another table as well.

For example , lots of employee can be living in the same building or apartment. Means they have the same address(except flat number / house number). The one to many relation, here, is many employee living in the same apartment. The related mapping figure is given below :

The Address.hbm.xml is given below :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="net.roseindia">
<class name="Address" table="address">
<id name="addressId" column="address_id">
<generator class="native" />
</id>
<property name="street" column="street"/>
<property name="city" column="city"/>
<property name="state" column="state"/>
<property name="country" column="country"/>
<set name="employees" cascade="delete" table="employee" >
<key>
<column name="address_id" not-null="true" />
</key>
<one-to-many class="net.roseindia.Employee" />
</set>
</class>
</hibernate-mapping>

In cascade delete, if record of one table is deleted, its all related or referenced record will be deleted from another table as well. In the above example, if a row from the address table is deleted, its related records in the employee table will be deleted automatically as follows :

Query q = session.createQuery("from Address where addressId= :addressId ");
q.setParameter("addressId", "3000");
Address address= (Address)q.list().get(0);
session.delete(address);

Now, you are well aware of cascade delete, you can easily understand Cascade delete-orphan .

Cascade delete-orphan

What if , you want to delete only two referenced record from employee table. This can be accomplish using Cascade delete-orphan. The Address.hbm.xml  for this example is given below :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="net.roseindia">
<class name="Address" table="address">
	<id name="addressId" column="address_id">
		<generator class="native" />
	</id>
	<property name="street" column="street"/>
	<property name="city" column="city"/>
	<property name="state" column="state"/>
	<property name="country" column="country"/>
	<set name="employees" cascade="delete-orphan" table="employee" >
		<key>
			<column name="address_id" not-null="true" />
		</key>
		<one-to-many class="net.roseindia.Employee" />
	</set>
</class>
</hibernate-mapping>

For enabling delete-orphan cascade  effect, we declare cascade="delete-orphan" in <set> tag . It will remove the selected rows of employee when you saveOrUpdate the Address as follows :

Employee e1 = (Employee)session.get(Employee.class, new Integer(21));
Employee e2 = (Employee)session.get(Employee.class, new Integer(30));

Address address= (Address)session.get(Address.class, new Integer(3));
address.getEmployees().remove(e1);
address.getEmployees().remove(e2);

session.saveOrUpdate(address);

After execution of the above code, you will get the following record in console :


Hibernate: delete from anky.employee where employee_id=?           

Hibernate: delete from anky.employee where employee_id=?