Table of contents
1.
Introduction
2.
Inheritance Mapping
3.
Table Per Concrete Class
4.
Example of Table Per Concrete Class(TPC)
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 the Table Per Concrete class in Hibernate?
5.2.
How many layers are there in Hibernate architecture?
5.3.
What is the difference between session and season factory?
5.4.
What is the difference between getCurrentSession() and openSession()?
6.
Conclusion
Last Updated: Aug 13, 2025
Medium

TPC Using Annotation | 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.

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

Before discussing the TPC using the annotation, let’s briefly explain the Inheritance Mapping.

Inheritance Mapping

Hibernate is an open-source, lightweight framework in Java 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.
Today, in this article, we will be discussing the TPC(Table Per Concrete class) Using the Annotation, one of the inheritance mapping strategies in Hibernate. So follow the article till the end to grasp the topic completely.

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 Concrete Class

In this type of inheritance mapping strategy, tables are created per class. So the table does not contain any nullable values. The main disadvantage of this approach is that duplicate column are created in the subclass tables. 

Here, In the parent class, we need to use the @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) annotation. It specifies the table per class strategy that we are using. And in the subclasses, we need to use the @AttributeOverrides annotation. It defines that the parent class attribute will be overridden in this class. 

In the table structure, the parent class table columns will also be added to the subclass table.

Example of Table Per Concrete Class(TPC)

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.TABLE_PER_CLASS)

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")
@AttributeOverrides({
        @AttributeOverride(name = "user_id", column = @Column(name = "user_id")),
        @AttributeOverride(name = "user_name", column = @Column(name = "user_name"))
})
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")
@AttributeOverrides({
        @AttributeOverride(name = "user_id", column = @Column(name = "user_id")),
        @AttributeOverride(name = "user_name", column = @Column(name = "user_name"))
})

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:

persistent classes

Add Dependencies on pom.xml File

Add the following dependencies in your pom.xml file, version may be different for your case.

<!-- 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">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

Frequently Asked Questions

What is the Table Per Concrete class in Hibernate?

Table Per Concrete class is a type of inheritance mapping strategy where tables are created per class. So the table does not contain any nullable values. 

How many layers are there in Hibernate architecture?

In Hibernate architecture, there are four layers.

What is the difference between session and season factory?

The main difference between the session and session factory is that the session is only available for a particular transaction. In contrast, the session factory is a factory class for session objects and is available for the whole application.

What is the difference between getCurrentSession() and openSession()?

The main difference between getCurrentSession() and openSession() is that openSession() always opens a new session that you need to close once you are done, but getCurrentSession() returns a session bound to a context that you don’t need to close.

Conclusion

This article extensively discussed TPC(Table Per Concrete class) Using Annotation in Hibernate.

We hope that through this blog, you got some ideas regarding TPC Using Annotation. If you would like to learn more, follow our articles on Your Guide To the ORM, Java Persistent APIExplore 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.

Happy Reading!

Live masterclass