Hibernate – Difference Between @JoinColumn and @PrimaryKeyJoinColumn
Last Updated :
31 Jul, 2023
@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
@Entity
public class Student {
@Id
@GeneratedValue
private long id;
private String name;
@ManyToOne
@JoinColumn (name = "course_id" )
private Course course;
}
@Entity
public class Course {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany (mappedBy = "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
@Entity
public class Student {
@Id
@GeneratedValue
private long id;
private String name;
@OneToOne
@PrimaryKeyJoinColumn
private Education education;
}
@Entity
public class Education {
@Id
@GeneratedValue
private long id;
private int sscPercentage;
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 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.
|
Share your thoughts in the comments
Please Login to comment...