Hibernate Tutorial
Core Hibernate
Hibernate Mapping
Hibernate Annotations
Hibernate with Spring Framework
Hibernate with Database
Hibernate Log4j
Inheritance Mapping
Inheritance is a fundamental concept in object-oriented programming that allows one class to inherit properties and behaviors from another class. In relational databases, there isn't a direct way to represent inheritance. Hibernate provides strategies to map an inheritance hierarchy into relational databases. Here are the main inheritance mapping strategies in Hibernate:
Pros:
Cons:
Example:
For an inheritance hierarchy of Vehicle
, Car
, and Bike
, you'd have one table named VEHICLE
with columns specific to both Car
and Bike
and a discriminator column to differentiate between records of type Car
and Bike
.
Pros:
Cons:
Example:
For the Vehicle
, Car
, and Bike
hierarchy, there would be two tables: CAR
and BIKE
. Both tables will have columns for attributes defined in the Vehicle
class.
Pros:
Cons:
Example:
For the Vehicle
, Car
, and Bike
hierarchy, there would be three tables: VEHICLE
, CAR
, and BIKE
. The CAR
and BIKE
tables would have foreign keys that reference the primary key of the VEHICLE
table.
Here's how you'd represent inheritance mapping using annotations:
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "vehicle_type") public class Vehicle { /* common attributes */ } @Entity @DiscriminatorValue("car") public class Car extends Vehicle { /* car-specific attributes */ } @Entity @DiscriminatorValue("bike") public class Bike extends Vehicle { /* bike-specific attributes */ }
@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class Vehicle { /* common attributes */ } @Entity public class Car extends Vehicle { /* car-specific attributes */ } @Entity public class Bike extends Vehicle { /* bike-specific attributes */ }
@Entity @Inheritance(strategy = InheritanceType.JOINED) public class Vehicle { /* common attributes */ } @Entity @PrimaryKeyJoinColumn(name = "vehicle_id") public class Car extends Vehicle { /* car-specific attributes */ } @Entity @PrimaryKeyJoinColumn(name = "vehicle_id") public class Bike extends Vehicle { /* bike-specific attributes */ }
In conclusion, the choice of inheritance mapping strategy depends on your application's specific requirements and the trade-offs you're willing to accept between normalization and performance.
Implementing single table inheritance in Hibernate:
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "employee_type", discriminatorType = DiscriminatorType.STRING) public abstract class Employee { // Common fields and methods } @Entity @DiscriminatorValue("FT") public class FullTimeEmployee extends Employee { // Fields and methods specific to FullTimeEmployee } @Entity @DiscriminatorValue("PT") public class PartTimeEmployee extends Employee { // Fields and methods specific to PartTimeEmployee }
Table per concrete class mapping in Hibernate:
@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class Employee { // Common fields and methods } @Entity public class FullTimeEmployee extends Employee { // Fields and methods specific to FullTimeEmployee } @Entity public class PartTimeEmployee extends Employee { // Fields and methods specific to PartTimeEmployee }
Mapping joined table inheritance in Hibernate:
@Entity @Inheritance(strategy = InheritanceType.JOINED) public abstract class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // Common fields and methods } @Entity public class FullTimeEmployee extends Employee { private double salary; // Fields and methods specific to FullTimeEmployee } @Entity public class PartTimeEmployee extends Employee { private double hourlyRate; // Fields and methods specific to PartTimeEmployee }