July 13 2015

Hibernate cascade delete

Hibernate cascade delete

When cascade=”delete” is declared on association, hibernate deletes all the persistent instances of the association (children) whenever the delete operation is performed on object. If you don’t not use cascade delete, you have to delete all the child objects one by one explicitly before deleting the parent object.

Hibernate cascade delete example

In the below example, ’CascadeType.DELETE’ is declared for the association. Whenever delete operation is performed on Student object, all the associated StudentMarks records will be deleted along with Student record.

select * from student;

STUDENT_ID STUDENT_NAME FATHER_NAME DATE_OF_BIRTH COURSE
105 PAUL GEORGE 5/22/2015 MS

select * from student_marks;

STUDENT_MARKS_ID STUDENT_ID COURSE OBTAINED_MARKS TOTAL_MARKS
2 105 C 90 100
3 105 JAVA 80 100

Student.java

package com.answersz.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name = "student")
public class Student {

@Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "STUDENT_ID")
 private Integer studentId;

@Column(name = "STUDENT_NAME")
 private String studentName;

@Column(name = "FATHER_NAME")
 private String fatherName;

@Column(name = "DATE_OF_BIRTH")
 private Date dateofBirth;

@Column(name = "COURSE")
 private String course;

@OneToMany(mappedBy = "student")
 @Cascade(CascadeType.DELETE)
 private List<StudentMarks> studentMarks;

// getters and setters

}

StudentMarks.java

package com.answersz.dao;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "student_marks")
public class StudentMarks {

@Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "STUDENT_MARKS_ID")
 private Integer studentMarksId;

@Column(name = "COURSE")
 private String course;

@Column(name = "OBTAINED_MARKS")
 private Integer obtainedMarks;

@Column(name = "TOTAL_MARKS")
 private Integer totalmarks;

@ManyToOne(fetch = FetchType.LAZY)
 @JoinColumn(name = "STUDENT_ID", nullable = false)
 private Student student;

// getters and setters

}

HibernateCascadeDelete.java

package com.answersz.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateCascadeDelete {

 public static void main(String[] args) {

 SessionFactory factory = new AnnotationConfiguration().configure()
 .buildSessionFactory();

 Session session = factory.openSession();

 Transaction t = session.beginTransaction();

 Student student = (Student) session.get(Student.class, 105);

 session.delete(student);

 t.commit();

 session.close();
 factory.close();

 }
}

Output

Hibernate: select student0_.STUDENT_ID as STUDENT1_0_0_, student0_.COURSE as COURSE0_0_, student0_.DATE_OF_BIRTH as DATE3_0_0_, student0_.FATHER_NAME as FATHER4_0_0_, student0_.STUDENT_NAME as STUDENT5_0_0_ from student student0_ where student0_.STUDENT_ID=?
Hibernate: select studentmar0_.STUDENT_ID as STUDENT5_1_, studentmar0_.STUDENT_MARKS_ID as STUDENT1_1_, studentmar0_.STUDENT_MARKS_ID as STUDENT1_1_0_, studentmar0_.COURSE as COURSE1_0_, studentmar0_.OBTAINED_MARKS as OBTAINED3_1_0_, studentmar0_.STUDENT_ID as STUDENT5_1_0_, studentmar0_.TOTAL_MARKS as TOTAL4_1_0_ from student_marks studentmar0_ where studentmar0_.STUDENT_ID=?
Hibernate: delete from student_marks where STUDENT_MARKS_ID=?
Hibernate: delete from student_marks where STUDENT_MARKS_ID=?
Hibernate: delete from student where STUDENT_ID=?


Copyright © 2014. All rights reserved.

Posted July 13, 2015 by answersz in category Hibernate

Leave a Comment

Your email address will not be published. Required fields are marked *

*