OneToMany Mapping using @JoinTable in Hibernate/JPA

In this post, We will see OneToMany bidirectional mapping using @JoinTable example in Hibernate/JPA. We will see how to use @JoinTable with OneToMany bidirectional mapping. We are going to use Spring Boot, Oracle database and postman(for testing). We have a separate tutorial for OneToMany bidirectional mapping without using @JoinTable.

Many To Many @JoinTable Example In Hibernate/JPA Using Spring Boot And Oracle.

Note – For this tutorial, we are not going to create the table manually. Let’s hibernate do this job.


We have two entity and Both entities are mapped into bidirectional mapping. Consider Book entity is the parent entity and Story entity is a child entity.



Using @JoinTable and @JoinColumn we can define the table name and column name. For example, in the above code snippet, we have defined book_story table. The book_story table contains two columns book_id and story_id which we have defined using @JoinColumns annotation.

Now we are going to see OneToMany Mapping using @JoinTable in Hibernate/JPA table details.

Table details.

When we use @JoinTable we will have a separate table(let’s say book_story in our case). There will be three tables book, story and book_story(Define using @JoinTable).

OneToMany Mapping using @JoinTable in Hibernate/JPA

We will have rest API to save the record to the database.

Rest endpoints for this example.

1. Save book(POST) – http://localhost:9091/book/savebook – Observ in below response one book have many stories.

Request data

“bookName”:”Premchand’s stories”

Response Data


OneToMany Mapping using @JoinTable in Hibernate/JPA using Spring Boot and Oracle

2. Get the book (GET) – http://localhost:9091/book/{bookId}- Get book details.

@JoinTable annotation using Spring Boot

OneToMany Mapping using @JoinTable in Hibernate/JPA, Spring Boot and Oracle.

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

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


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.

Let maven download all necessary jar. Once it is done we will able to see the maven dependency folder which contains different jar files. We can start writing our controller classes, ServiceImpl and Repository. The directory structure of the application looks as below.


Define the repository interface extending CrudRepository.


Define the Service interface.


Define service implementation class.


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


Define the


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


Let’s run the SpringMain class and test the endpoints.



Generated Query while save and get operation.

Below Query Generated while the saving operation.

Hibernate: insert into book (book_name, id) values (?, ?)
Hibernate: insert into story (story_name, id) values (?, ?)
Hibernate: insert into book_story (book_id, story_id) values (?, ?)
Hibernate: insert into story (story_name, id) values (?, ?)
Hibernate: insert into book_story (book_id, story_id) values (?, ?)
Hibernate: insert into story (story_name, id) values (?, ?)
Hibernate: insert into book_story (book_id, story_id) values (?, ?)

Below Query generated while the get operation.

Hibernate: select as id1_0_0_, book0_.book_name as book_name2_0_0_, storylist1_.book_id as book_id1_1_1_, as story_id2_1_1_, as id1_2_2_, story2_.story_name as story_name2_2_2_,
story2_1_.book_id as book_id1_1_2_
from book book0_ left outer join book_story storylist1_ on
left outer join story story2_ on
left outer join book_story story2_1_ on where

Some basic points about @JoinTable.

  • @JoinTable annotation is available in javax.persistence package and introduced in JPA 1.0.
  • This annotation can be used with the getter method or field.
  • Some important attributes of @JoinTable annotation are name, joinColumns, inverseJoinColumns etc.


That’s all about OneToMany Mapping using @JoinTable in Hibernate/JPA Using Spring Boot and Oracle.

More hibernate/JPA association mapping examples.

One To One unidirectional mapping example.
One To One Bidirectional mapping example.
One To Many unidirectional mapping example.
Many To one unidirectional mapping example.

Spring Data JPA example.


See Hibernate Docs.