July 13 2015

Hibernate cascade delete orphan

Hibernate cascade delete orphan

Deletes the persistent instance that has been removed (from collection) or dereferenced (object).The orphan delete allows us to delete the child entity by removing or dereferencing it from parent instead of deleting the child entity’s directly.

Hibernate cascade delete orphan Example

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_ORPHAN)
 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

}

If you want to delete a Student’s StudentMarks record you can remove it from the Student’s studentMarks list instead of deleting the StudentMarks directly. The StudentMarks record will be deleted when save or update operation is performed on Student object.Following code removes StudentMarks record whose course is ‘JAVA’

CascadeDeleteOrphan.java


package com.answersz.dao;

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

public class CascadeDeleteOrphan {

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);

StudentMarks studMarks = null;
 for (StudentMarks studentMarks : student.getStudentMarks()) {
 if (studentMarks.getCourse().equals("JAVA")) {
 studMarks = studentMarks;
 break;
 }
 }

System.out.println("StudentMarksId : " + studMarks.getStudentMarksId());
 student.getStudentMarks().remove(studMarks);

session.save(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=?
StudentMarksId : 3
Hibernate: delete from student_marks where STUDENT_MARKS_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 *

*