Problem of Inheritance:
in the relational environment does not directly support inheritance, however, we can consider the following three choices.
1.
Table per class hierarchy
2.
Table for
subclass.
3.
Table for
concrete class.
Table per class hierarchy:
In this process we can map
the entire class hierarchy to a single table. This table includes columns ofr
the properties of al classes in the hierarchy.
Benefits
:
This is basic and easier to use,
and efficient to compare other two
approaches.
Problems:
The columns for properties
declared by subclasses must be declared to accept null
Values,
these columns cannot be declared with NOT NULL constraint.
Database:
create
table employeedetails(empno int(4),category char(16),ename char(16),annualSal
int(4))
Ex3:
//this program will
describes – Table per class hierarchy
Employee.java
package madhav;
public class
Employee {
private int empno;
private
String ename;
public int getEmpno() {
return empno;
}
public void setEmpno(int
empno) {
this.empno = empno;
}
public
String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}
SalariedEmployee.java
package madhav;
public class
SalariedEmployee extends Employee{
public double annualSal;
public double
getAnnualSal() {
return annualSal;
}
public void
setAnnualSal(double annualSal) {
this.annualSal = annualSal;
}
public String toString()
{
return "salariedEmployee";
}
}
employee.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="madhav.Employee" table="employeedetails">
<id name="empno" type="int"
column="empno" >
<generator class="increment"/>
</id>
<discriminator column="category"></discriminator>
<property name="ename">
<column name="ename" />
</property>
<subclass name="madhav.SalariedEmployee" discriminator-value="salaried">
<property name="annualSal"></property>
</subclass>
</class>
</hibernate-mapping>
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">
<!-- Generated by MyEclipse Hibernate
Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mysql</property>
<property name="connection.username">madhav</property>
<property name="connection.password">madhav</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
TestCase.java
package madhav;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.cfg.*;
import org.hibernate.*;
import org.apache.log4j.*;
public class TestCase {
/**
* @param args
*/
SessionFactory factory;
public void
createEmp(SalariedEmployee sm)
{
Session session=null;
Transaction tx=null;
try
{
session
=factory.openSession();
tx=session.beginTransaction();
System.out.println("break1 in cd");
session.save(sm);
tx.commit();
System.out.println("record inserted");
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
public static void
main(String[] args) {
// TODO
Auto-generated method stub
System.out.println("first");
TestCase t=new
TestCase();
Configuration
cfg=new Configuration();
cfg.configure();
//cfg.addResource("employee.hbm.xml");
//cfg.addResource("personaldetails.hbm.xml");
System.out.println("break 2");
t.factory=cfg.buildSessionFactory();
SalariedEmployee
se=new SalariedEmployee();
se.setEname("madhav");
se.setAnnualSal(9600);
t.createEmp(se);
System.out.println("execution over");
}
}
Output –console:
Output-sql-prompt:
Related Topics:
- hibernate-basic-example(xml-based)
- hibernate-inheritance-tableforclass-hierarchy(xml-based)
- hibernate-inheritance-joinsubclass-(xml-based)
- hibernate-inheritance-tableforconcreteclass(xml-based)
- hibernate-onetoone-relation(xml-based)
- hibernate-onetomany-relation(xml-based)
- hibernate-manytomany-relation(xml-based)
- hibernate-basic-example(Annotation-based)
- hibernate-inheritance-tableforclass-hierarchy(Annotation-based)
- hibernate-inberitance-joinsubclass-(Annotation-based)
- hibernate-inheritance-tableforconcreteclass(Annotation-based)
- hibernate-onetoone-relation(Annotation-based)
- hibernate-onetomany-relation(Annotation-based)
- hibernate-manytomany-relation(Annotation-based)
Hi thank you for sharing your blog keep it up.Uttarainfo Provides Web development J2EE Training and Android Trainig in Bangalore(Jayanagar,Rajajinagar).Uttarainfo develops web development and Android apps.hibernate training classes in bangalore india
ReplyDelete