Table Per Concrete class using Annotation in hibernate

In case of Table Per Concrete class, tables are created per class. So there are no nullable values in the table. Disadvantage of this approach is that duplicate columns are created in the subclass tables.

Here, we need to use @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) annotation in the parent class and @AttributeOverrides annotation in the subclasses.

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) specifies that we are using table per concrete class strategy. It should be specified in the parent class only.

@AttributeOverrides defines that parent class attributes will be overriden in this class. In table structure, parent class table columns will be added in the subclass table.

The class hierarchy is given below:

table per concrete class

Example of Table per concrete class

In this example we are creating the three classes and provide mapping of these classes in the employee.hbm.xml file.

Create the Persistent classes

You need to create the persistent classes representing the inheritance. Let's create the three classes for the above hierarchy:

package com.javatportal.mypackage;
import javax.persistence.*;

@Table(name = "employee102")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

public class Employee {
@Column(name = "id")
private int id;

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

//setters and getters

package com.javatportal.mypackage;
import javax.persistence.*;

    @AttributeOverride(name="id", column=@Column(name="id")),
    @AttributeOverride(name="name", column=@Column(name="name"))
public class Regular_Employee extends Employee{
private float salary;

private int bonus;

//setters and getters

package com.javatportal.mypackage;
import javax.persistence.*;
    @AttributeOverride(name="id", column=@Column(name="id")),
    @AttributeOverride(name="name", column=@Column(name="name"))
public class Contract_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 payPerHour) {
		pay_per_hour = payPerHour;
	public String getContract_duration() {
		return contract_duration;
	public void setContract_duration(String contractDuration) {
		contract_duration = contractDuration;

Add mapping of hbm file in configuration file


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<!-- Generated by MyEclipse Hibernate Tools.                   -->
        <property name="">update</property>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">system</property>
        <property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 		<mapping class="com.javatportal.mypackage.Employee"/>
 		<mapping class="com.javatportal.mypackage.Contract_Employee"/>
 		<mapping class="com.javatportal.mypackage.Regular_Employee"/>

The property is defined for creating automatic table in the database.

3. Create the class that stores the persistent object

In this class, we are simply storing the employee objects in the database.

package com.javatportal.mypackage;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class StoreData {
public static void main(String[] args) {
AnnotationConfiguration cfg=new AnnotationConfiguration();
Session session=cfg.configure("hibernate.cfg.xml").buildSessionFactory().openSession();
	Transaction t=session.beginTransaction();
	Employee e1=new Employee();
	Regular_Employee e2=new Regular_Employee();
	e2.setName("rahul Kumar");
	Contract_Employee e3=new Contract_Employee();
	e3.setName("neah kumari");
	e3.setContract_duration("15 hours");