JPA Cascade Persist

The cascade persist is used to specify that if an entity is persisted then all its associated child entities will also be persisted. The following syntax is used to perform cascade persist operation: -

@OneToOne(cascade=CascadeType.PERSIST)  

JPA Cascade Persist 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.PERSIST)  
    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

package com.javahubpoint.jpa.subject;  
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>  
<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

package com.javahubpoint.jpa.cascade;  
import javax.persistence.*; 
import com.javahubpoint.jpa.student.*;  
import com.javahubpoint.jpa.subject.Subject;

public class StudentCascade {  
  
     public static void main( String[ ] args ) {  
             
          EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );  
            
          EntityManager em = emf.createEntityManager( );  
          em.getTransaction().begin();
           
          StudentEntity s1=new StudentEntity();  
          s1.setS_id(101);  
          s1.setS_name("Vipul");  
          s1.setS_age(20);  
            
          StudentEntity s2=new StudentEntity();  
          s2.setS_id(102);  
          s2.setS_name("Aman");  
          s2.setS_age(22);  
            
          Subject sb1=new Subject();  
          sb1.setName("ENGLISH");  
          sb1.setMarks(80);  
          sb1.setS_id(s1.getS_id());  
              
          Subject sb2=new Subject();  
          sb2.setName("Maths");  
          sb2.setMarks(75);  
          sb2.setS_id(s2.getS_id());  
            
          s1.setSub(sb1);  
          s2.setSub(sb2);   
            
          em.persist( s1 );//No need to perform persist operation separately for different entities.  
          em.persist(s2);  
          
          em.getTransaction().commit();  
  
          em.close( );  
          emf.close( );     
    }    
}  

Note - The primary key in student table i.e s_id will treat as a foreign key in subject table to maintain a relationship between both the tables.

Output:

After the execution of the program, the following tables are generated under MySQL workbench.

Student Table - To fetch data, run select * from student in MySQL.





Subject Table - To fetch data, run select * from subject in MySQL.