Table of contents
1.
Introduction
2.
Inheritance Mapping
3.
Table Per Subclass 
4.
Example of Table Per Subclass(TPS)
4.1.
Create Persistent Classes
4.1.1.
User.java
4.1.2.
Student.java
4.1.3.
Teacher.java
4.2.
Add Dependencies on pom.xml File
4.3.
Add Mapping in Configuration(hibernate.cfg.xml) File
4.4.
Finally, Create the Class Storing the Persistent Object
4.5.
Output
5.
Frequently Asked Questions
5.1.
What is Hibernate?
5.2.
What is Inheritance Mapping?
5.3.
What are the different types of inheritance mapping strategies in Hibernate?
5.4.
How many layers are there in Hibernate Architecture?
6.
Conclusion
Last Updated: Aug 13, 2025
Medium

TPS Using Annotations | Hibernate

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello and welcome, readers! We hope you are doing well.
Today, in this article, we will discuss the TPS(Table Per Subclass) Using the Annotation, one of the inheritance mapping strategies in Hibernate. So follow the article till the end to grasp the topic completely.

So, without further ado, let’s start our discussion.

Before discussing the TPS using the annotation, you must first learn about inheritance mapping.

Inheritance Mapping

Hibernate is an open-source, lightweight Java framework that simplifies the development of the Java application to interact with the database. It is also an ORM(Object Relational Model) tool that implements the specification of JPA(Java Persistent API) for data persistence.

In Hibernate, we can map the inheritance hierarchy classes with the table of the database, known as inheritance mapping. 

There are mainly three inheritance mapping strategies in Hibernate. These are as follows:

  • Table Per Hierarchy
  • Table Per Concrete Class
  • Table Per Subclass

 

Table Per Subclass 

In this type of inheritance mapping strategy, tables are created as per the persistent classes, but they are treated using the primary and foreign keys. So there will not be any duplicate column in the relation.

Here, In the parent class, we need to use the @Inheritance(strategy = InheritanceType.JOINED) annotation, and for the subclasses, we need to use the @PrimaryKeyJoinColumn annotation. 

Example of Table Per Subclass(TPS)

In this example, we will be creating the superclass User and its subclasses, namely Students and Teachers and showing a mapping of these classes step by step.

Create Persistent Classes

First, we will create the three persistent classes, UserStudents and Teachers, representing the inheritance.

User.java

package com.codingninjas.MavenProject;

import javax.persistence.*;

@Entity
@Table(name = "users")
@Inheritance(strategy = InheritanceType.JOINED)

public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)

  @Column(name = "user_id")
  private int user_id;

  @Column(name = "user_name")
  private String user_name;

  public void setUserName(String name) {
    user_name = name;
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Student.java

package com.codingninjas.MavenProject;

import javax.persistence.*;

@Entity
@Table(name = "students")
@PrimaryKeyJoinColumn(name = "user_id")
public class Student extends User {
  @Column(name = "course")
  private String course;

  @Column(name = "dept")
  private String dept;

  public void setCourse(String course) {
    this.course = course;
  }
  public void setDept(String dept) {
    this.dept = dept;
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Teacher.java

package com.codingninjas.MavenProject;

import javax.persistence.*;

@Entity
@Table(name = "teachers")
@PrimaryKeyJoinColumn(name = "user_id")
public class Teacher extends User {
  @Column(name = "subject")
  private String subject;

  @Column(name = "salary")
  private int salary;

  public void setSubject(String subject) {
    this.subject = subject;
  }
  public void setSalary(int salary) {
    this.salary = salary;
  }
}
You can also try this code with Online Java Compiler
Run Code

 

The hierarchy of the above persistent classes is shown below:

Hierarchy of persistent classes in the example

Add Dependencies on pom.xml File

Add the below dependencies in your pom.xml file, version may be different for your case, for this project we have used the below versions.

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.6.9.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>

 

Add Mapping in Configuration(hibernate.cfg.xml) File

Now, In your configuration file(hibernate.cfg.xml), you just need to add the following mapping,

<mapping class="com.codingninjas.MavenProject.User" />
<mapping class="com.codingninjas.MavenProject.Student" />
<mapping class="com.codingninjas.MavenProject.Teacher" />

 

After, adding the mapping the hibernate.cfg.xml would look like the below. Remember in order to connect to your own database, you need to set the properties accordingly inside the hibernate.cfg.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hiberdb</property>
        <property name="connection.username">root</property>
        <property name="connection.password">***Enter_your_mysql_password***</property>
        <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
        <mapping class="com.codingninjas.MavenProject.User" />
        <mapping class="com.codingninjas.MavenProject.Student" />
        <mapping class="com.codingninjas.MavenProject.Teacher" />
    </session-factory>
</hibernate-configuration>

 

Finally, Create the Class Storing the Persistent Object

Here, we are just creating objects of the persistent classes. We are storing these object values in the database.

package com.codingninjas.MavenProject;

import org.hibernate.Transaction;

import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.cfg.Configuration;

public class App 
{
    public static void main( String[] args )
    {
         
         Configuration cfg = new Configuration();
         cfg.configure("hibernate.cfg.xml");
           
         SessionFactory factory = cfg.buildSessionFactory(); 
           
         User user=new User();  
         user.setUserName("Ninja Sharma");  
           
         Student student=new Student();
         student.setUserName("Ninja2 Jain");
         student.setCourse("B.Tech");
         student.setDept("Information Technology");
           
         Teacher teacher=new Teacher();
         teacher.setUserName("Ninja3 Gupta");
         teacher.setSubject("Java");  
         teacher.setSalary(50000); 
         
         
         Session session = factory.openSession();
         
         Transaction tx = session.beginTransaction();
           
         session.save(user);  
         session.save(student);  
         session.save(teacher); 
         tx.commit();
           
         session.close();      
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Output

Frequently Asked Questions

What is Hibernate?

Hibernate is an open-source, lightweight Java framework that simplifies the development of the Java application to interact with the database.

What is Inheritance Mapping?

In Hibernate, the inheritance mapping is the mapping between the inheritance hierarchy classes with the database table.

What are the different types of inheritance mapping strategies in Hibernate?

There are three inheritance mapping strategies in hibernate, and they are as follows:

  • Table Per Hierarchy
  • Table Per Concrete Class
  • Table Per Subclass

How many layers are there in Hibernate Architecture?

In Hibernate architecture, there are four layers.

Conclusion

We started with the basic introduction. Then we discussed the followings:

  • What is inheritance mapping
  • What is Table Per Subclass Strategy in Inheritance Mapping
  • Then, we discussed one example.
     

We hope this blog gives you some ideas regarding TPS(Table Per Subclass) Using the Annotation. To learn more, follow our articles on Your Guide To the ORM, Java Persistent API. Explore our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more.!
 

Do upvote this blog to help other ninjas grow.

Thank you


Happy Reading!

Live masterclass