Jan 1, 2010

Inheritance using Hibernate

Introduction

Hibernate is an open source java package. It helps us to work with relational database. It provides a solution to map database tables to a class. You will feel that database contains plain Java objects. It allows you to concentrate on the objects and features of your application without worrying about the storing and finding the data. Thus hibernate relieves the developer from 95 percent of common data related programming tasks.

Hibernate is a Free Software.

Hibernate lets you develop persistent classes with the following object oriented features: -

  • Association (Gives the relationship between Objects. Association identifies that one object is required by another object to perform its task)
  • Inheritance (Portrays generalization and specification among classes)
  • Polymorphism (overloading in object oriented concept)
  • Composition
  • Java collections framework (like Set, Bag, List etc)

In this article we will see how to use inheritance using hibernate.

Inheritance

Inheritance is an object oriented mechanism which portrays generalization and specification. It expresses similarity among classes. It is a parent child relationship with generalized class at the top and specialized classes below.

For example, Person is a generalized class and Teacher and Student are specialized classes or subclasses of Person. i.e. Teacher and Student can have properties of Person as well as their specific properties.

image

Inheritance Mapping

In hibernate there are three different ways by which inheritance can be represented.

  • Table per class hierarchy
  • Table per subclass
  • Table per concrete class

Table per class hierarchy

In this approach the entire hierarchy is mapped to a single table. I.e. All attributes of all the classes are available in a single table as its columns. A discriminator is used to distinguish different classes.

Considering the example of Student, Teacher and Person, we will be using a single table as below with a discriminator (Person_Type) to distinguish the type of Person

image 

PERSON table consists of all attributes of Person, Teacher and Students as its columns.

Mapping of Table per class hierarchy:

Person.hbm.xml

<hibernate-mapping>
<class name=”Persontable=”PERSON>
<id name=”idcolumn=”ID>
<generator class=”increment/>
</id>
<discriminator column=”PERSON_TYPEtype=”string/>
<property name= “namecolumn=”NAME/>
</class>
</hibernate-mapping>


Person table contains a column ‘Person_type’. This column is mapped using the <discriminator> XML element.


Discriminator is not a property of any java class, it is a column shared between database and Hibernate. Hibernate using this column to instantiate the appropriate class and populate it accordingly.



Student.hbm.xml
===============

<hibernate-mapping>
<subclass name=”Studentextends=Persondiscriminator-value=”Student>
<property name= “branchcolumn=”BRANCH/>
</subclass>
</hibernate-mapping>

Teacher.hbm.xml
===============

<hibernate-mapping>
<subclass name=”Teacherextends=Persondiscriminator-value=”Teacher>
<property name= “departmentcolumn=”DEPARTMENT/>
</subclass>
</hibernate-mapping>

<subclass> XML element is used to map the concrete classes, Student and Teacher.


<subclass> element consists of the attributes extends and discriminator-value.


Advantage: This approach is simple and efficient as all data are available in a single table and no joining of tables is required.


Disadvantage: Disadvantage in this mapping strategy is that columns declared by the subclasses cannot have NOT NULL constraints.


Table per concrete class


In this approach separate class elements are required for each class.


Considering the example of Student, Teacher, Person we will use two different tables:


image


Mapping using <union-subclass>


<union-subclass> element is used for each subclass in mapping file.


<class name="Person">
<id name="id" type="long" column="ID">
<generator class="sequence"/>
</id>
<property name="name" column="NAME"/>
<union-subclass name="Student" table="STUDENT">
<property name="branch" column="BRANCH"/>
</union-subclass>
<union-subclass name="Teacher" table="TEACHER ">
<property name="branch" column="BRANCH"/>
</union-subclass>
</class>


The limitation of this approach is that if a property is mapped on the super class, the column name must be the same on all subclass tables.


Table per subclass



In this approach also, separate tables are required for different classes, but will have only specific attributes of subclasses instead of the inherited properties as in table per concrete class.



Considering the example of Student, Teacher, Person we will use three different tables:



image



<joined-subclass> element is used for each subclass in mapping file A table per subclass mapping looks like this:



<class name="Person" table="PERSON">
<id name="id” column="ID">
<generator class="native"/>
</id>
<property name="name" column="NAME"/>
<joined-subclass name="Teacher" table="TEACHER">
<key column="ID"/>
<property name="department" column="DEPARTMENT"/>
</joined-subclass>
<joined-subclass name="Student" table="STUDENT">
<key column="ID"/>
<property name="branch" column="BRANCH"/>
</joined-subclass>
</class>


In this case there are three tables. The subclasses will have primary key with the super class.



Conclusion



In this document we have seen three different approaches to represent inheritance using hibernate.




  • Table per class hierarchy

  • Table per subclass

  • Table per concrete class



Reference




9 comments:

Anonymous said...

Good article. Thnx

Anonymous said...

Very nice short and easy explanation. Thanks.

Srinivas said...

thanks for the article. really gives the required basics for inheritance mapping

Anonymous said...

Good article..Useful for beginers.thanks.

Anonymous said...

Descriptions were provided in a nice and straight way. Likewise it is possible to understand the content with wasting minimal amount of time.
Wonderful work...

Mohan Rao said...

Incredible! Gives a clear concept view of Inheritance mapping.
Thanks a ton.

Anonymous said...

Good one.. I tried to understand from the material provided hibernate's official site.. but couldn't get much.
Your article really helped build my concepts about inheritance mapping :)

Thank you & keep up :))

Roberto Dungino said...

The first example :
When you use the discriminator. How to do create a Pojo file ?

Unknown said...

very good

Text Widget

Copyright © Vinay's Blog | Powered by Blogger

Design by | Blogger Theme by