Get all updates of Java4s
Follow Us: Email Newsletter

Hibernate Inheritance: Table Per Concrete Class Hierarchy

Hibernate » On Jun 21, 2011 { 36 Comments } By Sivateja

Something like previous example but the changes are at mapping file only, and one more thing is..

x number of derived classes = x number of tables in the database

  • Once we save the derived class object, then derived class data and base class data will be saved in the derived class related table in the database
  • for this type we need the tables for derived classes, but not for the base class
  • in the mapping file we need to use one new element <union-subclass — >under <class —>

Required files_

  • Payment.java (Base class)
  • CreditCard.java (Derived class)
  • Cheque.java (Derived class)
  • ClientForSave.java (for our logic)
  • Payment.hbm.xml
  • hibernate.cfg.xml

 

Payment.java:

CreditCard.java:

Cheque.java:

ClientForSave.java

Payment.hbm.xml:

 

hibernate.cfg.xml:

Eclipse Output

In the database

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

36 Responses to “Hibernate Inheritance: Table Per Concrete Class Hierarchy”
  1. Yogesh says:

    Nice explaination java4s, thanks.

  2. Java4s says:

    @Yogesh

    Thanks for your feedback :-)

  3. Anupam says:

    Nicely distinguished between the three types of Hibernate Inheritance!

  4. Suresh says:

    This is nice explanation. But there is something shaking my mind.
    Since data is getting stored only in 2 tables i.e. Cheque and CreditCard; do we still need to have “Payment” table?
    I mean, why is there a table which is not getting used? Can we have some kind of implementation approach which will eliminate need of this table and still achieving this hierarchy?

    Thanks in advance for your reply.

  5. suresh says:

    Really good explanation.

  6. Java4s says:

    @suresh

    I am showing all the tables, just for your understanding purpose :-)

  7. krishna says:

    Hi SivaTej,

    I read some articles on mappings but finally i got clear picture while i am reading your blog.

    Thanks alot. Keep up your good work.

    KRISHNA

  8. Anasuya says:

    Hi Java4s thank you very much for the nice explanation,but can you please explain as when to use which hierarchy in real time?
    Thanks in advance…

  9. naveen says:

    good work….it is working…i did ur inheritence hierarchy programs…i understood…thank u very much….

  10. swapna says:

    Neat and clear explaination

  11. Hi Java4s u r doing a grt job :)

    but i also want to know what @Anasuya says

    when to use which Inheritance hierarchy in real time?

    Can u explain this ?

    Advance thanks

  12. Kiran N says:

    Hi Java4s Team,

    Its really nice explanation , thanks a lot Java4s team keep it up..

  13. Hi,
    I want to ask that in this example there is no table name provided for sub classes like CreditCard and Cheque, insted Payment Class is mapped with PAYMENT table that is of no use ? can some body explain me ?

  14. sarthi says:

    This is nice explanation. But there is something shaking my mind.
    Since data is getting stored only in 2 tables i.e. Cheque and CreditCard; do we still need to have “Payment” table?
    I mean, why is there a table which is not getting used? Can we have some kind of implementation approach which will eliminate need of this table and still achieving this hierarchy?

  15. hari krishna says:

    could you tell me the real time scenarious for all the types of inheritance mappings..?

  16. murali says:

    Thank you so much…..

  17. Raj says:

    I gone through your most of blogs from this site… the way you have written this blog is awesome. By Reading itself anyone can understand the concept. Thank you very much!!

  18. karthick says:

    why did you left the sub class table name in the …you have mentioned the base class table only…confused….please clear the doubt..ASAP …thanx in advance

  19. Prasad says:

    @karthick

    we do not left the sub classes with table names….otherwise error will rise…
    Here sir has taken same name for both table name and class name. In this situation it is optional to mention table name…

  20. Yogendra Joshi says:

    Nice explanation, short and simple. Easy to understand with these examples.

  21. P.Pradeep says:

    Thank you for your explanation Sivateja Kandula.

  22. Pranab Pradhan says:

    Detailed explanation, well done java4s!!! Keep up good work!!!

  23. raghava says:

    siva teja, u said only tables for derived classes but here we end up totally unused base class? didnt understand? why 3 tables?

  24. pavan says:

    Siva…. U said as per TablePerConcrete class require derived table to store all derived as well as base class, but in the mapping file, u mentioned table name for the base table PAYMENTS. Am i correct?

  25. G j says:

    for this type we need the tables for derived classes, but not for the base class ….I am not agree to you ….if tere will b no table for base class then if i only wants to save the data of a base class object where it will get saved ….

  26. krishna says:

    Excellent….

  27. Pranab says:

    Nice explanation. Thanks

  28. saroj kumar says:

    SIR is is it possible than Add next and prev.. butn on top also ..bcz if i wan to moove the next chapter thn we need to scroll every time

  29. Mohan Rao says:

    Hi JAVA4S Team,
    Thanks for sharing these details , it is helping us to explore more on hibernate.

    KINDLY CHECK THE EXPLANATIONS OF TPC (Table per Concrete class) and TPS (Table per Concrete class), please recheck it once.

    Regards
    Mohan

  30. Santhosh P says:

    Thanks for nice tutorial. I am just little bit confusing.

    In Table Per Concrete Class,

    x number of derived classes = x number of tables in the database or

    x number of classes = x number of tables in the database

    it is contracting with table per subclass. could you please explain

  31. vijay krishna says:

    In the mapping file subclass table name mapping is missing

  32. Pravin Dhas says:

    Very nice explanation for all the topics. Thank you very much java4s.

  33. Venkat says:

    Hi Java4s,

    I want to know usage of Main class Table(Payment) in Table Per Concrete Class Hierarchy.Because we are not saving any data.

  34. Arun says:

    Hi Java4s,

    Please verify Payment.hbm.xml file on that 13th and 17th line, actually you mention
    x no'of derived tables =x no'of tables but you are not creating table in <union-subclass > tag .

    if there is any modification please rectify that it is helpful for new learners.

    Thanks for your effort sir, your way of explanation is so nice ,

  35. sundar says:

    Hi java4s, can maintain the disadvantages of each hierarchy, and easily identify the what is the purpose of perticular hierarchy

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.