Get all updates of Java4s
Follow Us: Email Newsletter

Hibernate Inheritance: Table Per Class Hierarchy

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

Here is the explanation and one example on hibernate table per class hierarchy, consider we have base class named Payment and 2 derived classes like CreditCard, Cheque

If we save the derived class object like CreditCard or Cheque then automatically Payment class object will also be saved into the database, and in the database all the data will be stored into a single table only, which is base class table for sure.


But here we must use one extra discriminator column in the database,  just to identify which derived class object we have been saved in the table along with the base class object,  if we are not using this column hibernate will throws the exception, see this example so that you will get one idea on this concept.

Required files_

  • (Base class)
  • (Derived class)
  • (Derived class)
  • (for our logic)
  • Payment.hbm.xml
  • hibernate.cfg.xml




Eclipse Output

In the database


  •,, are just pojo classes nothing to explain, but see in, i have inherited the
  • In this inheritance concept, mapping file is the central part, see in line number 10,  we added one new line  discriminator, after the id element just to identify which derived class object we have been saved in the table (see the oracle console once)
  • every thing has been saved in a single table

You Might Also Like

  ♦ About Author ♦

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.


31 Responses to “Hibernate Inheritance: Table Per Class Hierarchy”
  1. Mohammed Vaseem says:

    Hello Java4s,
    I have a doubt in these lines 10,13, 17 of Payment.hbm.xml.
    1. Why we have used discriminator-value as an attribute to the element?
    2. Can we take discriminator-value as a separate element? If yes/not why?

  2. Mohammed Vaseem says:

    Hello java4s,
    In real time scenarios, where we can use this concept?
    Just give a hint to analyze utilization of this concept.

  3. sivakumar says:

    Big thanks to Java4s,but i have a little confusion over the table columns.i have 2 queries.
    1)can u give the query for the table(How many columns it has we need to create the discriminator column here or not)?

    2)when we will use table per class in our App dev ?

    Please give me the reply asap.


  4. RajKirpal says:

    Hi, Java4s

    I think you might be aware but still i am just intimating you buddy that in all these three INHERITANCE base examples you have missed out to show files details.

    One solution is user can download the code and read the

    But as all other examples includes all the required files so i just think i should let you know.


  5. Java4s says:


    Thanks dude, really am shocked.

    Not sure why my friends missed this, i think they might given the reason in previous articles i guess.

    Any way our team realized this :-) really appreciate you(r) valuable time to inform us.

    We will update ASAP.

    Thank you so much RajKirpal.

  6. Mohammed Vaseem says:

    Hello Java4s Team,
    I understand all the programs of Hibernate Inheritance concepts but am getting Confusion in the topic names

    In my understanding:
    Table per class hierarchy means – one table for one class hierarchy.
    Table per subclass hierarchy – one table for one subclass.
    Table per concrete class hierarchy – one table for one concrete class. Here I did not understand meaning of concrete.

    Please explain in precise terms for easy understanding.

    Mohammed Vaseem

  7. hi sir,
    in .The column type always be “string” type or what else and how will decide the length of this.
    thanks sir.

  8. Arun says:

    how to retrieve all elements from database if we use table per hierarchy.
    i need the query to retrieve all data’s to display

  9. touch says:

    thanks for the tutorial, you rocks :-)

    Just one question: does this example runs fine? I’m getting “mappingException: unknown entity: str.CreditCard” although I’ve revised carefully and I think mapping is right. It happens on saving object. Any advice?

    Thanks a lot,

  10. smileee says:

    It works fine for me..

  11. Tarun says:

    Thanks for sharing

  12. ravi says:


    Please look into the UML Diagram once ,I think it is in reverse order

  13. Anil says:

    Hi Java4s,

    it is a best tutorial for Hibernate, all the concepta explanation is very simple and your way of presentation is superb.
    I have a doubt that where we can use these type of scenario’s in real time project development


  14. siva says:

    How to retrieve data from DB using this concept table per class.

  15. venki says:

    here we are referring to different colums for CreditCardType and ChequeType properties.
    If we want, can we have assigned to same column in database for both properties.

  16. abhilash says:

    hi i think is require dcolumn which was added in Payment.hbm.xml as descriminator field.

  17. gopal says:

    Can you please explain what is the use of ‘discriminator-value=”CC”‘ in subclass tag below is the example from yours. And where we can configure ‘CC’ or ‘CQ’ to identify this ?.


  18. Ramakrishna says:

    hi Shivateja,
    iam Ramakrishna
    can you please tell me what is the difference between one to many and many to one, and when we go for it Relationships..

  19. RAWIKIRRAN says:

    Thanks for giving the valuable notes.

  20. Swamy Gunti says:

    in payment.hbm.xml
    at line

    why we are use class name = “str.Payment”

  21. Kiran says:

    Hi Siva, This site is really helping me.. Thanks for this and keep it up..

  22. arpita says:

    hey pls explain me which realtime scenario which concept of inheritence will be used

  23. Prabu says:

    Hi Java4s,

    Why we need to use below lines in mapping. i didn’t see any explanation given in this section regarding this. can you please provide reason for this ?

  24. Priti says:


    I understood the concept but I have 1 doubt who will pass the the value for dcolumn ? There is no mapping for this column from java side

  25. Abhishek says:

    nice tutorial …..
    but i am getting the issue while running application.

    Exception in thread “main” org.hibernate.MappingException: invalid configuration
    at org.hibernate.cfg.Configuration.doConfigure(
    at org.hibernate.cfg.Configuration.configure(
    at com.abhi.SavePayment.main(
    Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 26; Document root element “hibernate-Configuration”, must match DOCTYPE root “hibernate-configuration”.

  26. Madhav says:


    Nice tutorial.

    Could you please give an example how to print the payment details using HQL.

  27. pradeep says:

    og4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread “main” org.hibernate.InvalidMappingException: Could not parse mapping document from resource payment.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(
    at org.hibernate.cfg.Configuration.parseMappingElement(
    at org.hibernate.cfg.Configuration.parseSessionFactory(
    at org.hibernate.cfg.Configuration.doConfigure(
    at org.hibernate.cfg.Configuration.doConfigure(
    at org.hibernate.cfg.Configuration.configure(
    at ClientForSave.main(
    Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
    at org.hibernate.cfg.Configuration.addInputStream(
    at org.hibernate.cfg.Configuration.addResource(
    … 6 more
    Caused by: org.dom4j.DocumentException: Error on line 22 of document : Content is not allowed in trailing section. Nested exception: Content is not allowed in trailing section.
    at org.hibernate.cfg.Configuration.addInputStream(
    … 7 more
    i am getting this error i dont know why it cause please sir i want some clarity about this can u please check the code and tell me about this exception

  28. Swetha says:

    Hi Java4s,

    Really u guys rocks….nice one .its very easy to understand and helping me alot

  29. Yuvaraj K says:

    Each time when I run the above example with different value sets it is updating in the DB instead of inserting new row. May I know why its happening like that???

  30. vipul yadav says:

    thanks dude……


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
  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.