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.
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).
|
|