Advertisement
Home > Hibernate Tutorial > mapping class hierarchy table per subclass > ExamplePlease log in to add or reply to any matter<- requires login or
RMI Example

Home > Hibernate Tutorial > mapping class hierarchy table per subclass > Example
Struts Tutorials:
Struts2 Tag CheckBoxList
, Checkbox, Iterator, IF
Struts2 Tag Library Example Struts2 Tiles Example Struts2 Tiles I18N Example Struts2 Questions Struts Tiles I18N Example Struts Eclipse MVC Struts2 Tags Struts2 Example and Tutorial Struts MVC Struts2 Validation
Hibernate Tutorials: Hibernate Case Study Class Hierarchy Persist Example Using Hibernate Interceptor Hibernate Questions with Answer Hibernate Many-to-Many Mapping Example Hibernate one-to-many Mapping Example Hibernate and ORM tools Spring Hibernate Example Hibernate SessionFactory Example Hibernate Mapping Class Hierarchy Hibernate Questions Hibernate SessionFactory Questions Spring Hibernate Example: Spring Hibernate Case Study

Written By : Amit
Title :
Interview Questions
Description : On Hibernate
More...


Written By : Amit
Title :
mapping class hierarchy table per subclass
Description : Example
More...


Written By : Amit
Title :
ways create Hibernate SessionFactory
Description : Example
More...


Written By : Amit
Title :
Spring Hibernate Integration example
Description : Example
More...


Written By : Amit
Title :
ORM Hibernate Best FIT
Description : Discussion
More...


Written By : Amit
Title :
One to Many mapping example
Description : Code
More...

Hibernate Tutorial,mapping-class-hierarchy-table-per-subclass,Example
Author : Amit
Date (Year/Month/Date): 2009-02-17 Mapping class Hierarchy to Table
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.
Advertisement
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. Person Contact Address ------- ------- ------- personId contactId addressId name emailId addrLine1 age phoneNumber addrLine2 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");
Author of this article/writeup has expressed his/her willingness
to help or guide users with any technical difficulties he/she faces while working with the example code environment setting up, running and resolving any such exception raised during compile or at runtime. You may ask for any technical doubt or seek technical help related to this article by using following form to reach for technical help from the Author for FREE. This article's Author shall be reading your request and responding within reasonable time (no resolution timeframe defined as such).

Commented By ->
Girish
Can you provide related mapping HBM file with appropriate example
for 1. Table per Hierarchy
2. Table per concrete class ???

Commented By ->
Girish
What are the basic difference between Hibernate component mapping 
and Hibernate Association mapping?
Please show me some example on this.
Please write your Comment on this Matter
(This will be visible if found suitable):
Name: *
Email (will not be displayed): *
Matter: *
33,37
Enter bigger number from above :*
Home > Hibernate Tutorial > mapping class hierarchy table per subclass > Example
Visitor/User submitted related resources:
(Visible upon review and approved by this site Administrator)
Referred By Name *:
Resource URL *: (e.g, URL should be starting with http://www.-----.---)
 
Resource Short Description *:
10,36
Enter bigger number from above : *

Please log in to add or reply to any matter<- requires login
Log in or Register
This List is generated as on 2009-07-12 (YYYY-MM-DD)
#Discuss-these : questions-and-answer : Interview-Questions-on-Java
#Question-on-Session-Factory-Creation : SessionFactory : Hibernate-Tutorial
#On-Hibernate : Interview-Questions : Hibernate-Tutorial
#Example : mapping-class-hierarchy-table-per-subclass : Hibernate-Tutorial
#Example : ways-create-Hibernate-SessionFactory : Hibernate-Tutorial
#Example : Spring-Hibernate-Integration-example : Hibernate-Tutorial
#Discussion : ORM-Hibernate-Best-FIT : Hibernate-Tutorial
#Code : One-to-Many-mapping-example : Hibernate-Tutorial
#code : Many-to-Many-Mapping-Example : Hibernate-Tutorial
#discussion : Interview-questions-answer : Hibernate-Tutorial
#example-code : Interceptor-use-log-SQL-statements : Hibernate-Tutorial
#code : Class-Hierarchy-Persist-example : Hibernate-Tutorial
#discussion : Case-study-Hibernate : Hibernate-Tutorial
#With-Answer : Hibernate-Interview-Questions : Hibernate-Tutorial
#Discussed : Hibernate-Interview-questions-answer : Hibernate-Tutorial
#Question : Hibernate-Transaction : Hibernate-Tutorial
#Example-Case-study : Contextual-Session : Hibernate-Tutorial
#Design-with-example-code : Struts2-Hibernate : Hibernate-Tutorial
#Hibernate-query : Hibernate-Criteria : Hibernate-Tutorial
#Hibernate-Criteria-Example : Hibernate-Filter-Example : Hibernate-Tutorial
#Hibernate-Filter-Example : Hibernate-Filter : Hibernate-Tutorial
#Hibernate-Sample : Hibernate-many-to-one : Hibernate-Tutorial
#Hibernate-Sample-Example : Hibernate-named-query : Hibernate-Tutorial
#Composite-Primary-Key : Hibernate-Composite-Key : Hibernate-Tutorial
#Mapping-Example : Hibernate-one-to-one : Hibernate-Tutorial
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved.
CONTACT    PRIVACY POLICY    DISCLAIMER

This web site provides some of the information about various technologies, example 
code, tips, tutorials etc. Like any printed matterials, content of these pages may 
become out of date over a period of time. Therefore all visitor/users of this web 
site are requested/advised to refer to the originating parties/sources for the 
latest changes and happenings for detailed information. This information is not 
intended to be a substitute for the original reference provided by the originating 
parties/sources.