Open In App

Hibernate – @MapsId Annotation

Last Updated : 21 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

@MapsId annotation in Hibernate is used to obtain a one-to-one relationship between two entities by mapping the primary key of one entity to the foreign key of another entity.  This annotation is used when we have to use a shared primary key between two entities. 

Examples for @MapsId Annotation

Example 1:

Java




// on the below line creating an entity for Student
@Entity
public class Student {
    // on the below line creating an id for student which is
    // generated value.
    @Id
    @GeneratedValue
    @Column(name = "student_id")
    private long studentId;
    // on the below line creating a field for the student
    // name.
    private String studentName;
}
// on the below line creating an entity for the Section of
// students.
@Entity
public class Education {
    // on the below line creating an id for the section.
    @Id
      @GeneratedValue
      private long id;
    // on the below line creating a field for ssc
    // percentage.
    private int sscPercentage;
    // on the below line creating a field for hssc
    // percentage.
    private int hsscPercentage;
  
    // on the below line creating a field for students to
    // which percentages are mapped.
    @MapsId("studentId")
    @JoinColumn(name = "student_id")
    private Student student;
}


Code Explanation:

In the above example, we are creating two entities. One entity for Students consists of several fields such as student id and student name. Student id is annotated with @Id which is a unique id and @GeneratedValue to generate it automatically. Similarly, we are creating one more entity for Education in which we are creating several fields such as id, sscPercentage, hsscPercentage, and a field for students to which we have to map these marks. For the student field, we are annotating it with @MapsId and passing the id for the student which we have created for the student entity. By using the MapsId annotation we are mapping a primary key of a student entity to the foreign key for the education entity. 

Example 2:

Java




// on the below line creating an entity for Employee
@Entity
public class Employee {
    // on the below line creating an id for an employee
    // which is generated value.
    @Id
    @GeneratedValue
    @Column(name = "employee_id")
    private long empId;
    // on the below line creating a field for employee name.
    private String employeeName;
}
// on the below line creating an entity for Address of
// employee.
@Entity
public class Address {
    // on the below line creating an id for address.
    @Id
      @GeneratedValue 
      private long id;
    // on the below line creating a field for the street.
    private String street;
    // on the below line creating a field for the city.
    private String city;
    // on the below line creating a field for state.
    private String state;
    // on the below line creating a field for pin code.
    private int pincode;
  
    // on the below line creating a field for students to
    // which percentages are mapped.
    @MapsId("empId")
    @JoinColumn(name = "employee_id")
    private Employee employee;
}


Code Explanation:

In the above example, we are creating two entities. One entity for the Employee which consist of several fields such as employee ID and employee name. We are annotating employee id with @Id and @GaneratedValue to generate it automatically. Similarly we are creating one more entity for Address in which we are creating fields for id, street, city, state, PIN code and employee. We are annotating employee entity with @MapsId and passing the id for the employee which we have created in employee entity. By using MapsId annotation we are mapping primary key of Employee entity to the foreign key for the Address entity.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads