Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

polymormsim related demos

 
umesh rawat
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi all
if someone has some inheritance and polymorphim related hibernate examples .
please send me at -umeshrawat30@gmail.com

thanks



 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you just link the hibernate reference documentation?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 "hibernate.hbm2ddl.auto"). 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
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers!
This example is very much helpful in understanding mapping with inheritance classes.
How do we map the same example using annotations?

regards
 
kri shan
Ranch Hand
Posts: 1460
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic