Tech I Enjoy Logo
Custom Search
   Log In    OR    Register  


  Home >> Hibernate >> Hibernate-mapping-class-hierarchy-table-per-subclass
Hibernate mapping for object hierarchy using table per
subclass way:
1. Persisting Parent child relationship (Inheritence in
domain objects) in RDBMS, using Hibernate as ORM tool.
This aspect of mapping object hierarchy is better understood
when I sat down in front of my Laptop and started implementing
a case study of my own.

Suppose, a person has a profile that contains his name, age, contact information etc. Contact can have phone, email, address, and Address constitutes of address line one, line two, line three, city, state, pin code etc. In this case, the domain classes, I can think of are Person Contact and Address class. Suppose, a person has a profile that contains his name, age, contact information etc. Contact can have phone, email, address, and Address constitutes of address line one, line two, line three, city, state, pin code etc. In this case, the domain classes, I can think of are Person Contact and Address class.
PersonContactAddress
---------------------
personIdcontactIdaddressId
nameemailIdaddrLine1
agephoneNumberaddrLine2
addrLine3
city
state
pin
In order to persist this object hierarchy, There are three possible ways (please refer http://www.hibernate.org for details), 1. Table per concrete class 2. Table per hierarchy 3. Table per subclass In this example I am going to use table per subclass. I think this type of mapping for object hierarchy is having better database normalization, as it has different tables for each domain class, like in this case, there are three tables for three domain classes , such as Person, Contact and Address and unlike 'table per hierarchy' where the entire object hirarchy is persisted in a single table, and can have duplicate records for different object states with a slight change in combination of field values for different sub classes/objects. Person class that is the parent class here in this example, has class tag, and Contact should be included inside Person as joined-subclass, and Contact joined-subclass has another joined-subclass for Address. So there are three tables as Person, Contact and Address, with the person_id as primary key in Person table, contact_id as primary key/foreign key to person_id in Person table. Similarly address_id is primary key /foreign key to contact_id in Contact table. One important point to be noted here is that all the three ids (person_id, contact_id and address_id) are same and one value only. As there is no Agregation or composition exist among these objects. The complete object hierarchy is identified by a single instance or key only, as multiple inheritance of classes is not possible in Java Technology. It is quite logical to assume that all the primary keys such as person_id, contact_id, and address_id should have same value. The client code is like the following, where the Address object can be used to store all the fields values of its super classes, Contact and Person: Address address = new Address(); address.setPersonId("P003"); address.setName("Girish"); address.setAge(23); address.setPassportNumber("PPP"); address.setContactId("P003"); address.setPhoneNumber(234323); address.setEmailId("share.understanding@gmail.com"); address.setAddressId("P003"); address.setAddrLine1("123 street"); address.setAddrLine2("Street Marg"); address.setAddrLine3("Fun"); address.setCity("Pune"); address.setState("Maharashtra");
If you like to share your comment/suggestions/feedback relating to this Page, you can do so by droping us an email at usingframeworks @ gmail . com with the subject line mentioning URL for this Page (i.e, /Hibernate-mapping-class-hierarchy-table-per-subclass.php) or use this LINK. As per this website's privacy policy, we never disclose your email id, though we shall post your comments/suggestions/feedback with your name (optional) and date on this Page. If you don't want your comments/suggestions/feedback to be shared in this Page, please mention so in your email to us. Thank you very much..... If anything missed out , please let me know at techienjoy at yahoo . com
Some of the other Articles you may would like to read :
Hibernate Bag Mapping Example :
class mapping using Bag Tag example using Hibernate
Framework and a simple to follow steps.
Hibernate Named Query Example :
Named Query markup using an example
and Hibernate Framework.
List of Examples on Hibernate :
List of example using Hibernate.
Hibernate Join Example :
Using Table join explained with an example
while using Hibernate Framework.
Hibernate Property Formula :
Hibernate Example on Property
Tag with ease to do code walk-through
Hibernate Example on Filter :
Example on using Filter using Hibernate Framework
to work with.
Hibernate class heirarchy mapping :
Hibernate Example on mapping
class hierarchy using various ways
of persisting into database
tables.
Hibernate Interview Questions :
Interview Questions on Hibernate with answer.
Hibernate Example on composite Primary key :
Example on using Hibernate Framework
to work with mapping using composite
Primary key.
Example on persisting Class Hierarchy :
Example on using Hibernate Framework
to persist Class Hierarchy into database.
Hibernate one to many mapping Example :
one to many mapping explained using an example
and Hibernate Framework.
Class Hierarchy Mapping Example :
class hierarchy mapping example using Hibernate
Framework and a simple to follow steps.
Hibernate one to one mapping Example :
one to one mapping explained using an example
and Hibernate Framework.
Hibernate Component Property :
Hibernate Example on Component 
with source code explained.
Hibernate Interceptor Example :
Example on using Interceptor using Hibernate Framework
with source code explained.
Hibernate Transaction on JBoss :
Explaining Transaction using Hibernate
on JBoss Application Server.
Hibernate Example on Filter Criteria :
Example on using Filter Criteria
using Hibernate Framework to work with.
Hibernate one to many mapping Example :
one to many mapping explained using an example
and Hibernate Framework.
Hibernate Many to Many Mapping Example :
Many to many mapping example using Hibernate
Framework and a simple to follow steps.
Hibernate Insert Update control :
Hibernate Example on controlling
insert and update attributes


References :
Tags: Hibernate Class Hierarchy Persist example
Tags: Hibernate composite primary key
Tags: Hibernate criteria filter example
Tags: Hibernate Filter Example
Tags: Hibernate Interceptor example
Tags: Hibernate Interview Questions
Tags: Hibernate join example
Tags: Hibernate Many to Many Mapping Example
Tags: Hibernate mapping bag
Tags: Hibernate mapping class hierarchy table per subclass
Tags: Hibernate named query markup
Tags: Hibernate One to Many mapping example
Tags: Hibernate One To One Mapping Example
Tags: Hibernate scenario one to many
Tags: Hibernate transaction jboss
Tags: Hibernate


For any of the content, if you would like to bring it to notice for removal from this web site, please write to this web site administrator @ EMAIL-ID,
with appropriate concern and supporting proof(s). After thorough review and if found genuine concern, we would take appropriate action and 
remove disputed content from this web site within 24 hours starting from the time it has brought to our notice.


The content provided in this page is not warranted and/or guaranteed by techienjoy.com. techienjoy.com is not liable for any negative 
consequences that may result/arise from implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis, without WARRANTIES or conditions of any kind, either express
or implied, including, without limitation, merchantability, or fitness for a particular purpose. You are solely responsible for determining 
the appropriateness of using or refering this and assume any risks associated with this.

This web site is optimized for learning and training. Examples might be simplefied to improve reading and basic understanding only. 
This web site content are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. 
In spite of all precautions taken to avoid any typo in these pages, there might be some issues like grammatical mistakes and typos 
being observed in these pages, techienjoy.com extends sincerest apologies to all our visitors for the same.

While using this web site, you agree to have read and accepted our terms of use and privacy policy.


Android Examples || Android Training

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions