Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Tutorial
  • Java Spring
  • Spring Interview Questions
  • Java SpringBoot
  • Spring Boot Interview Questions
  • Spring MVC
  • Spring MVC Interview Questions
  • Java Hibernate
  • Hibernate Interview Questions
  • Advance Java Projects
  • Java Interview Questions
Open In App
Next Article:
Hibernate - Difference Between @JoinColumn and @PrimaryKeyJoinColumn
Next article icon

Hibernate - Difference Between @JoinColumn and @PrimaryKeyJoinColumn

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
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.


Next Article
Hibernate - Difference Between @JoinColumn and @PrimaryKeyJoinColumn

C

chaitanyamunje
Improve
Article Tags :
  • Java
  • Advance Java
  • Java-Hibernate
  • Hibernate- Annotations
Practice Tags :
  • Java

Similar Reads

    Difference between JDBC and Hibernate in Java
    Java is one of the most powerful and popular server-side languages in the current scenario. One of the main features of a server-side language is the ability to communicate with the databases. In this article, let's understand the difference between two ways of connecting to the database (i.e.) JDBC
    2 min read
    Difference Between “INNER JOIN” and “OUTER JOIN”
    Are you working with SQL and struggling to understand the differences between INNER JOIN and OUTER JOIN? These two types of joins are among the most commonly used tools for combining tables in SQL queries. Whether you're analyzing student data or managing customer records, knowing when to use INNER
    5 min read
    Hibernate - @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 anoth
    4 min read
    Hibernate - Difference Between List and Bag Mapping
    Hibernate supports both List and Bag Mapping and Set Mapping too. Hence there will be a tradeoff, regarding which is the best data type to use. The choice will be purely based on requirements but still, let us see the differences between them. Whenever there is a one-to-many relationship or many-to-
    3 min read
    Difference between JOIN and UNION in SQL
    Pre-requisites: JOIN, UNION JOIN in SQL is used to combine data from many tables based on a matched condition between them. The data combined using the JOIN statement results in new columns. Consider the two tables: Boys Girls Example: sql> SELECT Boys.Name, Boys.Age, Girls.Address, FROM Boys INN
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences