Table per Subclass – Hibernate

Table per Subclass – Hibernate
Table per Subclass one table for base class is created and another table is created for all the sub-classes. The primary key of base class is made as Foreign key for sub-class tables.
This code is tested using NetBeans 8.0.2.

1) File –> New Project –> Java –> Java Web application
2) Give application name (“hibernate”)
3) Last page select Hibernate Framework and choose the database.

In Source Packages create a folder “pojo” and create 3 java class,
1) Student.java
2) Regular.java
3) Parttime.java
4) save.java

Student.java

Regular.java

Parttime.java

hibernate.cfg.xml

Right click source packages –>New–>Hibernate Mapping Wizard and Rename it as “student.hbm”

student.hbm.xml

save.java

Output:

 Student Table

id Name Department
1 Beta Models
2 Delta Science
3 Alpha Robotics

Student 1 table

sid regularfees
2 12000

 Student 2 table

sid partimefees
1 20000

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *