by December 7, 2007 0 comments

Inheritance mapping and support for polymorphic queries was a crucial feature
missing in the traditional EJB 2.1 Entity Beans model. Hibernate, an Open Source
object-relational mapping and persistence framework provides three strategies
for entity inheritance. In the first of the series of two articles, we explore a
simple inheritance mapping strategy used in Hibernate ORM.

Direct Hit!

Applies To:
Adv Java Developers
Map Java “IS-A” relationship to underlying d/b tables with
Hibernate’s entity inheritance strategies
Primary Link: hib_docs/ reference/en/html/inheritance.html
Hibernate Inheritance Mappings

Table per class hierarchy
As the name suggests, in ‘Table per class hierarchy’ approach, there exists
only a single table in the database which represents each class in the
hierarchy. To demonstrate different mapping strategies in Hibernate, we’ll use
the MedTracker app. We shall see how Hibernate can be used to map the ‘IS-A’
association to a single database table.

The root of the inheritance hierarchy is defined by the Person class. We’ll
define a class called Patient and have it extend the Person base class and
define an Employee class that extends the Patient class. Employees will be
treated with a special discount when they are admitted.

This is by far the most simple strategy to implement. The persistence engine
does not have to do any complex joins, unions, or sub selects when loading the
entity or when traversing a polymorphic relationship, because all data is stored
in one table. As an illustration, consider the following code snippet taken from
the mapping file to implement table per class hierarchy strategy.

<hibernate-mapping package="com.pcquest.medtracker.orm"
<class name="Person" table="PERSON">
<id name="id" type="long" column="PERSON_ID">
<generator class="native"/>
<discriminator column="PERSON_TYPE" type="string"/>
<property name="firstName" column="F_NAME"/>
<property name="lastName" column="L_NAME"/>
<property name="gender" column="GENDER"/>
<property name="age" column="AGE"/>
<subclass name="Patient" discriminator-value="PATIENT">
<property name="diagnosis" column="DIAGNOSIS"/>
<property name="doa" column="DATE_OF_ADMISSION" type="timestamp"/>
<subclass name="Employee" discriminator-value="EMPLOYEE">
<property name="medicalPolicyNo" column="POLICY_NO"/>

As shown in this code, Hibernate uses the direct field access strategy
mechanism. This means that you don’t have to get into the mess of providing a
getter/setter for every persistent property in the entity class. Hibernate can
directly access private fields using the Reflection API. The single table per
class hierarchy mapping requires an additional discriminator column. This column
identifies the type of entity being stored in a particular row of the database

The single database table which Hibernate will create on the fly is named
PERSON. The <subclass> tag is used to map subclasses. Later, you’ll notice that
the PERSON table possesses all the columns necessary to store every attribute of
the Person class hierarchy.

Hibernate’s DB
schema generator tool will create only a single table in the database called
PERSON. This single DB table contains all persistent properties from every
class in the inheritance tree

Client application
The client code is simple. We’ll use a static singleton to startup
Hibernate. The startup includes building a global SessionFactory object. A
SessionFactory can open up new Sessions. A Session represents a single-threaded
unit of work, while the SessionFactory is a thread-safe global object,
instantiated once.

Session session =
Person p=new Employee("Satish","Sharma","Male",26,"Common Cold",new
java.util.Date(),new Long(1));;

The single table per class hierarchy scales better than the other two
strategies that we’ll discuss in the next issue. However, there’s one big
limitation of this mapping strategy- columns declared by the subclasses may not
have NOT NULL constraints. In the next article, we’ll explore the remaining two
inheritance mapping strategies-table per subclass and table per concrete class.

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.