Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › JPA › Relationships

Cascade and orphan removal

What happens to the children when you save or delete only the parent?

Why it exists — When you handle entities that are created together and disappear together, such as a post and its comments, without cascade you have to call em.persist or em.remove separately for each child, and if you miss even one, a save is skipped or a delete fails. Cascade makes the same operation happen to the children when you save or delete the parent, and orphanRemoval deletes the rows of children removed from the collection, so that just working with the parent object keeps the rows of the children in line.

Where it's used today — In a relationship where a child belongs to only that one parent, such as an order and its order items, or a post and its attached files, CascadeType.ALL and orphanRemoval = true are often used together. It is also used so that when you delete one item of a list on a screen, just removing it from the collection sends a DELETE. You do not set it on an entity that several orders point to together, such as a product, and you save and delete it separately.

A post (the parent) has two comments (the children). Whether the comments are saved and deleted together when you persist or remove only the post is decided by cascade and orphanRemoval on @OneToMany.

Checked against real execution · 24 combinations were run on Hibernate 7.4.5 and H2, and the number and order of the SQL statements, the exceptions, and the rows left in the DB were checked

Concepts in this lab

Cascade
A setting that passes an operation performed on a parent entity on to its associated child entities in the same way. You write it in the cascade attribute of an association annotation such as @OneToMany or @OneToOne. CascadeType.PERSIST passes on saving, REMOVE passes on deletion, and ALL passes on every operation. Without it, persisting only the parent does not save the children, and removing a parent that still has children fails because of the foreign key constraint. It is a convenience that passes operations on, not a feature that sets up the relationship, and you use it when only that one parent owns the children.
Orphan removal (orphanRemoval)
A setting that also deletes from the DB a child entity that has been disconnected from its parent entity. You write it as orphanRemoval = true on @OneToMany or @OneToOne. If you take a child out of the parent's collection, a DELETE for that child is sent when the transaction commits. Without this setting, the row in the DB stays even if you take the child out of the collection. Children are also deleted when the parent is deleted, so you can think of it as including the behavior of CascadeType.REMOVE. Do not use it for a child that other entities also refer to.
Persistence context
The logical space in JPA that holds and manages entities. Entities that you load or save are kept here, and JPA tracks the changes made to them and applies those changes to the database when the transaction ends. It usually shares its lifecycle with one transaction (in a web request with Open Session in View (OSIV) turned on, it lasts until the request ends). Features such as the first-level cache, dirty checking, and transactional write-behind work on top of it.
Transactional write-behind and flush
Why it exists — If SQL were sent right away every time an entity changed, then even changing the same row several times would send that many UPDATE statements, and every statement would make a trip to the database. Transactional write-behind delays the SQL until the flush, sends it once with the final state, and, depending on the settings, lets several statements be grouped into a JDBC batch.
Where it's used today — In batch jobs that save many rows, you turn on hibernate.jdbc.batch_size to group INSERT statements (they are not grouped with the IDENTITY strategy). In tests, you send the SQL right away with em.flush() and em.clear() and read it back from the database to check.
Transactional write-behind means that INSERT, UPDATE, and DELETE statements are not sent to the database right away. They are collected and sent together. However, for an entity whose primary key is generated with the IDENTITY strategy (such as AUTO_INCREMENT in MySQL), the INSERT runs immediately when you request the save, because the identifier must be obtained. A flush sends the collected changes to the database. It happens automatically just before the transaction commits and just before a JPQL query runs. A flush only sends SQL to the database. It does not commit the transaction.
Entity
A Java object that corresponds to a row of a database table. It is defined as a class annotated with @Entity and has an identifier (@Id). JPA tracks the state changes of these objects and runs the matching SQL.

Try these

Running with the default settings

  1. new Post, addComment twice: Created one post object and two comment objects and connected them to each other. They are still ordinary Java objects that JPA does not know about (New). The DB has nothing.
  2. em.persist(post): The post comes under the management of the persistence context. The primary key uses the IDENTITY strategy, so the INSERT is sent right away to get the id.
  3. Because cascade does not have PERSIST, the persist stops at the post. The two comments that the list points to are still New objects that JPA does not know about.
  4. Method ends, commit: It commits without an exception. comments is the side with mappedBy, so it does not manage the foreign key, and Hibernate does not complain even if this list points to objects that have not been saved. The comment INSERTs were never sent.

Result of this run

Code

@Entity
class Post {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    String title;

    @OneToMany(mappedBy = "post")
    List<Comment> comments = new ArrayList<>();

    protected Post() { }               // the no-argument constructor JPA uses to create the entity
    Post(String title) { this.title = title; }

    public List<Comment> getComments() { return comments; }

    void addComment(String text) {     // sets both sides together
        Comment c = new Comment(text);
        c.post = this;
        comments.add(c);
    }
}

@Entity
class Comment {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    String text;

    @ManyToOne(fetch = FetchType.LAZY)
    Post post;                         // foreign key post_id

    protected Comment() { }
    Comment(String text) { this.text = text; }
}

// Runs inside a transaction (imports, the method declaration, and the declaration of EntityManager em omitted)
Post post = new Post("Notice");
post.addComment("First comment");
post.addComment("Second comment");
em.persist(post);                      // persists only the post
// The commit happens when the method ends

Assumptions of this model

This lab assumes a bidirectional relationship (@OneToMany(mappedBy) and @ManyToOne), a primary key with the IDENTITY strategy, and execution inside a single transaction. All 24 combinations (4 cascade settings, 2 orphanRemoval settings, and 3 actions) were run on real Hibernate 7.4.5 and an H2 database, and the number and order of the SQL statements sent, the exception, and the number of rows left in the DB after it ended were confirmed to match the screen. The SQL statements on the screen are shortened to be easy to read and are not letter-for-letter the same as the actual statements. The result that "if you persist only the parent without cascade, the children are left out without an exception" holds for this mapping, where the comment list has mappedBy. In the same environment, it was confirmed that if you change it to a unidirectional @OneToMany(@JoinColumn), where the list side manages the foreign key directly, and run the same code, a TransientPropertyValueException is thrown at commit and the post is not saved either. The opposite case, persisting only a comment that points to a post that has not been saved, is not covered in this lab. If the primary key uses the SEQUENCE strategy, the INSERTs are sent at commit, not when you call persist (this is covered in the "Persistence context" lab). Setting ON DELETE CASCADE on the foreign key in the DB is a different feature from cascade in JPA and is not covered here.

Previous lab: Inheritance mapping · Next lab: Proxies and lazy fetching

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these