hibernate inheritance

Posted: April 21, 2012 in Hibernate

Hibernate inheritance

In Front-End we have hierarchical data (inheritance)

In Back-End we don’t have hierarchical data (inheritance)

Front End and Back End model/formats is different. You cannot persist hierarchical data in Back End.

There is no inheritance concept in data base like oracle, mysql. But by using hibernate we can achieve. Everything done at hibernate level only.

Hibernate provides solution for the hierarchical data in different strategies like below.

  1. Table per class hierarchy
  2. Table per sub class
  3. Table per concrete class

Table per Class hierarchy:

In this strategy Hibernate allows you to persist sub class and super class information into a single table(entire hierarchy). And hibernate can distinguish the data belongs what particular sub class in the table based on discriminator column. The discriminator column value will be hard coded in the xml file. As it is not related to business. We are not mapping discriminator column to a pojo property.

Storing information in a single table we have to face “Sub Type” problem. To avoid this hibernate provides discriminator column in table per hierarchy.

Problem: (NOTNULL constraint)

In this strategy if we are persisting regular employee object the columns that is mapped to contract employee will be null vice versa. Suppose in our data base table if we are not interested to have a null value column means that column have constraint “NOT NULL”. Then this strategy is a failure one.

x number of classes = single  table in the database

Code in xml file:

<class name=”org.gopal.Employee” table=”employee”>

<id name=”eid” type=”java.lang.Long” column=”eid”>

<generator class=”assigned”>

</id>

<property name=”ename” column=”ename”/>

<discriminator column=”subtype”/>

<sub-class name=”org.gopal.RegularEmployee” discriminator-value=”regular”>

<property name=”salary” column=”salary”/>

</sub-class>

<sub-class name=”org.gopal.ContractEmployee” discriminator-value=”contract”>

<property name=”rateperhour” column=”rt_per_hour”/>

<property name=”hourperday” column=”hr_per_day”/>

</sub-class>

</class>

Table per sub class:

In this strategy for every class will be mapped to different tables. In our case employee is mapped to employee table. Regular employee is mapped to regular_employee table and contract employee is mapped contract_employee table.

regular_employee table has reference column which refer to employee table similarly

contact_employee table has reference column which refer to employee table.

Inside regular_employee or contact_employee we can have NOTNULL constraint also(drawback of class per hierarchy)

Problem: (joins)

If we want to read/get employee we should use joins. If we apply joins performance will be decrease when compare to reading a record from a single table.

x number of classes = x number of tables in the database

Code in xml file:

<class name=”org.gopal.Employee” table=”employee”>

<id name=”eid” type=”java.lang.Long” column=”eid”>

<generator class=”assigned”>

</id>

<property name=”ename” column=”ename”/>

<joined-subclass name=”org.gopal.RegularEmployee” table=”regular_emp”>

<key><column name=”eid”/> </key>

<property name=”salary” column=”salary”/>

</joined-subclass>

<joined-subclass name=”org.gopal.ContractEmployee” table=”contract_emp”>

<key><column name=”eid”/> </key>

<property name=”rateperhour” column=”rt_per_hour”/>

<property name=”hourperday” column=”hr_per_day”/>

</joined-subclass>

</class>

Table per Concrete class:

In this strategy Regular Employee and Employee information will persist into the regular_employee table and Contract Employee and Employee information will persist into the contract_employee table. Both these tables will persist employee information. Because of this eid, ename are became duplication in regular_employee and contract_employee tables.

Advantages:

1)      No need to execute the joins to get the data

2)      Apply NOTNULL constraints to the table columns.

Disadvantages:

1)      Duplication of  values like eid, ename.

x number of derived classes = x number of tables in the database

Code in xml file:

<class name=”org.gopal.Employee” table=”employee”>

<id name=”eid” type=”java.lang.Long” column=”eid”>

<generator class=”assigned”>

</id>

<property name=”ename” column=”ename”/>

<union-subclass name=”org.gopal.RegularEmployee” table=”regular_emp”>

<key><column name=”eid”/> </key>

<property name=”salary” column=”salary”/>

</union-subclass>

<union-subclass name=”org.gopal.ContractEmployee” table=”contract_emp”>

<key><column name=”eid”/> </key>

<property name=”rateperhour” column=”rt_per_hour”/>

<property name=”hourperday” column=”hr_per_day”/>

</union-subclass>

</class>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s