Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@inheritance annotation

 
rani vini
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain me @inheritance annotation ans the following types
TABLE_PER_CLASS
JOINED
SINGLE_TABLE (the default)
 
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
Heh...you may as well have asked for my tutorial!

Understanding Hibernate and JPA Inheritance Mapping Tutorial


Imagine you wanted to map this inheritance hierarchy:



There are multiple ways to do it, with various drawbacks and benefits. But essentially, these three options will create three different database structures:

ONE TABLE PER CLASS



JOINED TABLES



SINGLE TABLE



I'm a fan of Single Table, but your mileage may vary. Here's an example of multiple instances being saved to a single table:






Example Tutorial of Mapping Inheritance with the Java Persistence API (JPA)

Hope that helps!

-Cameron McKenzie








 
rani vini
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I define the POJO classes for ancestor,parent and child.
Can anyone tell that? I tried that and I am getting following exception

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.AnnotationException: Unable to define/override @Id(s) on a subclass:
test
 
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
Well, if the parent class has the id, the client classes don't really need it, because it's inherited. The magic on inheritance at work, right there in a Hibernate/JPA application!

-Cameron McKenzie
 
rani vini
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have Table employee with ( int id,String name,Date startDate)

And then company_employee with (int id, int emp_id,int vacation) ---
where id is a unique identity field
emp_id is the forienkey..which refers the employee id
and vacation is new column.

Here company_employee should get all the info from employee like name and start date.

Can we achieve this through inheritance?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic