Cascading Type in Spring Boot

Posted By : Aditya Jaiswal | 31-Oct-2022

What is Cascading?

Entity relationships depend on another entity, Person–Address relationship. If Person will not exist, the Address entity doesn't have any meaning of its own. When we delete the Person entity, our Address entity should also get deleted.

 

Cascade type in JPA :-

 

* All operations are represented by the javax.persistence.CascadeType.

1) ALL

2) MERGE

3) PERSIST

4) REFRESH 

5) DETACH

6) REMOVE

 

Cascade type in Hibernate:-

 

* All operations are represented by the org.hibernate.annotations.CascadeType.

1) SAVE_UPDATE 

2) REPLICATE

3) LOCK
 

Differences in JPA :-

 

1) CascadeType.ALL :-  All the above cascade operations can be applied to the entities related to the parent entity.

Ex :-

2) CascadeType.MERGE :- If the parent entity is merged, all its related entities will also be merged.

Ex :- 

3) CascadeType.PERSIST :- When we will save the person entity, the address entity will also get saved.

Ex :-

 

4) CascadeType.REFRESH :- If the parent entity is refreshed, all its related entities will also be refreshed.

 

Ex :-

 


5) CascadeType.DETACH :- If the parent entity is detached, all its related entities will also be detached.

Ex :-

 

6) CascadeType.Remove :- If the parent entity is removed, all its related entities will also be removed.

Ex :-

 

@Test
public void parentRemovedThenChildRemoved() {
    int studentId;
    Student student = buildPerson("devendera");
    Address address = buildAddress(student);
    student.setAddresses(Arrays.asList(address));
    session.persist(student);
    session.flush();
    studentId = student.getId();
    session.clear();

    Student savedStudentEntity = session.find(Student.class, studentId);
    session.remove(savedStudentEntity);
    session.flush();
}
// Run  test case in following SQL

Hibernate: delete from Address where id=?

Hibernate: delete from Student where id=?

Differences in Hibernate :-

1) CascadeType.SAVE_UPDATE :- It is used to save or update the mapped entity.

Ex :-

@Test

public void parentSavedThenChildSaved()

{

Student student = buildPerson("devendera");

Address address = buildAddress(person);

student.setAddresses(Arrays.asList(address));

session.saveOrUpdate(student); session.flush();

}

// Run  test case in following SQL

Hibernate: insert into Student (name, id) values (?, ?)

Hibernate: insert into Address (

city, houseNumber, student_id, street, zipCode, id) values (?, ?, ?, ?, ?, ?)

2) Cascade.Type.REPLICATE :-  It is used to replicate the entity operation.

Ex :-

@Test

public void parentReplicatedThenChildReplicated() {

Student student = buildPerson("devendera");

student.setId(2);

Address address = buildAddress(student);

address.setId(2); student.setAddresses(Arrays.asList(address));

session.unwrap(Session.class).replicate(student, ReplicationMode.OVERWRITE);

session.flush();

assertThat(student.getId()).isEqualTo(2);

assertThat(address.getId()).isEqualTo(2);

}

3) CascadeType.LOCK :- It corresponds to the Hibernate native lock action.

Ex :-

@Test

public void whenDetachedAndLockedThenBothReattached() {

Student student = buildPerson("devendera");

Address address = buildAddress(student);

student.setAddresses(Arrays.asList(address));

session.persist(student);

session.flush();

assertThat(session.contains(student)).isTrue();

assertThat(session.contains(address)).isTrue();

session.detach(student);

assertThat(session.contains(student)).isFalse();

assertThat(session.contains(address)).isFalse();

session.unwrap(Session.class) .buildLockRequest(new LockOptions(LockMode.NONE)) .lock(student);

assertThat(session.contains(student)).isTrue(); assertThat(session.contains(address)).isTrue();

}

About Author

Author Image
Aditya Jaiswal

Aditya Jaiswal is an experienced backend developer with a specialization in Java. With over 1.6 years of experience in the industry, he possesses a strong understanding of the latest technologies, including Core Java, J2EE, Spring-Boot, Spring Security, MySQL, and Microservices. His expertise lies in gathering requirements from the functional team, analyzing, designing, and developing projects. He has made significant contributions to the success of various internal projects. He remains committed to enhancing his skills and knowledge by constantly learning and staying updated with the latest programming techniques and technologies.

Request for Proposal

Name is required

Comment is required

Sending message..