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