The cascade remove is used to specify that if the parent entity is removed then all its related entities will also be removed. The following syntax is used to perform cascade remove operation: -
@OneToOne(cascade=CascadeType.REMOVE)
Cascade Remove Example
In this example, we will create two entity classes that are related to each other but to establish the dependency between them we will perform cascading operation.
This example contains the following steps: -
Step 1. Create an entity class named as StudentEntity.java under com.javahubpoint.jpa.student package that contains attributes s_id, s_name, s_age and an object of Subject type marked with cascade specification.
StudentEntity.java
import javax.persistence.*;
import com.javahubpoint.jpa.subject.Subject;
@Entity
@Table(name="student")
public class StudentEntity {
@Id
private int s_id;
private String s_name;
private int s_age;
@OneToOne(cascade={CascadeType.REMOVE})
private Subject sub;
public Subject getSub() {
return sub;
}
public void setSub(Subject sub) {
this.sub = sub;
}
public StudentEntity(int s_id, String s_name, int s_age , Subject sub) {
super();
this.s_id = s_id;
this.s_name = s_name;
this.s_age = s_age;
this.sub=sub;
}
public StudentEntity() {
super();
}
public int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
public int getS_age() {
return s_age;
}
public void setS_age(int s_age) {
this.s_age = s_age;
}
}
Step 2. Create another entity class named as Subject.java under com.javahubpoint.jpa.subject package.
Subject.java
import javax.persistence.*;
@Entity
@Table(name="subject")
public class Subject {
private String name;
private int marks;
@Id
private int s_id;
public Subject(String name, int marks, int s_id) {
super();
this.name = name;
this.marks = marks;
this.s_id=s_id;
}
public Subject()
{
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
public int getS_id() {
return s_id;
}
public void setS_id(int s_id) {
this.s_id = s_id;
}
}
Step 3. Now, map the entity class and other databases configuration in Persistence.xml file.
Persistence.xml
<persistence-unit name="Student_details">
<class>com.javahubpoint.jpa.student.StudentEntity</class>
<class>com.javahubpoint.jpa.subject.Subject</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/studentdata"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.logging.level" value="SEVERE"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
</properties>
</persistence-unit>
</persistence>
Step 4. Create a persistence class named as StudentCascade.java under com.javahubpoint.jpa.cascade package to persist the entity object with data.
StudentCascade.java
import javax.persistence.*;
import com.javahubpoint.jpa.student.*;
public class StudentCascade {
public static void main( String[ ] args ) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );
EntityManager em = emf.createEntityManager( );
em.getTransaction().begin();
StudentEntity s=em.find(StudentEntity.class, 101);
em.remove(s);
em.getTransaction().commit();
em.close( );
emf.close( );
}
}
Output:
After the execution of the program, the following tables are generated under MySQL workbench.
Student table - The details of student having s_id 101 will be removed from student table. To fetch data, run select * from student query in MySQL.
Subject table - Due to cascade remove, the details related to s_id 101 will also be deleted from subject table. To check the result, run select * from subject query in MySQL