Implementing Inheritance in Hibernate (Single Table Strategy, With Table Per Class Strategy, With Joined Strategy)

This tutorial discusses what are the types of inheritance models in Hibernate and describes how they work like vertical inheritance and horizontal.
There are three types of inheritance mapping in hibernate
1. Table per concrete class with unions
2. Table per class hierarchy(Single Table Strategy)
3. Table per subclass
Example:
Let us take the simple example of 3 java classes.
Class TwoWheelerVehicle and FourWheelerVehicle are inherited from Vehicle Abstract class.

1. Table per concrete class with unions
In this case there will be 2 tables
Tables: TwoWheelerVehicle, FourWheelerVehicle[all common attributes will be duplicated]

2. Table per class hierarchy
Single Table can be mapped to a class hierarchy
There will be only one table in database called 'Vehicle' that will represent all the attributes required for all 3 classes.
But it needs some discriminating column to differentiate between TwoWheelerVehicle and  FourWheelerVehicle;
3. Table per subclass
In this case there will be 3 tables represent TwoWheelerVehicle , FourWheelerVehicle and Vehicle

Implementing Inheritance in Hibernate

Inheritance is one of the most visible facets of Object-relational mismatch. Object oriented systems can model both “is a” and “has a” relationship. Relational model supports only “has a” relationship between two entities. Hibernate can help you map such Objects with relational tables. But you need to choose certain mapping strategy based on your needs. There are three possible strategies to use.
  1. Single Table Strategy,

  2. With Table Per Class Strategy,

  3. With Joined Strategy 



Single Table Strategy

In Single table per subclass, the union of all the properties from the inheritance hierarchy is mapped to one table. As all the data goes in one table, a discriminator is used to differentiate between different type of data.
Advantages of Single Table per class hierarchy

  • Simplest to implement.
  • Only one table to deal with.
  • Performance wise better than all strategies because no joins or sub-selects need to be performed.

Disadvantages:
  • Most of the column of table are nullable so the NOT NULL constraint cannot be applied.
  • Tables are not normalized.
Lets see the following example code.
Vehicle.java
package com.sdnext.hibernate.tutorial.dto;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table(name="VEHICLE")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE) //Least normalisation strategy
public class Vehicle 
{
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 @Column(name="VEHICLE_ID")
 private int vehicleId;
 
 @Column(name="VEHICLE_NAME")
 private String vehicleName;
 
 public int getVehicleId() {
  return vehicleId;
 }
 public void setVehicleId(int vehicleId) {
  this.vehicleId = vehicleId;
 }
 public String getVehicleName() {
  return vehicleName;
 }
 public void setVehicleName(String vehicleName) {
  this.vehicleName = vehicleName;
 }
}



TwoWheeler.java
package com.sdnext.hibernate.tutorial.dto;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="TWO_WHEELER")
//@DiscriminatorValue("Bike")
public class TwoWheeler extends Vehicle 
{
        @Column(name="STEERING_TYPE")
        private String steeringTwoWheeler;

 public String getSteeringTwoWheeler()
 {
  return steeringTwoWheeler;
 }

 public void setSteeringTwoWheeler(String steeringTwoWheeler) 
 {
  this.steeringTwoWheeler = steeringTwoWheeler;
 }
}

FourWheeler.java
package com.sdnext.hibernate.tutorial.dto;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="FOUR_WHEELER")
//@DiscriminatorValue("Car")
public class FourWheeler extends Vehicle
{
 @Column(name="STEERING_TYPE")
 private String steeringFourWheeler;

 public String getSteeringFourWheeler() 
 {
  return steeringFourWheeler;
 }

 public void setSteeringFourWheeler(String steeringFourWheeler) 
 {
  this.steeringFourWheeler = steeringFourWheeler;
 }
}

