JPA CascadeType.REMOVE vs orphanRemoval

JPA CascadeType.REMOVE vs orphanRemoval

Learn about the difference between JPA CascadeType.REMOVE and orphanRemoval for deleting entities.

1. Overview

In this tutorial, we'll be discussing the difference between two of the options we have for removing entities from our databases when working with JPA.

First, we'll start with CascadeType.REMOVE which is a way to delete a child entity or entities when the deletion of its parent happens. Then we'll take a look at the orphanRemoval attribute, which was introduced in JPA 2.0. This provides us with a way to delete orphaned entities from the database.

Throughout the tutorial, we'll be using a simple online store domain to demonstrate our examples.

2. Domain Model

As mentioned earlier, this article makes use of a simple online store domain. Wherein the OrderRequest has a ShipmentInfo and a list of LineItem.

Given that, let's consider:

  • For the removal of ShipmentInfo, when the deletion of an OrderRequest happens, we'll use CascadeType.REMOVE
  • For the removal of a LineItem from an OrderRequest, we'll use orphanRemoval

First, let's create a _ShipmentInfo _entity:

@Entity
public class ShipmentInfo {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    // constructors
}

Next, let's create a LineItem entity:

@Entity
public class LineItem {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    @ManyToOne
    private OrderRequest orderRequest;

    // constructors, equals, hashCode
}

Lastly, let's put it all together by creating an OrderRequest entity:

@Entity
public class OrderRequest {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(cascade = { CascadeType.REMOVE, CascadeType.PERSIST })
    private ShipmentInfo shipmentInfo;

    @OneToMany(orphanRemoval = true, cascade = CascadeType.PERSIST, mappedBy = "orderRequest")
    private List<LineItem> lineItems;

    // constructors

    public void removeLineItem(LineItem lineItem) {
        lineItems.remove(lineItem);
    }
}

It's worth highlighting the removeLineItem method, which detaches a LineItem from an OrderRequest.

java programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Top 10+ Java Development Companies in India & Developers Reviews 2020 – TopDevelopers.co

A thoroughly researched list of top 10 Java development companies in India with ratings & reviews to help find the best java developers around India.

Hire Java Developers Team | Java Developer India - Developers.dev

.DEV is a leading java Developer agency. Our offshore Java Developer Staff offers robust cross-platform java development services at an affordable rate. 1000+ Sucessful Projects, 100+ Happy Customers, 650+ Professionals, 60% Saving on Project Cost

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

3 Best Programming Languages for Java Developers

In this article, I’ll share 3 JVM Programming languages Java programmers can learn and why you should learn them. Being a Polyglot developer is of good quality and often valued highly in interviews.