Hibernate Tutorial - Part IV

For previous posts refer:
Hibernate tutorial Part - I
Hibernate tutorial Part - II
Hibernate tutorial Part - III

In this post, we will take a look at 'Table per class hierarchy' strategy of inheritance.


Table Per Class hierarchy:

In this approach, an entire class hierarchy can be mapped to a single table. This table includes columns for all properties of all classes in the hierarchy. The concrete subclass represented
by a particular row is identified by the value of a type discriminator column.

Lets take a look at the classes and their annotations:

User Class:

@Entity
@Table(name = "USER") 
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(     name="USER_TYPE",     discriminatorType=DiscriminatorType.STRING )
@DiscriminatorValue(value="U") 
public class User implements Serializable{
     @Id    
     @Column(name = "USER_ID")
     private String userId;
     
     @Column(name = "USERNAME")
     private String userName;
     
     @Column(name = "PASSWORD")
     private String password;
     
     //setters and getters
}

Customer Class:

@Entity
@Table(name="USER") 
@DiscriminatorValue(value="C")     
public class Customer extends User implements Serializable{
     
     @Column(name = "ADDRESS1")
     private String address1;
     @Column(name = "ADDRESS2")
     private String address2;
     @Column(name = "CITY")
     private String city;
     @Column(name = "STATE")
     private String state;
     @Column(name = "COUNTRY")
     private String country;
     
     //setters and getters
}

Internal User Class:

@Entity
@Table(name = "USER") 
@DiscriminatorValue(value="I")
public class InternalUser extends User implements Serializable{
     /*Admin/Non Admin*/
     @Column(name = "ACCESSTYPE")
     private String accessType;
     /*Contract/Permament*/
     @Column(name = "EMPLOYMENTTYPE")
     private String employmentType;
     
     //setters and getters
}

Note:
@DiscriminatorColumn - Is used to define the discriminator column for the SINGLE_TABLE and JOINED inheritance mapping strategies. The strategy and the discriminator column are only specified in the root of an entity class hierarchy or sub hierarchy in which a different inheritance strategy is applied.

@DiscriminatorValue- Is used to specify the value of the discriminator column for entities of the given type.

Table Structure:
Single Table User with the following columns. Note the USER_TYPE column is used to distinguish between different entities.

USER_ID
USERNAME
PASSWORD
ADDRESS1
ADDRESS2
CITY
STATE
COUNTRY
USER_TYPE
ACCESSTYPE

EMPLOYMENTTYPE


Disadvantages of this approach:

  1. Columns for properties declared by subclasses must be declared to be nullable.
  2. Schema is not normalized. We’ve created functional dependencies between nonkey columns, violating the third normal form.
The advantage of this approach compared to others is performance. Since we have to query only a single table, this approach provides very good performance.

                                                Continued...




Comments

Popular posts from this blog

Pivotal Cloud Foundry (PCF) Integration with Elastic Cloud Storage (ECS)

Restful code example using Spring MVC

Spring Integration - Bulk processing Example