hibernate delete orphan

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

Ads

Tutorials   
How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code
Ads

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=?

Advertisements

Share on Google+Share on Google+

hibernate delete orphan

Posted on: April 9, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: hibernate delete orphan  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads