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
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 (
JPA One To Many example
We're gonna create a Spring project from scratch, then we implement JPA/Hibernate One to Many Mapping with
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:
|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
|PUT||/api/comments/:id||update a Comment by
|DELETE||/api/comments/:id||delete a Comment by
|DELETE||/api/tutorials/:id||delete a Tutorial (and its Comments) by
|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
comments table after that:
- Retrieve all Comments of specific Tutorial: GET
- Delete all Comments of specific Tutorial: DELETE
Check the comment table, all Comments of Tutorial with id=2 were deleted:
- Delete a Tutorial: DELETE
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
- Java 8
- Spring Boot 2.6.2 (with Spring Web MVC, Spring Data JPA)
- Maven 3.8.1
Let me explain it briefly.
Comment data model class correspond to entity and table tutorials, comments.
CommentRepository are interfaces that extends JpaRepository for CRUD methods and custom finder methods. It will be autowired in
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:
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
- 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: