Hibernate Table per concrete class hierarchy using annotations Tutorial with examples

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:

The table structure for each table will be as follows:
CREATE TABLE EMPLOYEE (
ID int IDENTITY,
NAME VARCHAR(50)

)


CREATE TABLE REGULAR_EMPLOYEE (
ID int ,
NAME VARCHAR(50),
SALARY decimal(10,2),
BONUS decimal(10,2)
)

CREATE TABLE CONTRACT_EMPLOYEE (
ID int ,
NAME VARCHAR(50),
PAY_PER_HOUR decimal(10,2),
CONTRACT_DURATION VARCHAR(50)
)


Example of Table per concrete class

In this example we are creating the three classes and provide mapping of these classes in the hibernate.cfg.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:
File: Employee.java
package com.tutorialsdesk.inheritance;

import javax.persistence.*;

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

public class Employee{
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
    
@Column(name = "id")
private int id;

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

public Employee() {
 
}

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: Regular_Employee.java
package com.tutorialsdesk.inheritance;

import javax.persistence.*;

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

@Column(name="bonus") 
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;
}


}

File: Contract_Employee.java
package com.tutorialsdesk.inheritance;

import javax.persistence.AttributeOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity  
@Table(name="CONTRACT_EMPLOYEE")   
@AttributeOverrides({   
    @AttributeOverride(name="id", column=@Column(name="id")),   
    @AttributeOverride(name="name", column=@Column(name="name"))   
}) 
public class Contract_Employee extends Employee{
 
 @Column(name="pay_per_hour")
 private float pay_per_hour;
 
 @Column(name="contract_duration")
 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;
 }


}

Add mapping of hbm file in configuration file

File: hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.password">PASSWORD</property>
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433/DBNAME</property>
        <property name="hibernate.connection.username">USERNAME</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
     <property name="connection.pool_size">1</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>  
  <property name="format_sql">true</property>  
     
        <mapping class="com.tutorialsdesk.inheritance.Employee"/>
        <mapping class="com.tutorialsdesk.inheritance.Contract_Employee"/>
       <mapping class="com.tutorialsdesk.inheritance.Regular_Employee"/>
    </session-factory>
</hibernate-configuration>



Create the class that stores the persistent object

In this class, we are simply storing the employee objects in the database.
File: StoreData.java
package com.tutorialsdesk.inheritance;

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

public class StoreData {
public static void main(String[] args) {
 AnnotationConfiguration cfg=new AnnotationConfiguration();
 Session session=cfg.configure("/conf/hibernate.cfg.xml").buildSessionFactory().openSession();
 
 Transaction t=session.beginTransaction();
 
 Employee e1=new Employee();
 e1.setName("sonoo");
 
 Regular_Employee e2=new Regular_Employee();
 e2.setName("Vivek Kumar");
 e2.setSalary(50000);
 e2.setBonus(5);
 
 Contract_Employee e3=new Contract_Employee();
 e3.setName("Arjun Kumar");
 e3.setPay_per_hour(1000);
 e3.setContract_duration("15 hours");
 
 session.persist(e1);
 session.persist(e2);
 session.persist(e3);
 
 t.commit();
 session.close();
 System.out.println("success");
}
}
Hibernate Table per concrete class hierarchy using annotations Tutorial with examples