SpringBoot + Hibernate Spring JPA One-to-One Association + PostgreSQL | CRUD RestAPIs Post/Get/Put/Delete
Tags: springboot, postgres, hibernate, onetoone
Published: 2021-04-03 11:56:21

https://grokonez.com/spring-framework/spring-data/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

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

Related posts:
- <a href="https://grokonez.com/spring-framework/use-spring-jpa-postgresql-spring-boot">How to use Spring JPA with PostgreSQL | Spring Boot</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/react-redux-spring-boot-postgresql-crud-example">React Redux + Spring Boot + PostgreSQL CRUD example</a>
- <a href="https://grokonez.com/spring-framework/spring-data/query-alter-domain-model-spring-jpa-projection-springboot-mysql-database">How to query alter domain models by Spring JPA Projection</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/configure-spring-jpa-one-one-relationship-springboot">How to configure Spring JPA One to One Relationship – SpringBoot</a>
- <a href="https://grokonez.com/spring-framework/spring-data/configure-spring-jpa-one-to-many-relationship-springboot">Spring JPA Hibernate One to Many Relationship – SpringBoot + MySQL</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/spring-jpa-many-to-many">Spring JPA Hibernate Many to Many – SpringBoot + PostgreSQL</a>

Related page:
<ul>
<li><a href="https://grokonez.com/spring-framework-tutorial/spring-boot">SpringBoot Tutorials</a></li>
</ul>

<!--more-->
<h2>Technologies</h2>
<ul>
<li>Java 8</li>
<li>Maven 3.5.4</li>
<li>SpringToolSuite version 3.9.4.RELEASE</li>
<li>SpringBoot 2.0.4.RELEASE</li>
</ul>
<h2>Demo</h2>
<iframe width="853" height="480" src="https://www.youtube.com/embed/MA6B2KfrxkI?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<h2>Overview</h2>
We have 2 models <code>Student</code> & <code>Contact</code> with <code>one-to-one</code> relationship:

<img src="https://grokonez.com/wp-content/uploads/2018/08/spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-uml-one-to-one-relationships.png" alt="spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-uml-one-to-one-relationships" width="504" height="274" class="alignnone size-full wp-image-14351" />

We create a SpringBoot project as below:

<img src="https://grokonez.com/wp-content/uploads/2018/08/spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-architecture.png" alt="spring-boot-rest-apis-hibernate-spring-jpa-one-to-one-postgresql-architecture" width="701" height="325" class="alignnone size-full wp-image-14352" />

<img src="https://grokonez.com/wp-content/uploads/2018/08/spring-boot-rest-apis-spring-jpa-one-to-one-postgresql-project-structure.png" alt="spring-boot-rest-apis-spring-jpa-one-to-one-postgresql-project-structure" width="346" height="450" class="alignnone size-full wp-image-14353" />

Hibernate JPA configuration for 2 models <code>Student</code> & <code>Contact</code>:

- <code>Student</code> model ->

<pre><code class="language-java">
@Entity
@Table(name = "students")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 
public class Student implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
    
    @Column(name = "name")
	private String name;
    
    @Column(name = "age")
	private int age;
    
    @OneToOne(fetch = FetchType.LAZY,
            cascade =  CascadeType.ALL,
            mappedBy = "student")
    private Contact contact;
	
	public Student() {}
	
	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	// Getters & Setters methods
	// ...
</code></pre>

More at: https://grokonez.com/spring-framework/spring-data/springboot-hibernate-spring-jpa-one-to-one-association-postgresql-crud-restapis-post-get-put-delete