Spring Boot | Spring Data JPA
Understanding Relationships in JPA: Introduction
A series of articles where we will discuss the four types of relationships in JPA: one-to-one, one-to-many / many-to-one, and many-to-many
I’ll be honest with you — I used to struggle with implementing these relationships correctly in my applications. I found myself constantly searching on Google and reading several articles to make sure I was doing it right.
In order to gain a better understanding of how these relationships work, I decided to conduct thorough research and create a GitHub repository called ivangfr/spring-data-jpa-relationships. This repository contains simple examples for each type of relationship, and has now become my go-to reference whenever I need to implement them.
To demonstrate each relationship type, I will present the necessary code to map the entities and examine how JPA/Hibernate generates tables in PostgreSQL database by describing the tables in PostgreSQL terminal.
I hope this series of articles as well as the GitHub repository can be a helpful resource for you as well.
Following are the JPA relationships that we will be discussing:
One-to-One with Simple Primary Key
We will associate the Team
and TeamDetail
entities. A team can have only one team detail, and a team detail belongs to only one team. Besides, TeamDetail
entity will have its own auto increment ID.
One-to-One with Shared Primary Key
We will associate the Person
and PersonDetail
entities. A person can have only one person detail, and a person detail belongs to only one person. Additionally, we intend to utilize the Person
’s ID as the primary key for the PersonDetail
entity.
One-to-Many with Simple Primary Key
We will associate Restaurant
and Dish
entities. A restaurant can have zero, one, or more dishes, and a dish can only be in the menu of one restaurant. Additionally, the Dish
entity will have its own auto increment ID.
One-to-Many with Composite Primary Key
We will associate Player
and Weapon
entities. A player can have zero, one, or more weapons, and a weapon can only belong to one player. Additionally, we intend to utilize the Player
’s ID as the primary key for the Weapon
entity.
Many-to-Many with Simple Primary Key
We will associate Writer
and Book
entities. A writer can write zero, one, or more books, and a book can be written by one or more writers.
Many-to-Many with Simple Primary Key and Extra Column
We will associate Reviewer
, Article
and Comment
. Here, a reviewer can comment zero, one or more articles and an article can be commented by zero, one or more reviewers.
Many-to-Many with Composite Primary Key and Extra Column
We will associate Student
and Course
entities. A student can register to zero, one or more courses and one course can be composed by zero, one or more students. Additionally, it would be interesting to store the date when the student registered for the course and the student's grade in the course.
Conclusion
In this series of articles, we will explore the four different types of relationships in JPA: one-to-one, one-to-many / many-to-one, and many-to-many. By presenting simple examples for each type of relationship and providing necessary code to map the entities, we hope to help developers to gain a better understanding of the JPA relationships.
Support and Engagement
If you enjoyed this article and would like to show your support, please consider taking the following actions:
- 👏 Engage by clapping, highlighting, and replying to my story. I’ll be happy to answer any of your questions;
- 🌐 Share my story on Social Media;
- 🔔 Follow me on: Medium | LinkedIn | Twitter | GitHub;
- ✉️ Subscribe to my newsletter, so you don’t miss out on my latest posts.