Hibernate-Inheritance Mapping with XML
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">minu</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/inheritmapping/hibernate/hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Student.java
package com.inheritmapping.hibernate;
public class Student {
private int sid;
private String sname;
private String city;
private String status;
private double totalfee;
public Student(){}
public Student(int sid, String sname, String city, String status,
double totalfee) {
this.sid = sid;
this.sname = sname;
this.city = city;
this.status = status;
this.totalfee = totalfee;
}
public Student( String sname, String city, String status,
double totalfee) {
this.sname = sname;
this.city = city;
this.status = status;
this.totalfee = totalfee;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public double getTotalfee() {
return totalfee;
}
public void setTotalfee(double totalfee) {
this.totalfee = totalfee;
}
}
package com.inheritmapping.hibernate;
public class OldStudent extends Student{
private String ocompany;
private String ocemail;
private double octc;
public OldStudent(){}
public OldStudent(int sid, String sname, String city, String status,
double totalfee, String ocompany, String ocemail, double octc) {
super(sid, sname, city, status, totalfee);
this.ocompany = ocompany;
this.ocemail = ocemail;
this.octc = octc;
}
public OldStudent( String sname, String city, String status,
double totalfee, String ocompany, String ocemail, double octc) {
super(sname, city, status, totalfee);
this.ocompany = ocompany;
this.ocemail = ocemail;
this.octc = octc;
}
public String getOcompany() {
return ocompany;
}
public void setOcompany(String ocompany) {
this.ocompany = ocompany;
}
public String getOcemail() {
return ocemail;
}
public void setOcemail(String ocemail) {
this.ocemail = ocemail;
}
public double getOctc() {
return octc;
}
public void setOctc(double octc) {
this.octc = octc;
}
}
CurrentStudent.java
package com.inheritmapping.hibernate;
public class CurrentStudent extends Student{
private double feebal;
private String timings;
private String branch;
public CurrentStudent(){}
public CurrentStudent(int sid, String sname, String city, String status,
double totalfee, double feebal, String timings, String branch) {
super(sid, sname, city, status, totalfee);
this.feebal = feebal;
this.timings = timings;
this.branch = branch;
}
public CurrentStudent( String sname, String city, String status,
double totalfee, double feebal, String timings, String branch) {
super( sname, city, status, totalfee);
this.feebal = feebal;
this.timings = timings;
this.branch = branch;
}
public double getFeebal() {
return feebal;
}
public void setFeebal(double feebal) {
this.feebal = feebal;
}
public String getTimings() {
return timings;
}
public void setTimings(String timings) {
this.timings = timings;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
}
WeekdayStudent.java
package com.inheritmapping.hibernate;
public class WeekdayStudent extends CurrentStudent{
private String qualification;
private String percentage;
private int yoe;
public WeekdayStudent(){}
public WeekdayStudent(int sid, String sname, String city,
String status, double totalfee, double feebal, String timings,
String branch, String qualification, String percentage, int yoe) {
super(sid, sname, city, status, totalfee, feebal, timings, branch);
this.qualification = qualification;
this.percentage = percentage;
this.yoe = yoe;
}
public WeekdayStudent( String sname, String city,
String status, double totalfee, double feebal, String timings,
String branch, String qualification, String percentage, int yoe) {
super( sname, city, status, totalfee, feebal, timings, branch);
this.qualification = qualification;
this.percentage = percentage;
this.yoe = yoe;
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
public String getPercentage() {
return percentage;
}
public void setPercentage(String percentage) {
this.percentage = percentage;
}
public int getYoe() {
return yoe;
}
public void setYoe(int yoe) {
this.yoe = yoe;
}
}
WeekendStudent.java
package com.inheritmapping.hibernate;
public class WeekendStudent extends CurrentStudent{
private String wcompany;
private String wcemail;
private double wctc;
public WeekendStudent(){}
public WeekendStudent(int sid, String sname, String city, String status,
double totalfee, double feebal, String timings, String branch,
String wcompany, String wcemail, double wctc) {
super(sid, sname, city, status, totalfee, feebal, timings, branch);
this.wcompany = wcompany;
this.wcemail = wcemail;
this.wctc = wctc;
}
public WeekendStudent( String sname, String city, String status,
double totalfee, double feebal, String timings, String branch,
String wcompany, String wcemail, double wctc) {
super( sname, city, status, totalfee, feebal, timings, branch);
this.wcompany = wcompany;
this.wcemail = wcemail;
this.wctc = wctc;
}
public String getWcompany() {
return wcompany;
}
public void setWcompany(String wcompany) {
this.wcompany = wcompany;
}
public String getWcemail() {
return wcemail;
}
public void setWcemail(String wcemail) {
this.wcemail = wcemail;
}
public double getWctc() {
return wctc;
}
public void setWctc(double wctc) {
this.wctc = wctc;
}
}
hibernate.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 package="com.inheritmapping.hibernate">
<class name="Student" table="student1">
<id name="sid" column="sid" type="int">
<generator class="increment"/>
</id>
<property name="sname" column="sname" type="string"/>
<property name="city" column="city" type="string"/>
<property name="status" column="status" type="string"/>
<property name="totalfee" column="totalfee" type="double"/>
<joined-subclass name="CurrentStudent" table="cstudents">
<key column="sid"/>
<property name="feebal" type="double"/>
<property name="timings" />
<property name="branch" />
<joined-subclass name="WeekdayStudent" table="wdstudents">
<key column="sid"/>
<property name="qualification" column="quali"/>
<property name="percentage" />
<property name="yoe" type="int"/>
</joined-subclass>
<joined-subclass name="WeekendStudent" table="westudents">
<key column="sid"/>
<property name="wcompany"/>
<property name="wcemail" />
<property name="wctc" type="double"/>
</joined-subclass>
</joined-subclass>
<joined-subclass name="OldStudent" table="ostudents">
<key column="sid"/>
<property name="ocompany"/>
<property name="ocemail" />
<property name="octc" type="double"/>
</joined-subclass>
</class>
</hibernate-mapping>
HibernateUtil.java
package com.inheritmapping.hibernate;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
static SessionFactory factory;
static
{
try
{
Configuration cfg=new Configuration();
cfg=cfg.configure();
factory=cfg.buildSessionFactory();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory()
{
return factory;
}
}
Lab.java
package com.inheritmapping.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class Lab {
public static void main(String[] args) {
try
{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Student st=new Student("Minu","blr","active",20000);
session.save(st);
CurrentStudent cst=new CurrentStudent("praveen","cochin","active",20000,0,"7am-10pm","ECE");
session.save(cst);
OldStudent ost=new OldStudent("alen", "dufai", "not active", 19000, "mak", "a@mak", 5);
session.save(ost);
WeekdayStudent wdst=new WeekdayStudent("anu", "blr", "active", 19000, 1000, "8am-10am", "ECE", "Btech", "80", 5);
session.save(wdst);
WeekendStudent west=new WeekendStudent("arun", "mumbai", "not active", 17000, 3000, "4pm-8.30pm", "CSE", "Wipro", "Arun@wipro", 5);
session.save(west);
session.save(st);
tx.commit();
session.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Comments
Post a Comment