DEV Community

Cover image for JPA One To Many example with Hibernate and Spring Boot
Tien Nguyen
Tien Nguyen

Posted on • Edited on

JPA One To Many example with Hibernate and Spring Boot

In this tutorial, I will show you how to implement Spring JPA One-To-Many mapping with Hibernate in a Spring Boot CRUD 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 One-To-Many relationship
  • Way to use Spring JPA to interact with Database for One-To-Many association
  • Way to create Spring Rest Controller to process HTTP requests

Full Article: https://www.bezkoder.com/jpa-one-to-many/

Read more JPA Many to Many example

Appropriate way to implement JPA/Hibernate 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 One To Many 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-one-to-many-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-one-to-many-parent-table

Here are the example requests:

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

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

comments table after that:

jpa-one-to-many-child-table

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

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

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

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

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

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

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

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

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

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

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

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

Spring Boot One to Many 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-one-to-many-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 Repository with @DataJpaTest

You can also know:

Top comments (1)

Collapse
 
nenastefanovic profile image
Nena Stefanovic

Please explane me shortly is something wrong with application.
Connection with postgresql does not work

Image description

Thanx beforehand

Greetings