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.
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
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.
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
Disadvantages:
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(); } }
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.
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:
Disadvantage:
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; } }
It’s highly normalized but performance is not good.
Advantage:
Disadvantage:
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.
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 |
|
|
|
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>>
View Comments
Bro could you please tell me the process how can we retrieve entity based on discriminator value,i know this approach is not industry standard but still want to know.
"Class TwoWheelerVehicle and FourWheelerVehicle are inherited from Vehicle Abstract class."
I think it isn't really abstract, is it?
Hi,
I was reading the following link (http://www.baeldung.com/hibernate-inheritance). I could not understand what happens in the retrieve and insertion of records in the different cases. Can you kindly write a blog post or explain it below with respect to the entities in the given link.
Best regards
Rajasekhar
Great post !
In the Table per class strategy only child tables will be created not the parent. Hence we will see only two tables getting created in database i.e. TWO_WHEELER and FOUR_WHEELER.