Get all updates of Java4s
Follow Us: Email Newsletter

Hibernate Inheritance: Table Per subClass Hierarchy

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

This is also just like previous example, but some changes are there, in table per class hierarchy all the data was saved in a single table but here,

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

If we save the CreditCard class object, then first hibernate will saves the data related to super class object into the super class related table in the database and then CreditCard object data in CreditCard related table in the database, so first base class data will be saved

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

All are same but mapping file is different than previous example..

Payment.java:

CreditCard.java:

Cheque.java:

ClientForSave.java

Payment.hbm.xml:

 

hibernate.cfg.xml:

Eclipse output

In the database

Notes:

  • In the mapping file,  <key –> element is because,  once we save the derived class object, then hibernate will first save the baseclass object then derived class object right ..!, so at the time of saving the derived class object hibernate will copy the primary key value of the base class into the corresponding derived class, see in the above output 10 copied into dummy1 column of CreditCard table and 11 copied into Dummy2 column of the cheque table
  • that’s it friends, nothing to explain….   ;)

 

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

22 Responses to “Hibernate Inheritance: Table Per subClass Hierarchy”
  1. Karry Glenn says:

    Some really prize blog posts on this website , bookmarked .

  2. java4s says:

    Thank you Glenn….!!!!

  3. JOHN says:

    awesome website yar……… i recommended to every one go throw this …….. indepth explanations

  4. Java4s says:

    @John Thank you

  5. Anand says:

    I got an error
    Could not synchronize database state with session
    org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

  6. Jessy says:

    Thanks..its a great help for me :)

  7. Mani says:

    This Concept of Table for sub class what the explination you have given when it is executed in Eclipse it is giving

    Exception in thread “main” org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update…

  8. Mani says:

    To Anand,

    You are Error because you are using the database of previous example ie payment table contains only 2 columns not 4 as in the previous example so it may be a chance of constraint error to this particular example

  9. kadir says:

    Good explanation. Thanks.

    I have a question here.

    As each class in java has base class as Object class,where this object is getting saved every time?

  10. really awesome… Nice explanation

    Lots of thanks

  11. siddu says:

    wow!… what a beautiful explanation
    i god mad while reading your explanation.. we can easily understand for big concepts…so nice….
    if you have any updates, plz send into my mail: rajjmucharla@gmail.com

    thanks,

  12. karthick says:

    the best tutorial site ever seen….awesome… :D

  13. venki says:

    Thanks for the clear cut explanation..

    You stated “so at the time of saving the derived class object hibernate will copy the primary key value of the base class into the corresponding derived class”

    Here Question is.. If we have the composite primary key than should we create two dummy columns in each sub classes or any approach. Please explain.

    Thanks.

  14. Uday says:

    Good explanation. Thanks.

    I have a question here.

    As each class in java has base class as Object class,where this object is getting saved every time?

  15. Muniraju M.K says:

    really really very very very good website blog….i insisted every one to refer this blog…….

  16. venkat says:

    please give one exmple per load operation on table per class,table per subclass and table concert class

  17. raghava says:

    hi siva,
    what if parent class is abstract?

  18. ajay tiwari says:

    hell sir,
    I have a question here
    when we run the program again give the following exception

    Exception in thread “main” org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2250)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2678)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    at str.ClientForSave.main(ClientForSave.java:33)
    Caused by: java.sql.BatchUpdateException: ORA-00001: unique constraint (SYSTEM.SYS_C004176) violated

    at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
    at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10657)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    … 14 more

  19. ashish bhatt says:

    This website gave me too much help. Day by day i’m learning myself, my confidence increasing automatically bcoze of you. i owe you thank you sir

    Thanks and regard
    Ashish

  20. Amin says:

    Hi
    very Good. Thanks.

  21. Abhishek says:

    i got issue ……

    Cannot add or update a child row: a foreign key constraint fails (abhi/credit, CONSTRAINT FK78CA9719C05CE0B0 FOREIGN KEY (dummy1) REFERENCES pmt (pid))

  22. Shilpi Agrawal says:

    Please help me,
    Running above example, I am getting following error:
    Object saved successfully…..!!
    Hibernate: insert into PAYMENT (amt, pid) values (?, ?)
    Exception in thread “main” org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
    at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2228)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:52)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at shilpi.ClientForSave.main(ClientForSave.java:33)
    Caused by: java.sql.BatchUpdateException: Field ‘dcolumn’ doesn’t have a default value
    at com.mysql.jdbc.SQLError.createBatchUpdateException(SQLError.java:1158)
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1773)
    at com.mysql.jdbc.PreparedStatement.executeBatchInternal(PreparedStatement.java:1257)
    at com.mysql.jdbc.StatementImpl.executeBatch(StatementImpl.java:959)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
    … 14 more
    Caused by: java.sql.SQLException: Field ‘dcolumn’ doesn’t have a default value
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1751)
    … 18 more

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.