Search This Blog

Tuesday, 27 September 2011

The Hibernate Inheritance Mechanism - Preview

Inheritance which is one of the pillars of Object Oriented Programming Model isn't a big player in the SQL environment. While Objects can form both IS-A and HAS-A relationships, Tables are only aware of HAS-A  relationships. (for e.g. 1 Person has many Vehicles)

Hibernate provides techniques that allows us to represents Tables via inheritance relationships. Hibernate has 4 different approaches:
  1. Table per Concrete Class + Implicit Polymorphism
  2. Table per Concrete Class + Unions
  3. Table per Class Hierarchy
  4. Table per subclass
To study these mechanisms I decided to use something that I really enjoy a bit more ( OK OK a lot more) than work - sports. Consider the case of Sportsmen who share certain common attributes but when it comes to individual sports they also posses attributes unique to the game they play. So I developed a simple object model going with the the two sports that I love.
NOTE: Other sports lovers out there: I have used a very small sub set of attributes below to keep my Tables simple. Under no condition, is this to be considered as my very poor knowledge of these two really awesome sports :p
The java models for the above hierarchy would be as below:
public abstract class SportsPerson {
    private Long id;
    private String name;
        //setter getter
public class Cricketer extends SportsPerson {
    private Integer runs, wickets;
    private Boolean t20Player;
        // setter getter for above properties
public class Footballer extends SportsPerson {
    private Integer goals, appearances, sendOffs;
        // setter getter for above properties
I shall implement each of the supported approaches for this model schema in the coming posts

No comments:

Post a Comment