@OneToMany orphanRemoval true example in Hibernate/JPA

In this tutorial, we will see @OneToMany orphanRemoval true example in Hibernate/JPA using Spring Boot and Oracle.

Basic points about orphanRemoval true.

  • The orphanRemoval true is an optional element which has been defined in @OneToMany annotation. it has been introduced in JPA 2.0.
  • The default value of orphanRemoval is false.

public @interface OneToMany {
    boolean orphanRemoval() default false;
}

  • Example to use orphanRemoval = true.

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "book_id", referencedColumnName = "bookId")
private List<Story> storyList = new ArrayList<>();

  • Consider two entities (Book/Parent and Story/Child) are in OneToMany mapping and we are using orphanRemoval = true. If we remove Story entities(child entities) from Book entity the story/child record will be deleted automatically from the database. In case if we don’t use orphanRemoval = true the child entities will not get deleted from the database only foreign key will updated to null(we will see later in details).

 

Understanding Hibernate/JPA @OneToMany orphanRemoval = true.

In this section, we will see what exactly orphanRemoval = true does if we use with @OneToMany annotation. Suppose we have two entities called Book.java and Story.java. Both are in OneToMany relationship(One book can have multiple stories).

Book.java

Story.java

 

Also, we have BookServiceImpl.java class which contains logic to save and delete.

 

Now we are using orphanRemoval = true in Book.java with storyList field. If we try to remove story entities(child entities) from Book entity(Parent entity)(line number 30), the story/child record will be deleted automatically from the database.

If we don’t use orphanRemoval = true and will perform the same operation it will not delete story/child record from the database.

We will later complete example from scratch. For now, just see what will happen if we perform save operation and try to remove Stories from Book in both cases.

With orphanRemoval = true

http://localhost:9091/book/savebook

@OneToMany orphanRemoval true example in Hibernate/JPA

We should have some record in the database.

 

Removing Stories from Book using below API.

http://localhost:9091/book/1

Let’s check the database.

@OneToMany orphanRemoval true example in Hibernate/JPA

Generated Query with orphanRemoval = true.

Hibernate: select book0_.book_id as book_id1_0_0_, book0_.book_name as book_name2_0_0_ from book book0_ where book0_.book_id=?

Hibernate: select storylist0_.book_id as book_id3_1_0_, storylist0_.story_id as story_id1_1_0_, storylist0_.story_id as story_id1_1_1_, storylist0_.story_name as story_name2_1_1_ from story storylist0_ where storylist0_.book_id=?

Hibernate: update story set book_id=null where book_id=?
Hibernate: delete from story where story_id=?
Hibernate: delete from story where story_id=?

 

Without orphanRemoval = true

Let’s do the same steps except don’t use orphanRemoval = true.

http://localhost:9091/book/savebook

We should have some record in the database.

 

Removing Stories from Book using below API.

http://localhost:9091/book/1

 

@OneToMany orphanRemoval true example in Hibernate/JPA

Generated query without orphanRemoval = true.

Hibernate: select book0_.book_id as book_id1_0_0_, book0_.book_name as book_name2_0_0_ from book book0_ where book0_.book_id=?
Hibernate: select storylist0_.book_id as book_id3_1_0_, storylist0_.story_id as story_id1_1_0_, storylist0_.story_id as story_id1_1_1_, storylist0_.story_name as story_name2_1_1_ from story storylist0_ where storylist0_.book_id=?

Hibernate: update story set book_id=null where book_id=?

 

Different between cascade = CascadeType.REMOVE and orphanRemoval = true.

For beginners, it might little be confusing what is the difference between cascade = CascadeType.REMOVE and orphanRemoval = true.

If we don’t use CascadeType.REMOVE then we will not able to delete child entity along with the parent. We have a separate detailed post here.

 

@OneToMany orphanRemoval true example in Hibernate/JPA Using Sping Boot and Oracle.

Create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – orphanremoval, ArtifactId – orphanremoval and name – orphanremoval) and click on finish. Keep packaging as the jar.

Modify the pom.xml with the below code.

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps.

Define entity class i.e Book.java and Story.java.

Book.java

Story.java

 

Define the repository interface extending CrudRepository.

BookRepository.java

Define service interface i.e BookService.java

Define service implementation class.

BookServiceImpl.java

Note – See here more about @Component, @Controller, @Service and @Repository annotations here.

Define the controller class or endpoint.

BookController.java

Note – See more details about @Controller and RestController here.

Define the JpaConfig.java

Define the SpringMain.java

 

And finally, we have an application.properties file where we have database details.

application.properties

We are almost done. Just build the project once running the main method. Open git bash or cmd and Run mvn clean install.

Let’s deploy the application running SpringMain class as a java application.

Sample request JSON data to test the end point.

 

POST – http://localhost:9091/book/savebook

DELETE – http://localhost:9091/book/1

 

That’s all about @OneToMany orphanRemoval true example in Hibernate/JPA Using Spring Boot and Oracle.

You may like.

Other Tutorial.

 

OneToMany orphanRemoval  true docs.

 

Top