2. 2
Introduction
There are three types of inheritance mapping in hibernate
1. Table per concrete class with unions
2. Table per class hierarchy
3. Table per subclass
Hibernate Course in Navi Mumbai
3. 3
Example
Let us take the simple example of 3 java classes.
Class Manager and Worker are inherited from Employee Abstract
class.
1. Table per concrete class with unions
In this case there will be 2 tables
Tables: Manager, Worker [all common attributes will be
duplicated]
Hibernate Course in Navi Mumbai
4. Examples To be continued…
2. Table per class hierarchy
Single Table can be mapped to a class hierarchy
There will be only one table in database called
'Employee' that will represent all the attributes
required for all 3 classes.
But it needs some discriminating column to
differentiate between Manager and worker;
In this case there will be 3 tables represent
Employee, Manager and Worker
4Hibernate Course in Navi Mumbai
5. One table per class hierarchy:
5
Hibernate Course in Navi Mumbai
Lets say we have following class hierarchy. We have shape class
as base class and Rectangle and Circle inherit from Shape class.
6. Using table per class hierarchy
When using this inheritance strategy, the class hierarchy is represented by multiple classes combined into one de-
normalized DB table. A discriminator column identifies the type and the subclass.
Example:
6
Hibernate Course in Navi Mumbai
package com.sample
public class Vehicle {
long id;
int noOfTyres;
private String colour;
// Constructors and getters/setters
}
package com.sample
public class Car extends Vehicle {
private String licensePlate;
long price;
int speed;
// Constructors and getters/setters
}
9. 9
Hibernate Course in Navi Mumbai
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="VEHICLE")
@DiscriminatorValue("C")
public class Car extends Vehicle {
@Column(name="license_plate")
private String licensePlate;
@Column
long price;
@Column
int speed;
// Constructors and Getter/Setter methods,
}
10. Advantage & Disadvantage
10
Hibernate Course in Navi Mumbai
Advantage
Since this model uses a denormalized table, it
offers the best performance even for in the deep
hierarchy since it uses a single select to retrieve
data.
Disadvantage
Since the subclass state can’t have not-null
constraints data integrity is compromised. Also,
because all the state is in the same table, changes
to members of the hierarchy require column to be
altered, added or removed from the table.
11. 11Hibernate Course in Navi Mumbai
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
// Will use discriminator V
Vehicle v = new Vehicle(1, 4, "red");
session.save(v);
// Will use discriminator C
Car car = new Car("AB123456", 15000l, 200);
session.save(c);
session.getTransaction().commit();
session.close();
In the following example we will insert at first a Vehicle class (will into the
VEHICLE table using the discriminator "V"), then we will insert a Car Object
into the VEHICLE table (using the discriminator "C")
12. 12
Thank You
Vibrant Technology
Courses & Training institute in Navi Mumbai,Mumbai
“Corporate Training for hibernate in Mumbai”
Hibernate Course in Navi Mumbai