Sunday, May 17, 2015

How to delete the Child records while updating Parent Record.

If we use javax.persistence.CascadeType.All for deleting child records it will delete only collection of strings. Other than collection of strings in order to delete we need to use
org.hibernate.annotations.CascadeType.DELETE_ORPHAN to delete the child records.
For Example,
public class Employee {    
            @OneToMany(cascade = {CascadeType.ALL})
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
List<Department> departments;
}

Related Posts:

  • Difference between Constructor and Method Constructor Method Constructor is a special method of a class but can’t be invoked directly by method call. Methods are member of a class.            It is not a member of a cla… Read More
  • Finding the Nth heighest Salary in SQLProgram: Write a SQL query to get the Nth highest salary from Employee table. Here N can be any number.Solution:SELECT e.* FROM Employee e;Step1: If you want to find the height salary, initially you need to sort the empl… Read More
  • Serialization in Java Serialization: Serialization is a process of writing the object state into a file system with extension .ser and recreatign object state from that file(this is called deserialization). Generally objects are stored in th… Read More
  • Sorting and Searching the Employee Object by using CollectionIn this post, we can learn how to sort the Employee object by using age and search the Employee object by using age.Employee.java---------------------------------------- package com.ranga;import java.io.Serializable;/** … Read More
  • Difference between Abstraction and EncapsulationIn Java both Abstraction and Encapsulation are two important OOP (Object Oriented Programming) concepts or principles. Both are completely different from each other. Abstraction(Hiding): Abstraction means hiding.Abstra… Read More

0 comments: