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.



Similar Reads

Spring Boot - Difference Between @Service Annotation and @Repository Annotation
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. @Service Annotation In an application, the business logic resides
7 min read
Hibernate - Table Per Subclass using Annotation
Hibernate is an open-source, non-invasive, lightweight java ORM(Object-relational mapping) framework that is used to develop persistence logic that is independent of Database software. An ORM(Object-relational mapping) framework simplifies data creation, data manipulation, and data access. It is a programming technique that maps the object to the d
6 min read
Hibernate - Table Per Hierarchy using Annotation
Hibernate is a framework that provides some abstraction layer, meaning that the programmer does not have to worry about the implementations, it does the implementations for you internally like writing queries to perform CRUD operations, establishing a connection with the database, etc. It is an open-source, non-invasive, lightweight java ORM(Object
8 min read
Hibernate - Table Per Concrete Class Using Annotation
Hibernate is a framework that provides some abstraction layer, meaning that the programmer does not have to worry about the implementations, it does the implementations for you internally like writing queries to perform CRUD operations, establishing a connection with the database, etc. It is an open-source, non-invasive, lightweight java ORM(Object
7 min read
Hibernate - @Embeddable and @Embedded Annotation
The @Embeddable and @Embedded annotations in Hibernate are used to map an object’s properties to columns in a database table. These annotations are used in combination to allow the properties of one class to be included as a value type in another class and then be persisted in the database as part of the containing class. OverviewThe @Embeddable an
4 min read
Hibernate - @Version Annotation with Example
@Version annotation is used to specify the version number for a specific entity. Version Number provided using @Version annotation is used to prevent concurrent modification to an entity. When an entity is being updated, the version number is also incremented. If another transaction tries to update the same entity with the older version number, an
3 min read
Hibernate - @Transient Annotation with Example
@Transient annotation in Hibernate is used to mark a property or field in an entity class as transient. This means that the field or property marked as transient should be excluded when the data persists in the database. Wherever an entity is mapped to a database table, by default all the non-transient fields and properties are persisted. There are
4 min read
Hibernate - @OrderBy Annotation with Example
@OrderBy annotation is used in Hibernate to specify the ordering of the elements in the collection valued property of an entity class. It is used to define the order of the elements which should be displayed. We can order the data within the entity using a specific parameter in ascending or descending order. Examples of @OrderBy AnnotationExample 1
4 min read
Hibernate - @GeneratedValue Annotation in JPA
@GeneratedValue annotation, the name itself suggests that it will generate something. This annotation is generally used in conjunction with @Id annotation to automatically generate unique values for primary key columns within our database tables. When creating an entity class we have to specify a primary key for that entity. For marking the field p
3 min read
Hibernate - @OneToOne Annotation
@OnetoOne annotation in Hibernate is used to create a one-to-one association between two entities. The one-to-one annotation indicates that one instance of an entity is associated with only one instance of the other entity. When we annotate a field or method with @Onetoone annotation then Hibernate will create the one-to-one relation between these
4 min read
Practice Tags :