Forums Register Login

Database Inheritance in Hibernate

+Pie Number of slices to send: Send
how Aggregation and specialization of database concept can be applied through
hibernate pojo class.

is it the pojo classes inheritance.....?

is there other way of achieving this....?
+Pie Number of slices to send: Send
 


how Aggregation and specialization of database concept can be applied through
hibernate pojo class


I'm not sure I follow what you are asking. Plain Old Java Objects achieve these in exactly the same way as any other object, the presence of Hibernate doesn't make them special.

Or am I misunderstanding your question?
+Pie Number of slices to send: Send
 

Originally posted by Rajesh Giriyappa:
...
is it the pojo classes inheritance.....?
....


YES
+Pie Number of slices to send: Send
Say for Simple Examples i have animal and dog hierarchy,

in which the dog inherits the animal class.

i can't see animal and dog as two seperate tables.
+Pie Number of slices to send: Send
Well Hibernate does suggest a pattern called "table per class hierarchy" which uses a discriminator column to tell Hibernate which type or object a row represents. Have a look in the docs, they cover this.

In my opinion, its an anti-pattern. Fine, it fixes what would be a slightly odd bit of OO modelling, but only by introducing poor modelling in your ER model (you have to introduce nullable properties for data that should never be null for example). My experience of database backed applications is that the data model is key, so don't weaken it lightly.
+Pie Number of slices to send: Send
Take a look at this class hierarchy:



Using a single class inheritance mapping strategy, you'd have a talbe that looks like this:



You'd get a database that looks something like this, after a single instance of each class was created:



Alternatively, a joined table inheritance mapping would generate tables that look like this:



And a single table per class mapping strategy would give you tables that looked like this:



Sorry about posting such big images. I just think that this visually helps you see how mapping strategies differ in how the data is managed.

These images were taken from this tutorial on Inheritance Mapping with Hibernate and JPA Annotations:

Mapping Inheritance with Hibernate and JPA - An Example

-Cameron McKenzie
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2764 times.
Similar Threads
Relations like Many to One in Hibernate and Database?
Hibernate:What is the significance of not null
Hibernated Pojo Generator using config.xml for Oracle database
converting database tables into POJO
How to return data from a database table using Apache CXF web service?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:25:32.