Last updated on April 2nd, 2024
As we are creating the Spring Boot project, we may have used JPA and Hibernate when we are working with Relational databases such as Mysql/Oracle/PostgreSQL/etc. Hibernate is an ORM(Object Relational Mapping) framework for Java programming language. It maps the Java Bean class to the database table. In simple words, this framework allows us to convert from Java objects to tables. The Hibernate ORM framework is the default implementation of the JPA specification in the Spring Boot. When we are creating JPA entity classes in our Spring Boot Project then we need to make relationships between these entity classes. This topic teaches us about different types of JPA Entity mappings used when establishing relationships between one table and another.
Different types of JPA Entity Mappings
These are the following as given below:
• OneToOne JPA Mapping
• OneToMany JPA Mapping
• ManyToOne JPA Mapping
• ManyToMany JPA Mapping
OneToOne JPA Mapping
In One-To-One mapping, one item can belong to only one other item of the entities. It means each row of one entity is associated with one and only one row of another entity.
For this mapping, we can use two directionals(Unidirectional and Bidirectional) mapping in JPA. Let us consider an example, Employee and Address relationship in one-to-one mapping. An employee lives at one address, employee entity has occupied only one address entity. To implement this kind of mapping we need to use @OneToOne annotation in the JPA entity class.
How to use @OneToOne mapping annotation?
We can use @OneToOne mapping annotation in the JPA entity class to make the relation between two tables. We are creating an example to use this annotation in unidirectional mapping in JPA entities.
Employee.java
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 lombok.Data;
@Entity
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String emailId;
private String mobNo;
private String design;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "addrs_id", referencedColumnName = "id")
private Address address;
}
→ This @OneToOne annotation is used to make the one-to-one unidirectional mapping with the Address JPA entity class.
Address.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Entity
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String street;
private String city;
private String state;
}
→In these two classes, we used Lombok annotation(@Data).
OneToMany JPA Mapping
One-to-many mapping is a type of collection-valued association where an entity is associated with a collection of other entity entities. It means that this is a type of association instance of an entity that can be mapped into any number of instances of the other entities in JPA. It is unidirectional mapping but we can use bidirectional mapping with @ManyToOne annotation. Let us consider an example of this mapping Post entity association with the collection of Comment entity. Simply, a post can have multiple comments. To implement this kind of mapping we need to use @OneToMany annotation in the JPA entity class.
How to use @OneToMany mapping annotation?
We can use @OneToMany mapping annotation in the JPA entity class to make the relation between two tables. We are creating an example to use this annotation in unidirectional mapping in JPA entities.
Post.java
package com.springjava.entity;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import lombok.Data;
@Data
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
private String desc;
@OneToMany
@JoinColumn(name = "post_id")
private List<Comment> commentList;
}
→ This @OneToMany annotation is used to make one-to-many unidirectional mapping with the Comment JPA entity class.
Comment.java
package com.springjava.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Data
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String msg;
}
ManyToOne JPA Mapping
Many-to-one mapping is a type of single-valued association where the collection of entities is associated with a similar entity. It means that in the database more than one row is associated with similar rows of another entity. It is unidirectional mapping but we can use bidirectional mapping with @OneToMany mapping annotation. Let us consider an example of this mapping Comment entity class associated with Post entity class. In a simple way, multiple comments on a post. To implement this kind of mapping we need to use @ManyToOne annotation in the JPA entity class.
How to use @ManyToOne mapping annotation?
We can use @ManyToOne mapping annotation in the JPA entity class to make the relation between two tables. We are creating an example to use this annotation in unidirectional mapping in JPA entities.
Post.java
package com.springjava.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Data
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
private String desc;
}
Comment.java
package com.springjava.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import lombok.Data;
@Data
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String msg;
@ManyToOne
@JoinColumn(name = "post_id")
Post post;
}
→This @ManyToOne annotation is used to make many-to-one unidirectional mapping with the Post JPA entity class.
ManyToMany JPA Mapping
In Many-To-Many mapping where one or more rows from one entity are associated with more than one row in another entity in JPA. We can implement unidirectional and bidirectional mapping. Let us consider an example of this mapping Student entity association with the Subject entity. A student can select multiple subjects and a subject can have multiple students selected for it. To implement this mapping we need to use @ManyToMany annotation in the JPA entity class.
How to use @ManyToMany mapping annotation?
We can use @ManyToMany mapping annotation in the JPA entity class to make the relation between two tables. We are creating an example to use this annotation in unidirectional mapping in JPA entities.
Student.java
package com.springjava.entity;
import java.util.List;
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.JoinTable;
import javax.persistence.ManyToMany;
import lombok.Data;
@Data
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String email;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "student_subject",
joinColumns = { @JoinColumn(name = "stu_id") },
inverseJoinColumns = { @JoinColumn(name = "sub_id") })
private List<Subject> subList;
}
→ This @ManyToMany annotation is used to make many-to-many unidirectional mapping with the Subject JPA entity class.
Subject.java
package com.springjava.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Data
@Entity
public class Subject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
}
To know more about how to implement these mapping in Spring Boot then you can refer to our articles by clicking here.
Conclusion
In this topic, we learned about different types of JPA entity mappings and mapping annotations.