DEV Community

Cover image for JPA Many to One UniDirectional example
bezkoder
bezkoder

Posted on

JPA Many to One UniDirectional example

In this tutorial, I will show you how to implement JPA Many to One UniDirectional mapping with Hibernate in a Spring Boot example using @ManyToOne annotation. You'll know:

  • How to configure Spring Data, JPA, Hibernate to work with Database
  • How to define Data Models and Repository interfaces for JPA Many-to-One relationship
  • Way to use Spring JPA to interact with Database for Many-to-One association
  • Way to create Spring Rest Controller to process HTTP requests

Full Article: JPA Many to One UniDirectional example

JPA Many to One UniDirectional: Appropriate way to implement One To Many mapping

In a relational database, a One-to-Many relationship between table A and table B indicates that one row in table A links to many rows in table B, but one row in table B links to only one row in table A.

For example, you need to design data model for a Tutorial Blog in which One Tutorial has Many Comments. So this is a One-to-Many association.

You can map the child entities as a collection (List of Comments) in the parent object (Tutorial), and JPA/Hibernate provides the @OneToMany annotation for that case: only the parent-side defines the relationship. We call it unidirectional @OneToMany association.

Similarly, when only the child-side manage the relationship, we have unidirectional Many-to-One association with @ManyToOne annotation where the child (Comment) has an entity object reference to its parent entity (Tutorial) by mapping the Foreign Key column (tutorial_id).

The most appropriate way to implement JPA/Hibernate One To Many mapping is unidirectional Many-to-One association with @ManyToOne. You can read Vlad Mihalcea's article for more details.

JPA Many to One UniDirectional example

We're gonna create a Spring project from scratch, then we implement JPA/Hibernate One to Many Mapping with tutorials and comments table as following:

jpa-many-to-one-diagram

We also write Rest Apis to perform CRUD operations on the Comment entities.

These are APIs that we need to provide:

Methods Urls Actions
POST /api/tutorials/:id/comments create new Comment for a Tutorial
GET /api/tutorials/:id/comments retrieve all Comments of a Tutorial
GET /api/comments/:id retrieve a Comment by :id
PUT /api/comments/:id update a Comment by :id
DELETE /api/comments/:id delete a Comment by :id
DELETE /api/tutorials/:id delete a Tutorial (and its Comments) by :id
DELETE /api/tutorials/:id/comments delete all Comments of a Tutorial

Assume that we've had tutorials table like this:

jpa-many-to-one-parent-table

Here are the example requests:

  • Create new Comments: POST /api/tutorials/[:id]/comments

jpa-many-to-one-example-create-child-records

comments table after that:

jpa-many-to-one-child-table

  • Retrieve all Comments of specific Tutorial: GET /api/tutorials/[:id]/comments

jpa-many-to-one-example-spring-crud-retrieve

  • Delete all Comments of specific Tutorial: DELETE /api/tutorials/[:id]/comments

jpa-many-to-one-example-spring-crud-delete

Check the comment table, all Comments of Tutorial with id=2 were deleted:

jpa-many-to-one-example-spring-crud-table-delete

  • Delete a Tutorial: DELETE /api/tutorials/[:id]

jpa-many-to-one-example-spring-crud-delete-cascade

jpa-many-to-one-example-spring-crud-delete-parent-table

All Comments of the Tutorial with id=3 were CASCADE deleted automatically.

jpa-many-to-one-example-spring-crud-delete-cascade-table

Let's build our Spring Boot One to Many CRUD example.

Spring Boot Many to One example

Technology

  • Java 8
  • Spring Boot 2.6.2 (with Spring Web MVC, Spring Data JPA)
  • PostgreSQL/MySQL
  • Maven 3.8.1

Project Structure

jpa-many-to-one-example-hibernate-spring-boot-project-structure

Let me explain it briefly.

Tutorial, Comment data model class correspond to entity and table tutorials, comments.
TutorialRepository, CommentRepository are interfaces that extends JpaRepository for CRUD methods and custom finder methods. It will be autowired in TutorialController, CommentController.
TutorialController, CommentController are RestControllers which has request mapping methods for RESTful CRUD API requests.
– Configuration for Spring Datasource, JPA & Hibernate in application.properties.
pom.xml contains dependencies for Spring Boot and MySQL/PostgreSQL/H2 database.

– About exception package, to keep this post straightforward, I won't explain it. For more details, you can read following tutorial:
@RestControllerAdvice example in Spring Boot

For step by step and Github, please visit:
https://www.bezkoder.com/jpa-one-to-many/

You can apply this implementation in following tutorials:

Further Reading

If you want to add Pagination to this Spring project, you can find the instruction at:
Spring Boot Pagination & Filter example | Spring JPA, Pageable

To sort/order by multiple fields:
Spring Data JPA Sort/Order by multiple Columns | Spring Boot

Handle Exception for this Rest APIs is necessary:

Or way to write Unit Test for the JPA Repository:
Spring Boot Unit Test for JPA Repositiory with @DataJpaTest

You can also know:

Top comments (0)