hibernate.cfg.xml
<hibernate-configuration> 
 <session-factory> 
  <!-- Database connection settings -->
   <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
   <property name="connection.url">jdbc:mysql://localhost:3306/vehicleDB2</property> 
   <property name="connection.username">root</property> 
   <property name="connection.password">root</property> 

  <!-- JDBC connection pool (use the built-in) -->
   <property name="connection.pool_size">1</property> 

  <!-- SQL dialect -->
   <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

  <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property> 
  
  <!-- Disable the second-level cache -->
   <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

  <!-- Echo all executed SQL to stdout -->
   <property name="show_sql">true</property> 
  
  <!-- Drop and re-create the database schema on startup -->
   <property name="hbm2ddl.auto">create</property> 
   
   
   <mapping class="com.sdnext.hibernate.tutorial.dto.Vehicle">
   <mapping class="com.sdnext.hibernate.tutorial.dto.TwoWheeler">
   <mapping class="com.sdnext.hibernate.tutorial.dto.FourWheeler">
   
  </mapping></mapping></mapping></session-factory> 
 </hibernate-configuration>  
Now run the following test class
HibernateTestDemo.java
package com.sdnext.hibernate.tutorial;

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

import com.sdnext.hibernate.tutorial.dto.FourWheeler;
import com.sdnext.hibernate.tutorial.dto.TwoWheeler;
import com.sdnext.hibernate.tutorial.dto.Vehicle;

public class HibernateTestDemo {

 /**
  * @param args
  */
 public static void main(String[] args) 
 {
  SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  session.beginTransaction();
  
  Vehicle vehicle = new Vehicle();
  vehicle.setVehicleName("Car");
  
  TwoWheeler twoWheeler = new TwoWheeler();
  twoWheeler.setVehicleName("Bike");
  twoWheeler.setSteeringTwoWheeler("Bike Steering Handle");
  
  FourWheeler fourWheeler = new FourWheeler();
  fourWheeler.setVehicleName("Alto");
  fourWheeler.setSteeringFourWheeler("Alto Steering Wheel");
  
  session.save(vehicle);
  session.save(twoWheeler);
  session.save(fourWheeler);
  
  session.getTransaction().commit();
  session.close();
 }
}

Single Table Strategy



In the above table Vehicle there are four columns (DTYPE, VEHICLE_ID, VEHICLE_NAME, STEERING_TYPE).
The first column has the value of discriminator type(DTYPE) is Vehicle, TwoWheeler, FourWheeler as its entity name by default.

For user convenience we can override the default value of column as well as column name by using the following annotation.
@DiscriminatorColumn
Target:
  Classes

Specifies the discriminator column for the SINGLE_TABLE and JOINED Inheritance mapping strategies.
The strategy and the discriminator column are only specified in the root of an entity class hierarchy or subhierarchy in which a different inheritance strategy is applied
If the DiscriminatorColumn annotation is missing, and a discriminator column is required, the name of the discriminator column defaults to "DTYPE" and the discriminator type to DiscriminatorType.STRING.

@DiscriminatorValue

Target:
  Classes

Specifies the value of the discriminator column for entities of the given type.

The DiscriminatorValue annotation can only be specified on a concrete entity class.

If the DiscriminatorValue annotation is not specified and a discriminator column is used, a provider-specific function will be used to generate a value representing the entity type. If the DiscriminatorType is STRING, the discriminator value default is the entity name.

The inheritance strategy and the discriminator column are only specified in the root of an entity class hierarchy or subhierarchy in which a different inheritance strategy is applied. The discriminator value, if not defaulted, should be specified for each entity class in the hierarchy.

@Inheritance


Target:
  Classes

Defines the inheritance strategy to be used for an entity class hierarchy. It is specified on the entity class that is the root of the entity class hierarchy. If the Inheritance annotation is not specified or if no inheritance type is specified for an entity class hierarchy, the SINGLE_TABLE mapping strategy is used.

Now adding the following annotation to the Vehicle class is
@Entity
@Table(name="VEHICLE")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE) //Least normalisation strategy
@DiscriminatorColumn(
  name="VEHICLE_TYPE", 
  discriminatorType=DiscriminatorType.STRING
  )
public class Vehicle 
{
Now adding following annotation to the TwoWheeler class
@DiscriminatorValue("Bike")
public class TwoWheeler extends Vehicle 
{
Now adding following annotation to the FourWheeler class
@DiscriminatorValue("Car")
public class FourWheeler extends Vehicle 
{
After these above modification we run the code then we will get the following output.

Single Table Strategy

With Table Per Class Strategy

In this case every entity class has its own table i.e. table per class. The data for Vehicle is duplicated in both the tables.
This strategy is not popular and also have been made optional in Java Persistence API.
Advantage:

