Open In App

When to Use @JoinColumn Annotation in Hibernate?

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

The @JoinColumn annotation in Hibernate is used to specify the mapping of a foreign key column in a relationship between two entities. The @JoinColumn annotation is applied on the owning side of the association to define the foreign key column name and other attributes which are related to the join column. 

Examples for @JoinColumn Annotation 

Example 1:

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
      private long id;
    // on the below line creating a field for employee name.
    private String employeeName;
    // on the below line creating a field for employee
    // department details adding many to one annotation to
    // map employees with the Department. on the below line
    // we are annotating the department with a join column
    // and many to one.
    @ManyToOne
    @JoinColumn(name = "department_id")
    private Department department;
}
// on the below line creating an entity for the Department
// of employee.
@Entity
public class Department {
    // on the below line creating an id for the employee
    // Department.
    @Id 
      @GeneratedValue
      private long id;
    // on the below line creating a field for department
    // name.
    private int name;
}


Code Explanation:

In the above example, we are creating two entities for Employee and Department. The employee entity consists of different fields such as id for employee id, name, and department which we are annotating it with many-to-one and join columns. The many-to-one indicates that multiple employees can be associated with a single department. The Employee entity has a foreign key column that maps to the primary key of the Department entity. This mapping is obtained by using @ManyToOne and @JoinColumn annotations. With the help of @JoinColumnn annotation, Hibernate will generate the foreign key constraint in the database to establish the relationship between Employee and Department entity. 

Example 2:

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
      private long id;
    // on the below line creating a field for student name.
    private String studentName;
    // on the below line creating a field for section in
    // which student is admitted. adding many to one
    // annotation to map students with Section. on the below
    // line we are annotating section with join column and
    // many to one.
    @ManyToOne
    @JoinColumn(name = "section_id")
    private Section section;
}
// on the below line creating an entity for Section of
// students.
@Entity
public class Section {
    // on the below line creating an id for section.
    @Id 
      @GeneratedValue
      private long id;
    // on the below line creating a field for section name.
    private int name;
}


Code Explanation:

In the above example, we are creating two entities for Student and Section. The student entity consists of different fields such as id for the student if, name, and a section which we are annotating with many-to-one and join columns. The many-to-one indicates that multiple students can be associated within a section. The student entity is having a foreign key column that maps to the primary key of the Section entity. This mapping is obtained by using @ManyToOne and @JoinColumn annotations. With the help of @JoinColumn annotation, Hibernate will generate the foreign key constraint in the database to establish the relationship between the Student and Section entity.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads