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

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 parttimefees;

    public String getParttimefees() {
        return parttimefees;
    }

    public void setParttimefees(String parttimefees) {
        this.parttimefees = parttimefees;
    }
    
}

hibernate.cfg.xml

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <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">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="student.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

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

student.hbm.xml

<hibernate-mapping>
<class name="pojo.Student" table="student" discriminator-value="stud">
        <id name="id" type="int">
            <column name="id"/>
            <generator class="increment"></generator>
        </id>
        <property name="sname" type="string">
            <column name="Name"/>
        </property>
        <property name="sdept" type="string">
            <column name="Department"/>
        </property>
        
        <joined-subclass name="pojo.Regular" table="student1">
            <key column="sid"></key>
            <property name="regularfees" type="string">
            <column name="regularfees"/>
            </property> 
        </joined-subclass>
        
        <joined-subclass name="pojo.Parttime" table="student2">
            <key column="sid"></key>
            <property name="parttimefees" type="string">
            <column name="partimefees"/>
            </property> 
        </joined-subclass>
        
   </class>
  

</hibernate-mapping>

save.java

package pojo;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

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.setParttimefees("20000");
            
            reg.setSname("Delta");
            reg.setSdept("Science");
            reg.setRegularfees("12000");
            
            session.save(pt);
            session.save(reg);
            session.save(stud);
            trans.commit();
        
    } 
}

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

 

 

By Sri

Leave a Reply

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