Open In App

Hibernate – Difference Between @JoinColumn and @PrimaryKeyJoinColumn

Last Updated : 31 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

@JoinColumn annotation in JPA is used to specify the column which is responsible for storing the foreign key value that established the relationship between two entities. It is used when defining associations such as @OneToOne, @OneToMany, and @ManyToOne associations.

Example of @JoinColumn Annotation:

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 variable for student
    // name.
    private String name;
  
    // on the below line we are adding many to one
    // annotation.
    @ManyToOne
    // on the below line we are adding a join column
    // annotation and specifying name as cours_id which
    // specifies the foreign key column.
    @JoinColumn(name = "course_id")
    // on the below line creating a variable for course.
    private Course course;
}
  
// on the below line creating an entity class for course.
@Entity
public class Course {
    // on the below line creating id for course which is a
    // generated value.
    @Id 
      @GeneratedValue
      private Long id;
    // on the below line creating a variable to get the
    // course name.
    private String name;
    // on the below line adding one to many annotation which
    // is used to map one course to multiple students.
    @OneToMany(mappedBy = "course")
    // on the below line creating an array of student which
    // are enrolled in specific course.
    private List<Student> students;
}


Explanation:

In the above code, we are first creating an entity for Student which has different variables such as id, name variable for student name, and Course variable for the course which is being enrolled by the student. We are annotating the course with @JoinColumn annotation having a key as column_id which specifies the foreign key column. Then we are creating an entity for Course in which we are creating variables for id, name for course name, and array list of students which we are annotating with @OneToMany. @OnetoMany annotation is used to map one course with multiple students.

What is @PrimaryKeyJoinColumn Annotation?

@PrimaryKeyJoinColumn in Hibernate is used to specify the primary key of the associated entity as the foreign key of the current entity. This annotation is used to establish a one-to-one relationship between the two entities, where the primary key of one entity is considered the foreign key in another entity. 

Example of @PrimaryKeyJoinColumn annotation:

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 name;
    // on the below line creating a field for student
    // education details adding one to one annotation to map
    // student with education. adding primary key join
    // column annotation to indicate education as a primary
    // key for current entity and foriegn key for education
    // entity
    @OneToOne
    @PrimaryKeyJoinColumn
    private Education education;
}
// on the below line creating an entity for education
// details of student.
@Entity
public class Education {
    // on the below line creating an id for student
    // education details.
    @Id
      @GeneratedValue 
      private long id;
    // on the below line creating a field for ssc
    // percentage.
    private int sscPercentage;
    // on the below line craeating a field for hssc
    // percentage.
    private int hscPercentage;
}


Explanation:

In the above example, we are creating two entities for Student and Education details. These entities have a one-to-one relationship with each other. The Student entity has a primary key column as id which is annotated with @ID and a field for education which is annotated with @PrimaryKeyJoinColumn. This annotation indicates that the education field will be used as a primary key of the Education entity as the foreign key. The Education entity has its own primary key as id and also contains other fields such as hoc percentage, and sec percentages. By using the @PrimaryKeyJoinColumn annotation, Hibernate will automatically generate the foreign key constraint in the database to establish the one-to-one relationship. It will ensure that the primary key value of the Education entity matches the foreign key value in the Student entity.

Difference Between @JoinColumn and @PrimaryKeyJoinColumn

@JoinColumn

@PrimaryKeyJoinColumn

@JoinColumn annotation is used for various types of associations.

@PrimaryKeyJoinColumn annotation is used when the primary key is also a foreign key.

@JoinColumn annotation requires an additional column in the table.

@PrimaryKeyJoinColumn eliminates the need for an additional foreign key column.

@Join Column annotation does not modify the primary key of the owning entity.

@PrimaryKeyJoinColumn uses the primary key of the associated entity.

@JoinColumn provides us with more flexibility by providing a separate column.

@PrimaryKeyJoinColumn offers limited flexibility as it relies on the primary key.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads