1. Introduction
In this tutorial, we’ll explore the capabilities of JPA in performing cascading deletes on unidirectional One-to-Many relationships between related entities.
We’ll briefly explain what cascading delete entails in this context. Then, we will use a straightforward example to demonstrate how JPA can achieve the desired outcome. Finally, we will conduct an integration test on an in-memory H2 database to verify that the process works correctly.
2. Unidirectional One-to-Many Relationship
Essentially, in a relational data model, a unidirectional one-to-many relationship is a type of relationship between two tables where one table has multiple related records in another table. Still, the second table doesn’t directly relate to the first table. This means that the relationship flows in only one direction.
Moving onto the JPA, a unidirectional One-to-Many relationship can be established between two entities when one entity has a reference to the collection of related entities. Still, we cannot traverse back from the related entities to the first entity. Generally, the entity containing the reference is called the parent entity, and the referenced entity is called the child entity.
Let’s consider an example of an article and its comments. As we can imagine, an article can have many associated comments, but one comment can only belong to one article. Here the Article is the parent, and the Comment is the child entity.
Now, let’s set up the JPA entities representing the Article and Comment next:
We want the Article to reference all its comments alongside other fields:
@Entity
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@OneToMany
private Set<Comment> comments = new HashSet<>();
//...setters and getters
}
In this example, we’ve added a Set
Next, let’s define the Comment:
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String description;
private Date date;
//...setters and getters
}
We can see no reference to the Article inside the Comment entity. Hence, our example represents a unidirectional relationship.
3. Cascading Delete
In the example above, if we decide to delete the Article, any Comment associated with it will be left as a dangling reference or orphaned object.
To solve this problem, JPA provides a few properties that can be used to propagate the deletion and to clean up orphaned objects.
Let’s expand on our @OneToMany annotation on the Set
JPA also provides an option to set the cascading level for all operations. This is called CascadingType.All. If we only want to cascade the removal of child entities when the parent entity is deleted, then we can use CascadingType.Remove.
The second option we’ll use in conjunction with cascade is the orphanRemoval.
Let’s leverage these two options provided by JPA alongside our @OneToMany annotation established earlier on:
@Entity
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Comment> comments = new HashSet<>();
//...setters and getters
}
In addition to using CascadeType.All or CascadeType.remove, it is essential to set the orphanRemoval property to true to ensure the proper deletion of orphaned entities. With this property set, JPA automatically removes any orphaned entities from the database. By using either cascadingDelete.All or cascadingDelete.Remove with OrphanedRemoval=true; we can achieve efficient data management, maintain data integrity, and facilitate the automatic removal of obsolete references and database cleanup.
4. Integration Test
Let’s test our cascading deletion using an in-mem H2 database, assuming we have set up the service and the JPA repository layers. Primarily, we’ll test that all its associated Comments are automatically deleted when an Article is deleted.
@Test
@Transactional
public void givenAnArticleAndItsComments_whenDeleteArticle_thenCommentsDeletedAutomatically() {
Set<Comment> comments = new HashSet<>();
Article article = new Article();
article.setName("introduction to Spring");
Comment comment1 = new Comment();
comment1.setDescription("Explain types of Autowired");
comment1.setDate(Date.from(Instant.now()));
Comment comment2 = new Comment();
comment2.setDescription("Good article");
comment2.setDate(Date.from(Instant.now()
.minus(10, ChronoUnit.MINUTES)));
comments.add(comment1);
comments.add(comment2);
article.setComments(comments);
articleRepository.save(article);
List<Article> articles = articleRepository.findAll();
assertThat(articles.size()).isEqualTo(1);
Article retrivedArticle = articles.get(0);
List<Comment> fetchedComments = commentRepository.findAll();
assertThat(fetchedComments.size()).isEqualTo(2);
articleService.deleteArticle(retrivedArticle);
assertThat(articleRepository.findAll()).isEmpty();
assertThat(commentRepository.findAll()).isEmpty();
}
Here, we can see the cascading delete in action. After we delete the Article using articleService.deleteArticle(retrievedArticle), and then retrieve the comments using commentRepository.findAll(), we get an empty list which means all the comments have been deleted by cascading deletion from Article(parent) to the Comment (child).
5. Conclusion
In this article, we had a brief overview of Cascading deletion, focusing on unidirectional One-to-Many relationships. We looked at cascade and orphan removal options provided by JPA with @OnetoMany annotation to achieve cascading deletion and data integrity in the database. We looked at both CascasdeType.All and CascadeType.Remove to achieve the cascading deletion of associated child entities when the parent entity is deleted. Furthermore, we also highlighted the importance of using the OrphanRemoval option to ensure the database records are deleted as required.
Finally, we set up a Spring Boot integration test to see cascading delete in action.
As always, the full source code of the article is available over on GitHub.