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

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

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

Related posts:
How to use Spring JPA with PostgreSQL | Spring Boot
React Redux + Spring Boot + PostgreSQL CRUD example
How to query alter domain models by Spring JPA Projection
How to configure Spring JPA One to One Relationship – SpringBoot
Spring JPA Hibernate One to Many Relationship – SpringBoot + MySQL
Spring JPA Hibernate Many to Many – SpringBoot + PostgreSQL

Related page:

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 & Contact with one-to-one relationship:

spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-uml-one-to-one-relationships

We create a SpringBoot project as below:

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

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

Hibernate JPA configuration for 2 models Student & Contact:

Student model ->

Contact model ->

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

– Students ->

  • @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

– Contacts ->

  • @GetMapping("/contacts"): get all Contacts
  • @GetMapping("/students/{studentId}/contacts"): get a Contact by Student’s ID
  • @PostMapping("/students/{studentId}/contacts"): add a Contact
  • @PutMapping("/contacts/{contactId}"): update a Contact
  • @DeleteMapping("/contacts/{contactId}"): delete a Contact 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:

OneToOne Models

Student model ->

Contact model ->

JPA Repositories

StudentRepository ->

ContactRepository model ->

Add datasource configurations in application.properties file ->

Expose Rest APIs

StudentController ->

ContactController ->

Run & Check Results

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

spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-tables-created-by-jpa

– Add Students ->

spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-tables-post-a-student

– Add Contacts ->

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

– Update Contact ->

spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-put-contact

– Update Student ->

spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-put-contact

– Get All Students ->

spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-post-get-all-student

– Get All Contacts ->

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

– Delete a Student ->

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

– Delete a Contact ->

spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-tables-delete-a-contact

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 ->

testdb=# \q: Exit

SourceCode

SpringBootRestAPIsOneToOne

By grokonez | August 21, 2018.



Related Posts


Got Something To Say:

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

*