Hibernate Entity Mappings

Now as you you have seen in the previous article how to create an Entity, now let’s see how to create relationships between Entities also called as Entity mappings, JPA defines four annotations for defining entities:

1. @OneToOne
2. @OneToMany
3. @ManyToOne
4. @ManyToMany

1. One-to-one relationships

The @OneToOne annotation is used to define a one-to-one relationship between two entities.

For example, you have a table EMPLOYEE and you want to store employee’s personal information(such as age, gender and grade) in another table, in that case you create another Entity and map both the entities by @OneToOne mapping, one instance of an Employee Entity class mapped to one instance of EmployeeProfile Entity.

import javax.persistence.*;

@Entity
@Table(name = "EMPLOYEE")
public class Employee {

    @Id @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "address")
    private String address;
	
    @OneToOne(mappedBy="emp")
    private EmployeeProfile profile;
	...
	
}
@Entity
public class EmployeeProfile {
   @Id
   private Integer id;
   private int age;
   private String gender;
   private String grade;
   @OneToOne
   private Employee emp;
   ...
}

The JPA provider uses EmployeeProfile’s emp field to map EmployeeProfile to Employee. The mapping is specified in the mappedBy attribute in the @OneToOne annotation.

2. One-to-many and many-to-one relationships

The @OneToMany and @ManyToOne annotations facilitate both sides of the same relationship.

Here’s an example where one Employee can have multiple Projects and a Project may have many other employees as well.
The Employee entity would define a @ManyToMany relationship with Project and the Project Entity would define a @OneToMany relationship with an Employee.

@Entity
@Table(name = "EMPLOYEE")
public class Employee {

    @Id @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

	@ManyToOne
    @JoinColumn(name="PROJECT_ID")
    private Project project;
	...
}
@Entity
public class Project {
   @Id
   private Integer projectId;
   private String projectDescription;
   private String projectDuration;
   @OneToMany(mappedBy = "project")
   private List<Employee> emp = new ArrayList<>();
   ...
}

3. many-to-many relationships

Here’s a case where an Employee entity has many projects.

@Entity
@Table(name = "EMPLOYEE")
public class Employee {

    @Id @GeneratedValue
    @Column(name = "EMP_ID")
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

	@ManyToMany
    @JoinTable(name="EMP_PROJECTS",
    		   joinColumns=@JoinColumn(name="EMP_ID"),
    		   inverseJoinColumns=@JoinColumn(name="PROJECT_ID"))
    private Set<Project> projects = new HashSet<>();
	...
}
@Entity
public class Project {
   @Id
   private Integer projectId;
   private String projectDescription;
   private String projectDuration;
  
   @ManyToMany(mappedBy = "projects")
   private Set<Employee> projects = new HashSet<>();
   ...
}

In this example, we create a new table, EMP_PROJECTS, with two columns: EMP_ID and PROJECT_ID. Using the joinColumns and inverseJoinColumns attributes tells your JPA framework how to map these classes in a many-to-many relationship. The @ManyToMany annotation in the Employee class references the field in the Project class that manages the relationship; namely the projects property.

Summary

In this article we looked into the relational mapping between tables or entities in a JPA ORM way.
Hope you liked it !