Table Per Hierarchy – Hibernate
Table per class hierarchy is mapping all values in one table. The code given below is tested in NetBeans.
1) In Netbeans file–>new project –>java –>java webapplication
2) Give webapplication a name(“TPH”) –> Next
3) Select Hibernate Framework and choose the database where you want to create this table
Create 3 java files,
Student.java
Regular.java
Parttime.java
Create a folder named “pojo” inside source packages
Student.java
public class Student { String sname; String sdept; int id; public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSdept() { return sdept; } public void setSdept(String sdept) { this.sdept = sdept; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
Regular.java
public class Regular extends Student { private String regularfees; public String getRegularfees() { return regularfees; } public void setRegularfees(String regularfees) { this.regularfees = regularfees; } }
Parttime.java
public class Parttime extends Student { private String partimefees; public String getPartimefees() { return partimefees; } public void setPartimefees(String partimefees) { this.partimefees = partimefees; } }
hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="hibernate.hbm2ddl.auto">update</property> //update -->Checks if table exists, if table exists values get updated or new table will get created <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">"url"</property> <property name="hibernate.connection.username">"username"</property> <property name="hibernate.connection.password">"password"</property> <mapping resource="student.hbm.xml"/> </session-factory> </hibernate-configuration>
Right Click –> New–> Hibernate Mapping Wizard –>student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="pojo.Student" table="student" discriminator-value="stud"> <id name="id" type="int"> <column name="id"/> <generator class="increment"></generator> </id> <discriminator column="type" type="string"></discriminator> <property name="sname" type="string"> <column name="Name"/> </property> <property name="sdept" type="string"> <column name="Department"/> </property> <subclass name="pojo.Regular" discriminator-value="Regular"> <property name="regularfees" type="string"> <column name="regularfees"/> </property> </subclass> <subclass name="pojo.Parttime" discriminator-value="pt"> <property name="partimefees" type="string"> <column name="partimefees"/> </property> </subclass> </class> </hibernate-mapping>
Inside pojo create a javaclass
Save.java
public class Save { public static void main(String args[]) { Transaction trans=null; Session session=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory().openSession(); trans=session.beginTransaction(); Student stud=new Student(); Parttime pt=new Parttime(); Regular reg=new Regular(); stud.setSname("Alpha"); stud.setSdept("Robotics"); stud.setId(2); pt.setSname("Beta"); pt.setSdept("Models"); pt.setPartimefees("20000"); reg.setSname("Delta"); reg.setSdept("Science"); reg.setRegularfees("12000"); session.save(pt); session.save(reg); session.save(stud); trans.commit(); } }
Output:
id | type | Name | Department | regularfees | partimefees |
1 | pt | Beta | Models | null | 20000 |
2 | Regular | Delta | Science | 12000 | null |
3 | stud | Alpha | Robotics | null | null |
[…] using Annotations (Single Table) We have already seen an example for Table per hierarchy (here). In this article we are going to see how Table per Hierarchy can be implemented in Hibernate using […]