In this tutorial, you will learn a various ways to delete data using JPA/Hibernate Entity such as JPQL, CascadeType.REMOVE or CascadeType.ALL, and orphanRemoval. You will also learn to do JPA integration test in Spring Boot with in-memory database, @DataJpaTest and @DirtiesContext. Let's start building an example with Spring Boot and Spring Data JPA

What you will need

  • JDK 8+ and Maven 3+

Project structure

Project dependencies


Define JPA Entity and Repository





Application Configuration



Integration test Deleting Data with JPA/Hibernate and @DataJpaTest


@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) reset the test environment context after each test method

Keys to take away

  • Using JPQL, you don't need retrieve the data before deletion, hence this is a best approach for bulk delete.
  • Using cascade = CascadeType.REMOVE or cascade = CascadeType.ALL to delete the associated entities when deleting the relationship owner. JPA/Hibernate will delete one-by-one so considering the performance tradeoff when using this option.
  • Using orphanRemoval if you need to delete the associated entities only.

That's it folks! Thanks for joining and have a happy coding. Full source code of this example is available as below

Source code

https://github.com/hellokoding/springboot-examples/tree/master/jpa-hibernate/crud-deleting-entity