• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database Inheritance in Hibernate

 
Rajesh Giriyappa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajesh Giriyappa:
...
is it the pojo classes inheritance.....?
....

YES
 
Rajesh Giriyappa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic