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

0 comments: