Powered by Blogger.

Thursday, 10 July 2014

Inheritance mapping models in Hibernate

By Shambhu Kumar  |  01:59 No comments

There can be three kinds of inheritance mapping in hibernate
  1. Table per concrete class with unions
  2. Table per class hierarchy
  3. Table per subclass


Example:
We can take an example of three Java classes like Vehicle, which is an abstract class and two subclasses of Vehicle as Car and UtilityVan.

1. Table per concrete class with unions
In this scenario there will be 2 tables
Tables: Car, UtilityVan, here in this case all common attributes will be duplicated.

2. Table per class hierarchy
Single Table can be mapped to a class hierarchy
There will be only one table in database named 'Vehicle' which will represent all attributes required for all three classes.
Here it is be taken care of that discriminating columns to differentiate between Car and UtilityVan

3. Table per subclass
Simply there will be three tables representing Vehicle, Car and UtilityVan

One Table per Concrete Class example


In One Table per Concrete class scheme, each concrete class is mapped as normal persistent class. Thus we have 3 tables; PERSON, EMPLOYEE and OWNER to persist the class data. In this scheme, the mapping of the subclass repeats the properties of the parent class.
Following are the advantages and disadvantages of One Table per Subclass scheme.

Advantages

·         This is the easiest method of Inheritance mapping to implement.

Disadvantages

·         Data thats belongs to a parent class is scattered across a number of subclass tables, which represents concrete classes.
·         This hierarchy is not recommended for most cases.
·         Changes to a parent class is reflected to large number of tables
·         A query couched in terms of parent class is likely to cause a large number of select operations
This strategy has many drawbacks (esp. with polymorphic queries and associations) explained in the JPA spec, the Hibernate reference documentation, Hibernate in Action, and many other places. Hibernate work around most of them implementing this strategy using SQL UNION queries. It is commonly used for the top level of an inheritance hierarchy:



One Table Per Class Hierarchy example

Suppose we have a class Person with subclass Employee. The properties of each class are:
In One Table per Class Hierarchy scheme, we store all the class hierarchy in a single SQL table. A discriminator is a key to uniquely identify the base type of the class hierarchy.
Following are the advantages and disadvantages of One Table per Class Hierarchy scheme.

Advantage

This hierarchy offers the best performance even for in the deep hierarchy since single select may suffice.

Disadvantage

Changes to members of the hierarchy require column to be altered, added or removed from the table.

@Entity
@Table(name = "PERSON")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator",
                     discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="P")
public class Person

@Entity
@Table(name="PERSON")
@DiscriminatorValue("E")
public class Employee extends Person
One Table Per Subclass example
Suppose we have a class Person with subclass Employee and Owner. Following the class diagram and relationship of these classes.

In One Table per Subclass scheme, each class persist the data in its own separate table. Thus we have 3 tables; PERSON, EMPLOYEE and OWNER to persist the class data. Note that a foreign key relationship exists between the subclass tables and super class table. Thus the common data is stored in PERSON table and subclass specific fields are stored in EMPLOYEE and OWNER tables.
Following are the advantages and disadvantages of One Table per Subclass scheme.

Advantage

·         Using this hierarchy, does not require complex changes to the database schema when a single parent class is modified.
·         It works well with shallow hierarchy.

Disadvantage

·         As the hierarchy grows, it may result in poor performance.
·         The number of joins required to construct a subclass also grows.

@Entity
@Table(name = "PERSON")
@Inheritance(strategy=InheritanceType.JOINED)
public class Person

@Entity
@Table(name="EMPLOYEE")
@PrimaryKeyJoinColumn(name="PERSON_ID")
public class Employee extends Person


Author: Shambhu Kumar

Hello, I am Author, decode to know more: In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo magna nisl, ac porta turpis blandit quis. Lorem ipsum dolor sit amet.

0 comments:

Recent Articles

© 2014 Learning Java. WP themonic converted by Bloggertheme9. Published By Gooyaabi Templates
TOP