  • Possible to define NOT NULL constraints on the table.

Disadvantage:

  • Tables are not normalized.
  • To support polymorphism either container has to do multiple trips to database or use SQL UNION kind of feature.
In this case there no need for the discriminator column because all entity has own table.
The Vehicle entity in this case is
Vehicle.java
package com.sdnext.hibernate.tutorial.dto;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table(name="VEHICLE")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) //slightly more normalized
public class Vehicle 
{
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 @Column(name="VEHICLE_ID")
 private int vehicleId;
 
 @Column(name="VEHICLE_NAME")
 private String vehicleName;
 
 public int getVehicleId() {
  return vehicleId;
 }
 public void setVehicleId(int vehicleId) {
  this.vehicleId = vehicleId;
 }
 public String getVehicleName() {
  return vehicleName;
 }
 public void setVehicleName(String vehicleName) {
  this.vehicleName = vehicleName;
 }
}
And there no need to the discriminator value for the TwoWheeler and FourWheeler Entity so in this case the
TwoWheeler.java
package com.sdnext.hibernate.tutorial.dto;

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

@Entity
@Table(name="TWO_WHEELER")
public class TwoWheeler extends Vehicle 
{
 @Column(name="STEERING_TYPE")
 private String steeringTwoWheeler;

 public String getSteeringTwoWheeler()
 {
  return steeringTwoWheeler;
 }

 public void setSteeringTwoWheeler(String steeringTwoWheeler) 
 {
  this.steeringTwoWheeler = steeringTwoWheeler;
 }
}
FourWheeler.java
package com.sdnext.hibernate.tutorial.dto;

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

@Entity
@Table(name="FOUR_WHEELER")
public class FourWheeler extends Vehicle
{
 @Column(name="STEERING_TYPE")
 private String steeringFourWheeler;

 public String getSteeringFourWheeler() 
 {
  return steeringFourWheeler;
 }

 public void setSteeringFourWheeler(String steeringFourWheeler) 
 {
  this.steeringFourWheeler = steeringFourWheeler;
 }
}

Table Per Class Strategy




With Joined Strategy

It's highly normalized but performance is not good.
Advantage:
  • Tables are normalized.
  • Able to define NOT NULL constraint.
Disadvantage:
  • Does not perform as well as SINGLE_TABLE strategy
Using Join Strategy with the vehicle entity
Vehicle.java
package com.sdnext.hibernate.tutorial.dto;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table(name="VEHICLE")
@Inheritance(strategy=InheritanceType.JOINED)//Highly normalized
public class Vehicle 
{
 @Id
 @GeneratedValue
 @Column(name="VEHICLE_ID")
 private int vehicleId;
 
 @Column(name="VEHICLE_NAME")
 private String vehicleName;
 
 public int getVehicleId() {
  return vehicleId;
 }
 public void setVehicleId(int vehicleId) {
  this.vehicleId = vehicleId;
 }
 public String getVehicleName() {
  return vehicleName;
 }
 public void setVehicleName(String vehicleName) {
  this.vehicleName = vehicleName;
 }
}
Now run the code we will get the following output.

With Joined Strategy




We have seen the three strategies about inheritance in the hibernate.
A comparison of three strategies is as follows:
Criteria Single Table Table per subclass(Join Strategy) Table per Class
Table Support
  • Data not normalized.
  • Constraint for mandatory columns to be not nullable cannot applied.
  • Change in any subclass leads to change in structure of Table
  • Normalized.
  • Mandatory column constraint can be applied
  • One table for each concrete class.
  • Not maintainable.
  • Change in base class leads to changes in all tables of derived class
Discriminator Column Present Absent Absent
Retrieving data simple SELECT. All data is in one table. Using discriminator type, individual types can be selected Joins among table. For example fetching FourWheeler will require a join on FourWheeler and Vehicle table. If all user needs to be fetched than it will put a join for all three tables Separate Select or Union Select
Updating and Inserting Single INSERT or UPDATE Multiple. For Vehicle type one insert on Vehicle table. For FourWheeler type one insert on Vehicle table and another on FourWheeler table. One insert or update for each subclass
JPA Support Mandatory Optional



In the Next Chapter we will learn about the CRUD Operation using Hibernate.

                  <<Previous Chapter 21<<    >>Next Chapter23>>