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 Comment
s) 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:
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:
Here are the example requests:
- Create new Comments: POST
/api/tutorials/[:id]/comments
comments table after that:
- Retrieve all Comments of specific Tutorial: GET
/api/tutorials/[:id]/comments
- Delete all Comments of specific Tutorial: DELETE
/api/tutorials/[:id]/comments
Check the comment table, all Comments of Tutorial with id=2 were deleted:
- Delete a Tutorial: DELETE
/api/tutorials/[:id]
All Comments of the Tutorial with id=3 were CASCADE deleted automatically.
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
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:
- Spring JPA + H2 example
- Spring JPA + MySQL example
- Spring JPA + PostgreSQL example
- Spring JPA + Oracle example
- Spring JPA + SQL Server example
Further Reading
- Secure Spring Boot App with Spring Security & JWT Authentication
- Spring Data JPA Reference Documentation
- Spring Boot Pagination and Sorting example
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:
- Spring Boot @ControllerAdvice & @ExceptionHandler example
- @RestControllerAdvice example in Spring Boot
Or way to write Unit Test for the JPA Repository:
Spring Boot Unit Test for JPA Repository with @DataJpaTest
You can also know:
- how to deploy this Spring Boot App on AWS (for free) with this tutorial.
- dockerize with Docker Compose: Spring Boot and MySQL example
- way to upload an Excel file and store the data in MySQL database with this post
- upload CSV file and store the data in MySQL with this post.
Top comments (1)
Please explane me shortly is something wrong with application.
Connection with postgresql does not work
Thanx beforehand
Greetings