Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello Readers! In this blog, we’ll learn about the fundamentals of Table Per Hierarchy.
Before reading about the Table Per Hierarchy, first, let’s understand Hibernate.
Hibernate (Open source object-relational mapping (ORM) program) can be defined as a framework for mapping object-oriented domain models to relational databases for web applications. Hibernate frees the developer from most routine data persistence-related programming work by mapping Java classes to database tables and from Java data types to SQL data types.
Table Per Hierarchy
In a hierarchy, data of each type is stored in a single table by TPH(Table per Hierarchy), and each row's type is determined by a discriminator column. Now you must be wondering.
What is TPH?
Table per hierarchy can be defined as the strategy of mapping the entire hierarchy to a single table. Only one Table is created, storing all the attributes and the entire class hierarchy.
We can map the hierarchy using this method with a single table, where the class is shown in the Table by an additional column (also known as the discriminator column).
Hibernate Table Per Hierarchy using XML File
This hierarchy consists of three classes: Employee, PartTime_Employee, and FullTime_Employee. The mapping file for this hierarchy is written below:
The Hibernate framework adds a discriminator column that indicates the record type. It primarily serves to identify the record. The discriminator subelement of the class must be specified to do this.
The class's subclass subelement defines the subclass. In this instance, the subclasses of the Employee class are PartTime_Employee and Fulltime_Employee.
The hierarchy's table structure is depicted as follows:
Example of Table per class hierarchy
In this illustration, we'll create three classes and map them to one another in the employee.hbm.xml file.
Create the Persistent Classes
The permanent classes that reflect the inheritance must be created. Let's make the three classes needed for the hierarchy above
File: Employee.java
package com.codingninjas.mypackage;
public class Employee {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
File: FullTime_Employee.java
package com.codingninjas.mypackage;
public class FullTime_Employee extends Employee {
private float pay_per_hour;
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
}
}
File: PartTime_Employee.java
package com.codingninjas.mypackage;
public class PartTime_Employee extends Employee {
private float salary;
private int bonus;
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
}
To manage data for all entity types in an inheritance hierarchy, TPH inheritance uses a single database table.
What does hibernate's @table mean?
You can define specifics about the table that will be used to store the thing in the database using the @Table annotation.
How does @DiscriminatorValue work?
For entities of the specified type, it specifies the value of the discriminator column.
What is the purpose of Hibernate?
Hibernate is a free and open-source object-relational mapping (ORM) program that offers a framework for mapping relational databases to object-oriented domain models for web applications.
Conclusion
So that's the end of the article. Table Per Hierarchy
After reading about the Table Per Hierarchy, Are you interested in reading/exploring more? Don't worry; Coding Ninjas has you covered.
However, if you want to give your work an edge over the competition, you might choose to enroll in one of our premium courses.
With our Coding Ninjas StudioGuided Path, you may learn about Data Structures & Algorithms, Competitive Programming, JavaScript, System Design, and more! If you want to put your coding skills to the test, check out the mock test series on Coding Ninjas Studio and participate in the contests! But if you've only recently started your schooling and are looking for answers to issues presented by digital titans like Amazon, Microsoft, Uber, and others. In this situation, you must consider the obstacles, interview experiences, and interview package as part of your placement preparations. If you find our blogs valuable and fascinating, please vote them up!
Good luck with your studies!
Live masterclass
Multi-Agent AI Systems: Live Workshop for 25L+ CTC at Google
by Saurav Prateek
09 Feb, 2026
03:00 PM
Beginner to GenAI Engineer Roadmap for 30L+ CTC at Amazon