Wednesday, April 6, 2016

Inheritance in Hibernate

Hibernate have lot of good feature, in that Inheritance is one of the best. Its supports some of the following mapping strategies.

Single Table Per Class:
            Here both super class and sub class are mapped to same table. Another thing, one additional column need to mention to identify row is an instance of super class or sub class. 

Joined Sub Class:
            A separate table will be used for each class. The sub class table will store only the fields that are not present in the super class. So that in future, if you need to retrieve all data join need to performed on that two tables.

Table Per Class:
            A separate table will be used for each class. The sub class can store fields of super class also. So that no need to perform join to retrieve data, since sub class one row have all details.


Example of Single Table Per Class

            Already we have implemented Employee class. I have created below Skill class as a sub class of Employee.


public class Skill extends Employee {
private String skillName;
//Setter and Getter method
}

Now let’s see the mapping file for the additional field skillName. 


      
        
           
        
     

     
     
     
     
        
     


There is a new tag called discriminator column, which will store type information your current data.(Which class belongs to).


In our case we have created EMPLOYEE_TYPE as a discriminator and set the type to string. You can use int also. 

We have added new sub class element tag to mentioned added sub class to mention its fields and column. The subclass field skillName will be mapped to SKILL_NAME column.

session.getTransaction().begin();
Skill skill = new Skill();
skill.setFirstName("Test First Name");
skill.setLastName("Test Last Name");
skill.setSkillName("TestSkillName");
session.save(skill);
session.getTransaction().commit();


After executing this you will see below output.
Hibernate:
    drop table T_EMPLOYEE if exists
Hibernate:
    create table T_EMPLOYEE (
      ID bigint generated by default as identity,
      EMPLOYEE_TYPE varchar(255) not null,
      FIRST_NAME varchar(255),
      LAST_NAME varchar(255),
      SKILL_NAME varchar(255),
      primary key (ID)
)
Hibernate:
   insert into T_EMPLOYEE
       (ID, FIRST_NAME, LAST_NAME, SKILL_NAME, EMPLOYEE_TYPE)
   values
   null, ?, ?, ?, ’hibernate.entity.Skill’)

Example of Joined Sub Class
           
            Next we can see Joined sub class which definition already provided. See below mapping file,



   
    
     
   

        
        

      
           
       
      
    
            The Joined sub class tag will instruct hibernate to create table for the sub class of Skill and the table name is T_SKILL and added one new column for the purpose of accessing its parent table(Super class) T_EMPLOYEE. means, it will work as foreign key.


Once you execute this code, you will see below in console.


Hibernate:
      insert into T_EMPLOYEE
     (ID, FIRST_NAME, LAST_NAME, ID_ID_CARD)
     values
     (null, ?, ?, ?)

Hibernate:
     insert
     into
     T_SKILL
     (SKILL_NAME, ID_PERSON)
     values
     (?, ?)

Example of Table Per Class


            Next we can see table per class, its definition was provided already. see below mapping file,



 
  
   
    
   
   
   
   
  
   
    
   
  
 
You can see generator class as sequence , which will create a unique id for both table T_EMPLOYEE and T_SKILL. As usual Union-subclass tag will create a separate table T_SKILL whereas same column SKILL_NAME. 

After executing your code, you will see below in console tab.


Hibernate:
 call next value for hibernate_sequence
Hibernate:
 insert into T_PERSON (
 FIRST_NAME, LAST_NAME, ID)
values
   (?, ?, ?, ?)

Hibernate:
    call next value for hibernate_sequence

Hibernate:
    insert into T_SKILL (
 FIRST_NAME, LAST_NAME, FAV_PROG_LANG, ID)
values
   (?, ?, ?, ?, ?)

No comments:

Post a Comment