Spring JPA/Hibernate One-to-Many Association + PostgreSQL | SpringBoot CRUD RestAPIs Post/Get/Put/Delete example

spring-jpa-hibernate-one-to-many-association-postgresql-springboot-crud-restapis-post-get-put-delete-example-feature-image-new

In the tutorial, we show how to expose CRUD RestAPIs Post/Get/Put/Delete to interact with Hibernate Spring JPA One-to-Many association models using SpringBoot and PostgreSQL database.

Previous post:
Expose CRUD RestAPIs Spring JPA One-to-One Association

Related articles:
Spring JPA – Many to Many relationship
How to configure Spring JPA One to One Relationship – SpringBoot
Spring Data Rest – JPA One-to-Many relational entities | SpringBoot + MySql + HAL Browser
Kotlin SpringJPA Hibernate One-To-Many relationship

Related pages:

Technologies

  • Java 8
  • Maven 3.5.4
  • SpringToolSuite version 3.9.4.RELEASE
  • SpringBoot 2.0.4.RELEASE

Demo

Overview

We have 2 models Student & Assignment with One-to-Many association relationship:

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-association-relationship

We create a SpringBoot project as below:

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-architecture

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-project-structure

Hibernate JPA configuration for 2 models Student & Assignment:

Student model ->

Assignment model ->

We exposes RestAPIs for Post/Get/Put/Delete Students & Assignments:

– Student ->

  • @GetMapping("/api/students"): get all Students
  • @GetMapping("/api/students/{id}"): get a Student by ID
  • @PostMapping("/api/students"): post a Student
  • @PutMapping("/api/students/{id}"): update a Student
  • @DeleteMapping("/api/students/{id}"): delete a Student

– Assignments ->

  • @GetMapping("/students/{studentId}/assignments"): get a Assignment by Student’s ID
  • @PostMapping("/students/{studentId}/assignments"): add an Assignment
  • @PutMapping("/students/{studentId}/assignments/{assignmentId}"): update an Assignment
  • @DeleteMapping("/students/{studentId}/assignments/{assignmentId}"): delete an Assignment by ID

Now we’ll create a project from scratch ->

Practice

Create SpringBoot project

We use SpringToolSuite to create a Java 8 SpringBoot project with below dependencies:

OneToMany Models

Student model ->

Assignment model ->

JPA Repositories

StudentRepository ->

AssignmentRepository ->

Add datasource configurations in application.properties file ->

Expose Rest APIs

– Create NotFoundException ->

StudentController APIs ->

AssignmentController APIs ->

Run & Check Results

– Run the SpringBoot project with commandline mvn spring-boot:run.
2 tables is created in PostgreSQL ->

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-initial-tables

– Add Students ->

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-initial-tables

– Add Assignments ->

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-post-a-assignment

– Get All Students ->

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-get-all-students

– Get All Assignments of a Student->

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-get-all-assignment-of-a-student

– Update Student ->

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-put-a-student

– Update Assignment ->

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-put-a-assignment

– Delete a Student -> image

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-delete-a-student

– Delete an Assignment ->

jpa-hibernate-one-to-many-spring-boot-rest-apis-spring-jpa-one-to-many-postgresql-delete-an-assignment

Note: PostgreSQL commandline ->

PostgreSQL\9.6\bin>psql.exe --username="postgres" -W: connect to PostgreSQL ->

\l: List of databases

\c testdb: connect to “testdb”

\d: List of relations ->

\d assignments: Get Schema of a table assignments ->

SourceCode

SpringBootRestAPIOneToMany

By grokonez | August 22, 2018.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*