Home » JPA Cascade Persist

JPA Cascade Persist

by Online Tutorials Library

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: –

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: –

  • Create an entity class named as StudentEntity.java under com.tutoraspire.jpa.student package that contains attributes s_id, s_name, s_age and an object of Subject type marked with cascade specification.


  • Create another entity class named as Subject.java under com.tutoraspire.jpa.subject package.


  • Now, map the entity class and other databases configuration in Persistence.xml file.


  • Create a persistence class named as StudentCascade.java under com.tutoraspire.jpa.cascade package to persist the entity object with data.


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.


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.

JPA Cascade Persist

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

JPA Cascade Persist

Next TopicJPA Cascade Remove

You may also like