Javatpoint Logo
Javatpoint Logo

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.javatpoint.jpa.student package that contains attributes s_id, s_name, s_age and an object of Subject type marked with cascade specification.

StudentEntity.java

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

Subject.java

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

Persistence.xml

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

StudentCascade.java

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.

JPA Cascade Persist
  • Subject Table - To fetch data, run select * from subject in MySQL.

JPA Cascade Persist
Next TopicJPA Cascade Remove




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA