Database Relationships In Spring Data JPA

tolunaykatirci
Huawei Developers
Published in
5 min readJul 5, 2021

--

Hello everyone, in this story, I will try to show Database relationships (One-to-Many, Many-to-Many, One-to-One) with examples in Spring Data JPA.

Let’s start 💪

Introduction

I created an example ER diagram for book database. I will try to explain the relations by including the scenario in this diagram in the project.

In this diagram, there is a Many-to-Many relationship between books and categories. A book can have more than one category, and a category can have more than one book. The same is true for the relationship between the book and the author.

Categories can have subcategories. These subcategories must be linked to a parent category. In this case, the One-to-Many relationship emerges.

There is also a photo table attached to the books. This table keeps the cover photos of the related book in different sizes. The relationship here will be One-to-One.

First we will create a Spring project using Spring Initializr. Then we will establish the database connection. I will use PostgreSQL database in this project.

Next, we will look at these relationships in more detail by applying the diagram through the code.

I used the following versions while developing this project.

  • Java 11.0.11
  • Spring 2.5.2
  • PostgreSQL 13.3–2
  • Intellij IDEA Community 2021.1.2

Installation

Let’s start with creating a new Spring application with Spring Initializr.

We should specify project metadata and select some required dependencies. After providing required data, we can download our demo project by clicking generate button.

Open the project in Intellij IDEA and connect to database by adding required information to application.properties file.

Implementation

In this section, we will implement the previously specified ER diagram.

Let’s start with entities without creating relations. For the construction and Getter & Setter methods, we will use Lombok library. Data annotation contains ToString, Getter, Setter, EqualsAndHashCode and RequiredArgsConstructor.

This is an example to Book entity. Other entities will be similar. After we created all entities and run the program, we should find these tables on our database.

We can check the restrictions from the Column section of any table properties. For example, for the Book table, title cannot be null.

If all OK, we can start to implement relations.

OneToOne Relation

Relationship between Book and Photo is an example to OneToOne relation.

For that relation, we need to create a foreign key for photo_id parameter in Book table. OneToOne annotation will create this relation for us. We should add this annotation to both entity classes that’s because this is a bidirectional relationship.

We want to create a column in Book table. So, we will use JoinColumn annotation in Book class. On the Photo class, simply put OneToOne annotation and fill mappedBy field by variable name on the owning side. In this example, this value will be ‘photo’.

Rerun the program and you will see the photo_id in columns and the foreign key in the properties of the Book table.

OneToMany Relation

Relationship between parent category and children categories in Category table is an example to OneToMany relation.

In this case, both parent and children tables are same, we will add both OneToMany and ManyToOne annotations to same class. We should join a column for parent_id.

After rerunning the program, we will see a foreign key for parent_id in properties of Category table.

ManyToMany Relation

Relationship between book and category is an example to ManyToMany relation.

In this relation, we need to create a new table to handle ManyToMany relationship. This new table will hold foreign keys for both book_id and category_id fields. We will follow the relationship between books and categories by this table.

After running the code, a new table called books_categories will be exist and specified foreign keys will be shown in properties of this new table.

We need same relation for book and author. After adding that, we can test our database.

Testing

In pgAdmin, we can find a Generate ERD (Beta) option by right click to the our database.

The result is quite similar to our ER diagram.

Conclusion

In this story, i tried to explain database relations in Spring Data JPA. I hope, it was helpful.

You can ask any questions or text me if there is a mistake in story.

You can find final codes on my GitHub profile. And I would be very happy if you want contribute 🙃

Thanks for reading…

References

https://www.baeldung.com/spring-data-rest-relationships

--

--