@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.
Examples for @PrimaryKeyJoinColumn 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 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; } |
Code Explanation:
In the above example, we are creating two entities for Student and Education details. These entities are having 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.
Example 2:
Java
// on the below line creating an entity for employee @Entity public class Employee { // on the below line creating an id for 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 // address details adding one to one annotation to map // employee with address. on the below line we are // annotating address with primary key join column. @OneToOne @PrimaryKeyJoinColumn private Address address; } // on the below line creating an entity for address details // of employee. @Entity public class Address { // on the below line creating an id for employee address // details. @Id @GeneratedValue private long id; // on the below line creating a field for flat number. private int flatNumber; // on the below line craeating a field for street. private String street; // on the below line creating a field for city. private String city; // on the below line creating a field for state private String state; // on the below line creating a field for pincode private int pincode; } |
Code Explanation:
In the above example, we are creating two entities for an Employee and Address. These entities are having a one-to-one relationship with each other. The employee entity has a primary key column as id which is annotated with @ID and a field for the address which we are annotating with @PrimaryKeyJoinColumn. This annotation indicates that the address field will be used as a primary key for the Address entity as the foreign key. The Address entity has its own primary key as an id and also contains other fields such as street, city, state, and PIN code. By using the @PrimaryKeyJoinColumn annotation, Hibernate will generate the foreign key constraint in the database to establish one to one relationship. It will ensure that the primary key value of the Address entity matches the foreign key value in the Employee entity.