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 Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Hibernate - @Inheritance Annotation
Next article icon

Hibernate - @Inheritance Annotation

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The @Inheritance annotation in JPA is used to specify the inheritance relation between two entities. It is used to define how the data of the entities in the hierarchy should be stored in the database. The @Inheritance  annotation provides us with benefits to reduce code complexity by creating a base class and inheriting other classes from the base class. The @Inheritance annotation is applied to the root entity class to define the inheritance strategy. There are different types of strategies available for Inheritance annotation which are as follows: 

  1. Single Table Inheritance Strategy. 
  2. Joined Inheritance Strategy. 
  3. Table Per Class Inheritance Strategy. 

If no strategy is defined while using @inheritance annotation the default Single Table Inheritance Strategy is applied. 

1. Single Table Inheritance Strategy

The Single Table Inheritance strategy states that all the entities in this hierarchy are mapped to a single table in the database. Separate columns are used to differentiate between multiple entities. This strategy is suitable when entities in the hierarchy share most of their attributes and have a small number of different fields. 

Example for Single Table Inheritance Strategy:

Java
// on the below line creating an entity for Student @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "entity_type",                      discriminatorType                      = DiscriminatorType.STRING) public class Student {     // on the below line creating a field for id which we     // are annotating with @Id.     @Id        private int id;     // on the below line creating a field for the student     // name.     private String studentName; } // on the below line creating a separate entity for boys in // the group of students. @Entity // on the below line specifying the discriminator value as // boy. @DiscriminatorValue("BOY") // on the below line creating a class for boy and extending // it with Student public class Boy extends Student {     // Define Boys-specific properties in this class. } @Entity // on the below line specifying the discriminator value as // girl. @DiscriminatorValue("GIRL") // on the below line creating a class for girl and extending // it with Student public class Girl extends Student {     // Define Girls-specific properties in this class. } 

Code Explanation:

In the above example, we are creating a Base class for Students which will serve for both boys as well as girls. Inside this class we are specifying the common properties such as id, name. We are adding an inheritance annotation for the Student base class as InheritanceType.SINGLE_TABLE which indicates the Single Table inheritance strategy. The discriminator column entity_type is used to differentiate between entities and it is specified with @DiscriminatorColumn annotation. The discriminator values for each entity are set using @DiscriminatorValue annotation. 

2. Joined Inheritance Strategy

The Joined Inheritance Strategy generates a separate table for each entity class. The attribute for each table is joined with the primary key. It removes the possibility of duplicity. Each table that is being created contains specific columns and a join operation is used to retrieve data from multiple tables when querying the entire hierarchy. We can use the strategy when the entities we are creating have differences in their properties and relationships. 

Example forJoined Inheritance Strategy: 

Java
// on the below line creating an entity for Student @Entity // on the below line we are specifying the inheritance // strategy as joined. @Inheritance(strategy = InheritanceType.JOINED) public class Student {     // on the below line creating a field for id which we     // are anotating it with @Id.     @Id        private int id;     // on the below line creating a field for student name.     private String studentName; } // on the below line creating a seperate entity for boys in // the group of students. @Entity // on the below line specifying the table name for boys. @Table(name = "boys") // on the below line creating a class for boy and extending // it with Student public class Boy extends Student {     // Define Boys-specific properties in this class. } @Entity // on the below line specifying the table name for girl. @Table(name = "girls") // on the below line creating a class for girl and extending // it with Student public class Girl extends Student {     // Define Girls-specific properties in this class. } 

Code Explanation:

In the above example, we are creating three entities for Student, Boys, and Girls. Each entity that is being created has its own table in the database. For boys entities a new table will be created as boys. Similarly, for Girl entities, a new table will be created for girls. The @Inheritance annotation is set to InheritanceType.JOINED which indicates that the Joined Inheritance Strategy. We are creating a base class as Student which is a base entity and contains some common fields for both boys as well as girls such as name and id.

3. Table Per Class Inheritance Strategy

In the table per class inheritance strategy, a separate table is generated for each subclass. Each entity is mapped to its own table in the database, unlike joined strategy no separate table is generated for the parent entity class in the table per class strategy. This strategy provides us with the highest level of flexibility but this will result in redundant columns and more complex database schema. 

Example for Table Per Class Inheritance Strategy: 

Java
// on the below line creating an entity for Student @Entity // on the below line we are specifying the inheritance // strategy as tabler per class. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Student {     // on the below line creating a field for id which we     // are annotating with @Id.     @Id       private int id;     // on the below line creating a field for the student     // name.     private String studentName; } // on the below line creating a separate entity for boys in // the group of students. @Entity // on the below line creating a class for boy and extending // it with Student public class Boy extends Student {     // Define Boys-specific properties in this class. } // on the below line creating an entity for girl in the // group of students. @Entity // on the below line creating a class for girl and extending // it with Student public class Girl extends Student {     // Define Girls-specific properties in this class. } 

Code Explanation:

In the above example, each entity that we are creating from the Student entity such as Boy and Girl entity has its own table present in the database. The @Inheritance annotation is set to InheritanceType.TABLE_PER_CLASS indicates the Table Per class inheritance strategy. The Base class student contains some common fields in it such as an id for the student and a field for the Student name.


Next Article
Hibernate - @Inheritance Annotation

C

chaitanyamunje
Improve
Article Tags :
  • Java
  • Java-Hibernate
  • Java-Spring-Data-JPA
  • Hibernate- Annotations
Practice Tags :
  • Java

Similar Reads

    Hibernate - @OneToOne Annotation
    @OnetoOne annotation in Hibernate is used to create a one-to-one association between two entities. The one-to-one annotation indicates that one instance of an entity is associated with only one instance of the other entity. When we annotate a field or method with @Onetoone annotation then Hibernate
    4 min read
    Hibernate - @OneToMany Annotation
    @OneToMany annotation in Hibernate is used to obtain one-to-many relationships between two entities. It is used to map a collection-valued association where a single instance of an entity is mapped to multiple instances of another entity. Examples of @OneToMany AnnotationExample 1: Java // on the be
    4 min read
    Hibernate - @ManyToOne Annotation
    @ManytoOne annotation in Hibernate is used to create a many-to-one relationship between two entities. The @ManyToOne annotation indicates that the many instances of one entity can be associated with only one instance of another entity. When we annotate a field of the method with @ManyToOne annotatio
    4 min read
    Hibernate - @ManyToMany Annotation
    @ManyToMany annotation in Hibernate is used to obtain many-to-many relationships between two entities. It allows us to create a bidirectional relationship between two entities where each entity can be associated with another entity through multiple instances. Examples of @ManyToMany Annotation Examp
    4 min read
    Hibernate - Annotations
    Annotation in JAVA is used to represent supplemental information. As you have seen @override, @inherited, etc are an example of annotations in general Java language. For deep dive please refer to Annotations in Java. In this article, we will discuss annotations referred to hibernate. So, the motive
    7 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