Spring boot Series-2 One to One and One to Many Mapping

Mohamed Razan
May 20 · 2 min read

This is my 2nd article on Spring boot series. If you haven’t read the 1st article, click https://mhdrazan7.medium.com/spring-boot-series-1-project-setup-and-crud-operations-eb297d39ba22

In this article, I’ll show you how to implement one-to-one and one-to-many mapping using hibernate in Spring boot.

One to One Mapping

one to one relationship between user and profile

User Entity

package com.series.springboot.model;import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long uid;
private String email;
private String password;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "profile_id", referencedColumnName = "pid")
private Profile profile;
// Standard constructors, getters and setters}

Profile Entity

package com.series.springboot.model;import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table
public class Profile {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long pid;
private String firstName;
private String lastName;
private String contactNo;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "profile")
private User user;
// Standard constructors, getters and setters}

One to Many Mapping

one to many relationship between customer and product

Customer Entity

package com.series.springboot.model;import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long cid;
private String name;
private String email;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
private Set<Product> products;
// Standard constructors, getters and setters}

Product Entity

package com.series.springboot.model;import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
mport javax.persistence.Table;
@Entity
@Table
public class Product {
@Id
@GeneratedValue
private long pid;
private String type;
private double price;
@ManyToOne(cascade = CascadeType.ALL)
private Customer customer;
// Standard constructors, getters and setters}

I hope this article is useful for you. In the next article, I’ll show you how to implement Many to Many mapping in Spring boot.

Javarevisited

Medium’s largest Java publication, followed by 12700+ programmers. Follow to join our community.