File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes polymormsim related demos Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "polymormsim related demos " Watch "polymormsim related demos " New topic

polymormsim related demos

umesh rawat
Ranch Hand

Joined: Mar 03, 2007
Posts: 41

Hi all
if someone has some inheritance and polymorphim related hibernate examples .
please send me at


Umesh rawat <br />Rmw -2019
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
1. Inheritance and polymorphism
Suppose that a bookshop is selling CDs and DVDs to the customers. We first create a class
Disc and provide a mapping definition for it.

Mapping file as:

1.1. Inheritance

There are mainly two kinds of discs we are selling, audio discs and video discs. Each kind has
different properties from the other. From the orient-oriented perspective, we should model these two
kinds of discs, AudioDisc and VideoDisc, as “subclasses” of Disc to represent an “is-a” relationship.
In Java, we use the “extends” keyword to define subclass of a class. Reversely, the class Disc is
called the “superclass” or “parent class” of AudioDisc and VideoDisc.

The relationship from a subclass (e.g. AudioDisc and VideoDisc) to its parent class (e.g. Disc) is
called “inheritance”. All the subclasses and their parents make up a “class hierarchy”.

In relational model, there’s not a concept of inheritance. That means we must define a mapping
mechanism to persist the inheritance relationships of our object model. However, Hibernate is
providing several strategies to make it easy.

1.2. Polymorphism

For our disc hierarchy, we can use the following query to find all the discs in our system, no matter
audio or video ones. This kind of query is called “polymorphic query”.

Suppose we would like to support disc reservation for our online bookshop. We create a class
Reservation and define a many-to-one association to the Disc class. As the concrete class of the disc
may be AudioDisc or VideoDisc and it can only be determined at runtime, this kind of association is
called “polymorphic association”.

Mapping file as:

2. Mapping inheritance

Hibernate is providing three main strategies for mapping inheritance relationships. Each of them has
particular advantages and disadvantages.

2.1. Table per class hierarchy

This strategy assigns a single table to store all the properties of all the subclasses/non-subclasses
within a class hierarchy. In addition, a special column called “discriminator” is used to distinguish
the type of the object.

The main advantage of this strategy is simple and efficient, especially for polymorphic queries and
associations, since one table contains all the data and no table join is required. However, this
strategy has a fatal limitation that all the properties in subclasses must not have not-null constraint.

2.2. Table per subclass

This strategy uses one table for each subclass and non-subclass. It defines a foreign key in the table
of subclass that references the table of its parent. You can imagine there’s a one-to-one association
from the subclass to its parent.

This strategy has no limitation on not-null constraint but it is less efficient, since several tables need
to be joined for retrieving a single object. For polymorphic queries and associations, more tables
need to be joined.

2.3. Table per concrete class

The last strategy is to assign each concrete class an isolated table that duplicates all the columns for
inherited properties. Notice that we can no longer use identity id generation since the id need to be
unique across several tables. This strategy is not efficient for polymorphic queries and associations
since several tables need to be navigated for retrieving the objects.

Brainbench Java 2.0 Fundamentals, J2EE 1.4
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Why don't you just link the hibernate reference documentation?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Bauke Scholtz wrote:Why don't you just link the hibernate reference documentation?

A good suggestion, indeed. However, even a reference link would serve the purpose. Thanks.
Prathamesh Gaddam
Ranch Hand

Joined: Feb 18, 2008
Posts: 58
Hi Ranchers,

Bhagat, your example was very helpful to workout my first demo with inheritance. Thank you for it!

2.1. Table per class hierarchy .....

I need to check the how schema will be generated for subclasses extending the sublass (using ""). In simple, I want to extend AudioDisc class as:

Towards Disc.hbm.xml
How to code the mapping file?
Can we use multiple discriminator in a <class></class>? Or a discriminator in the <subclass></subclass>?
Can we have <subclass/> within <subclass></subclass>? How can i use this features?
Or can I use, already defined <subclass/> as a <class>? For example w.r.t above example can we use as <class name="AudioDisc">as:

Request: I regret if any question sounds crazy/weired, logically!

Thank you in advance!
Thirupathi Neela

Joined: Feb 26, 2009
Posts: 13
Hi Ranchers!
This example is very much helpful in understanding mapping with inheritance classes.
How do we map the same example using annotations?


kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1427
What is the difference between table per sub-class and table per concrete class(still i am not clear)? Can you explain with example? Both the types use 'join' for combining values from different tables.
I agree. Here's the link:
subject: polymormsim related demos
It's not a secret anymore!