SlideShare a Scribd company logo
1 of 14
Download to read offline
Copyright © 2010 Purple Desk All Rights Reserved. PurpleDesk.and its logo are Trademarks of Purple Desk.www.javafasttrack.com
Java Fast Track
www.javafasttrack.com
Inheritence Mapping
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com 2
public class TbPersonDescr {
// Fields
private Short id;
private String name;
private String address;
private String discriminator;
}
public class Student extends
TbPersonDescr{
private String rollNo;
public class Employee extends
TbPersonDescr {
private String empNo;
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
1 Table per hierarchy(using
Discriminator)
CREATE TABLE TB_PERSON_DESCR (
ID SMALLINT NOT NULL,
NAME VARCHAR ( 50 ) ,
ROLL_NO CHAR ( 10 ) ,
EMP_NO VARCHAR ( 50 ),
ADDRESS VARCHAR ( 50 ) ,
DISCRIMINATOR CHAR ( 5 ) ,
PRIMARY KEY(ID)
)
;
WS:hibernatedemo
Proj:oct9inheritencedesc
DB:jftsampledb
3
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
Step1:Create the table
Step2:Create a project,add hibernate capabilities
end up with a sessionfactory & hibernate.cfg.xml
step3:
POJO classes ,Person(Super),Employee ,Student
step4:Code the mapping file
hibernate.cfg.xml
<mapping resource="com/jft/demo/TbPersonDescr.hbm.xml" />
Write a Test Program & test 4
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
<hibernate-mapping>
<class name="com.jft.demo.TbPersonDescr" table="tb_person_descr"
catalog="jftsampledb">
<id name="id" type="java.lang.Short">
<column name="ID" />
<generator class="assigned" />
</id>
<discriminator column="DISCRIMINATOR"></discriminator>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" not-null="false" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" length="50" not-null="false" />
</property>
<subclass name="com.jft.demo.Student" discriminator-value="STUD">
<property name="rollNo" type="java.lang.String">
<column name="ROLL_NO" length="10" not-null="false" />
</property>
</subclass>
<subclass name="com.jft.demo.Employee" discriminator-value="EMPL">
<property name="empNo" type="java.lang.String">
<column name="EMP_NO" length="50" not-null="false"/>
</property>
</subclass>
</class>
</hibernate-mapping> 5
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
Session sess = HibernateSessionFactory.getSession();
sess.beginTransaction();
Student stu = new Student();
stu.setId(new Short("3"));
stu.setName("Jimmy");
stu.setRollNo("S002");
stu.setAddress("BTM Layout");
sess.save(stu);
Employee emp = new Employee();
emp.setId(new Short("4"));
emp.setEmpNo("E002");
emp.setName("Peter");
emp.setAddress("Jaya Nagar");
sess.save(emp);
sess.getTransaction().commit();
6
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
3 Table (Table per class)
CREATE TABLE TB_PERSON (
ID SMALLINT NOT NULL,
NAME VARCHAR ( 50 ) ,
ADDRESS VARCHAR ( 50 ) ,
PRIMARY KEY(ID)
)
;
CREATE TABLE TB_EMPLOYEE (
ID SMALLINT NOT NULL,
EMP_NO VARCHAR ( 50 ),
PRIMARY KEY(ID)
)
;
CREATE TABLE TB_STUDENT (
ID SMALLINT NOT NULL,
ROLL_NO CHAR ( 10 ) ,
PRIMARY KEY(ID)
)
;
Project oct9inheritence3tbl
7
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
<hibernate-mapping>
<class name="com.jft.demo.TbPerson" table="tb_person"
catalog="jftsampledb">
<id name="id" type="java.lang.Short">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" length="50" />
</property>
<joined-subclass name="com.jft.demo.TbEmployee" table="TB_EMPLOYEE">
<key column="ID"></key>
<property name="empNo" type="java.lang.String"
column="EMP_NO"></property>
</joined-subclass>
<joined-subclass name="com.jft.demo.TbStudent" table="TB_STUDENT">
<key column="ID"></key>
<property name="rollNo" type="java.lang.String"
column="ROLL_NO"></property>
</joined-subclass>
</class>
</hibernate-mapping>
8
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
Test Program
TbEmployee emp = new TbEmployee();
emp.setEmpNo("E001");
emp.setId(new Short("1"));
emp.setName("Allan");
emp.setAddress("Jaya Nagar");
TbStudent stu = new TbStudent();
stu.setId(new Short("2"));
stu.setName("Johny");
stu.setRollNo("S001");
stu.setAddress("Blore");
Session sess = HibernateSessionFactory.getSession();
sess.beginTransaction();
sess.save(stu);
9
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
2 Table(Table-Only for SubClass)
CREATE TABLE TB_EMPLOYEE_SUBCLS (
ID SMALLINT NOT NULL,
EMP_NO VARCHAR ( 50 ),
NAME VARCHAR ( 50 ) ,
ADDRESS VARCHAR ( 50 ) ,
PRIMARY KEY(ID)
);
CREATE TABLE TB_STUDENT_SUBCLS (
ID SMALLINT NOT NULL,
ROLL_NO CHAR ( 10 ) ,
NAME VARCHAR ( 50 ) ,
ADDRESS VARCHAR ( 50 ) ,
PRIMARY KEY(ID)
)
;
WS:hibernatedemo
Proj:oct9inheritencedesc
DB:jftsampledb
10
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
<hibernate-mapping>
<class name="com.jft.demo.TbPerson" catalog="jftsampledb">
<id name="id" type="java.lang.Short">
<column name="ID" />
<generator class="assigned" />
</id>
<union-subclass name="com.jft.demo.TbEmployee" table="TB_EMPLOYEE_SUBCLS">
<property name="empNo" type="java.lang.String" column="EMP_NO"></property>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" length="50" />
</property>
</union-subclass>
<union-subclass name="com.jft.demo.TbStudent" table="TB_STUDENT_SUBCLS">
<property name="rollNo" type="java.lang.String" column="ROLL_NO"></property>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" length="50" />
</property>
</union-subclass>
</class>
</hibernate-mapping>
11
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
Test Program
12
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com
Polymorphism
TbPerson p1 = (TbPerson)sess.get(TbEmployee.class,new Short("1") );
if (p1 instanceof TbEmployee ) {
TbEmployee e1 = (TbEmployee)p1;
System.out.println(e1.getEmpNo());
}
13
Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com 14
Questions and Comments

More Related Content

What's hot

Advanced php
Advanced phpAdvanced php
Advanced php
hamfu
 
Designing a JavaFX Mobile application
Designing a JavaFX Mobile applicationDesigning a JavaFX Mobile application
Designing a JavaFX Mobile application
Fabrizio Giudici
 
Jooctrine - Doctrine ORM in Joomla!
Jooctrine - Doctrine ORM in Joomla!Jooctrine - Doctrine ORM in Joomla!
Jooctrine - Doctrine ORM in Joomla!
Herman Peeren
 
PostgreSQL (2) by Aswin
PostgreSQL (2) by AswinPostgreSQL (2) by Aswin
PostgreSQL (2) by Aswin
Agate Studio
 

What's hot (20)

Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekinge
 
Round PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing FunctionallyRound PEG, Round Hole - Parsing Functionally
Round PEG, Round Hole - Parsing Functionally
 
Scrap your query boilerplate with specql
Scrap your query boilerplate with specqlScrap your query boilerplate with specql
Scrap your query boilerplate with specql
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
New methods for exploiting ORM injections in Java applications
New methods for exploiting ORM injections in Java applicationsNew methods for exploiting ORM injections in Java applications
New methods for exploiting ORM injections in Java applications
 
Designing a JavaFX Mobile application
Designing a JavaFX Mobile applicationDesigning a JavaFX Mobile application
Designing a JavaFX Mobile application
 
Erlang/OTP for Rubyists
Erlang/OTP for RubyistsErlang/OTP for Rubyists
Erlang/OTP for Rubyists
 
Smelling your code
Smelling your codeSmelling your code
Smelling your code
 
A Few of My Favorite (Python) Things
A Few of My Favorite (Python) ThingsA Few of My Favorite (Python) Things
A Few of My Favorite (Python) Things
 
Advanced Debugging with Xcode - Extending LLDB
Advanced Debugging with Xcode - Extending LLDBAdvanced Debugging with Xcode - Extending LLDB
Advanced Debugging with Xcode - Extending LLDB
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design Patterns
 
Jooctrine - Doctrine ORM in Joomla!
Jooctrine - Doctrine ORM in Joomla!Jooctrine - Doctrine ORM in Joomla!
Jooctrine - Doctrine ORM in Joomla!
 
Conf orm - explain
Conf orm - explainConf orm - explain
Conf orm - explain
 
PostgreSQL (2) by Aswin
PostgreSQL (2) by AswinPostgreSQL (2) by Aswin
PostgreSQL (2) by Aswin
 
Querydsl fin jug - june 2012
Querydsl   fin jug - june 2012Querydsl   fin jug - june 2012
Querydsl fin jug - june 2012
 
Introduction To Moose
Introduction To MooseIntroduction To Moose
Introduction To Moose
 
Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3Transparent Object Persistence with FLOW3
Transparent Object Persistence with FLOW3
 
Clean code
Clean codeClean code
Clean code
 
Building a SQL Database that Works
Building a SQL Database that WorksBuilding a SQL Database that Works
Building a SQL Database that Works
 
Introduction to Moose
Introduction to MooseIntroduction to Moose
Introduction to Moose
 

Similar to Hibernate Inheritenc Mapping

Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
Mouli Chandira
 
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpa
Staples
 

Similar to Hibernate Inheritenc Mapping (20)

Having Fun Programming!
Having Fun Programming!Having Fun Programming!
Having Fun Programming!
 
JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...
JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...
JavaOne 2017 - JNoSQL: The Definitive Solution for Java and NoSQL Database [C...
 
Introduction to hibernate
Introduction to hibernateIntroduction to hibernate
Introduction to hibernate
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
Building a friendly .NET SDK to connect to Space
Building a friendly .NET SDK to connect to SpaceBuilding a friendly .NET SDK to connect to Space
Building a friendly .NET SDK to connect to Space
 
Active Support Core Extensions (1)
Active Support Core Extensions (1)Active Support Core Extensions (1)
Active Support Core Extensions (1)
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
Postobjektové programovanie v Ruby
Postobjektové programovanie v RubyPostobjektové programovanie v Ruby
Postobjektové programovanie v Ruby
 
Using web2py's DAL in other projects or frameworks
Using web2py's DAL in other projects or frameworksUsing web2py's DAL in other projects or frameworks
Using web2py's DAL in other projects or frameworks
 
Ruby on Rails: Tasty Burgers
Ruby on Rails: Tasty BurgersRuby on Rails: Tasty Burgers
Ruby on Rails: Tasty Burgers
 
Hacking XPATH 2.0
Hacking XPATH 2.0Hacking XPATH 2.0
Hacking XPATH 2.0
 
ActiveJDBC - ActiveRecord implementation in Java
ActiveJDBC - ActiveRecord implementation in JavaActiveJDBC - ActiveRecord implementation in Java
ActiveJDBC - ActiveRecord implementation in Java
 
NoSQL Endgame Percona Live Online 2020
NoSQL Endgame Percona Live Online 2020NoSQL Endgame Percona Live Online 2020
NoSQL Endgame Percona Live Online 2020
 
Alternatives of JPA/Hibernate
Alternatives of JPA/HibernateAlternatives of JPA/Hibernate
Alternatives of JPA/Hibernate
 
Using DAOs without implementing them
Using DAOs without implementing themUsing DAOs without implementing them
Using DAOs without implementing them
 
A brief introduction to PostgreSQL
A brief introduction to PostgreSQLA brief introduction to PostgreSQL
A brief introduction to PostgreSQL
 
XML & XPath Injections
XML & XPath InjectionsXML & XPath Injections
XML & XPath Injections
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
 
Advanced SQL Webinar
Advanced SQL WebinarAdvanced SQL Webinar
Advanced SQL Webinar
 
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpa
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

Hibernate Inheritenc Mapping

  • 1. Copyright © 2010 Purple Desk All Rights Reserved. PurpleDesk.and its logo are Trademarks of Purple Desk.www.javafasttrack.com Java Fast Track www.javafasttrack.com Inheritence Mapping
  • 2. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com 2 public class TbPersonDescr { // Fields private Short id; private String name; private String address; private String discriminator; } public class Student extends TbPersonDescr{ private String rollNo; public class Employee extends TbPersonDescr { private String empNo;
  • 3. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com 1 Table per hierarchy(using Discriminator) CREATE TABLE TB_PERSON_DESCR ( ID SMALLINT NOT NULL, NAME VARCHAR ( 50 ) , ROLL_NO CHAR ( 10 ) , EMP_NO VARCHAR ( 50 ), ADDRESS VARCHAR ( 50 ) , DISCRIMINATOR CHAR ( 5 ) , PRIMARY KEY(ID) ) ; WS:hibernatedemo Proj:oct9inheritencedesc DB:jftsampledb 3
  • 4. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com Step1:Create the table Step2:Create a project,add hibernate capabilities end up with a sessionfactory & hibernate.cfg.xml step3: POJO classes ,Person(Super),Employee ,Student step4:Code the mapping file hibernate.cfg.xml <mapping resource="com/jft/demo/TbPersonDescr.hbm.xml" /> Write a Test Program & test 4
  • 5. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com <hibernate-mapping> <class name="com.jft.demo.TbPersonDescr" table="tb_person_descr" catalog="jftsampledb"> <id name="id" type="java.lang.Short"> <column name="ID" /> <generator class="assigned" /> </id> <discriminator column="DISCRIMINATOR"></discriminator> <property name="name" type="java.lang.String"> <column name="NAME" length="50" not-null="false" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="50" not-null="false" /> </property> <subclass name="com.jft.demo.Student" discriminator-value="STUD"> <property name="rollNo" type="java.lang.String"> <column name="ROLL_NO" length="10" not-null="false" /> </property> </subclass> <subclass name="com.jft.demo.Employee" discriminator-value="EMPL"> <property name="empNo" type="java.lang.String"> <column name="EMP_NO" length="50" not-null="false"/> </property> </subclass> </class> </hibernate-mapping> 5
  • 6. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com Session sess = HibernateSessionFactory.getSession(); sess.beginTransaction(); Student stu = new Student(); stu.setId(new Short("3")); stu.setName("Jimmy"); stu.setRollNo("S002"); stu.setAddress("BTM Layout"); sess.save(stu); Employee emp = new Employee(); emp.setId(new Short("4")); emp.setEmpNo("E002"); emp.setName("Peter"); emp.setAddress("Jaya Nagar"); sess.save(emp); sess.getTransaction().commit(); 6
  • 7. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com 3 Table (Table per class) CREATE TABLE TB_PERSON ( ID SMALLINT NOT NULL, NAME VARCHAR ( 50 ) , ADDRESS VARCHAR ( 50 ) , PRIMARY KEY(ID) ) ; CREATE TABLE TB_EMPLOYEE ( ID SMALLINT NOT NULL, EMP_NO VARCHAR ( 50 ), PRIMARY KEY(ID) ) ; CREATE TABLE TB_STUDENT ( ID SMALLINT NOT NULL, ROLL_NO CHAR ( 10 ) , PRIMARY KEY(ID) ) ; Project oct9inheritence3tbl 7
  • 8. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com <hibernate-mapping> <class name="com.jft.demo.TbPerson" table="tb_person" catalog="jftsampledb"> <id name="id" type="java.lang.Short"> <column name="ID" /> <generator class="assigned" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" length="50" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="50" /> </property> <joined-subclass name="com.jft.demo.TbEmployee" table="TB_EMPLOYEE"> <key column="ID"></key> <property name="empNo" type="java.lang.String" column="EMP_NO"></property> </joined-subclass> <joined-subclass name="com.jft.demo.TbStudent" table="TB_STUDENT"> <key column="ID"></key> <property name="rollNo" type="java.lang.String" column="ROLL_NO"></property> </joined-subclass> </class> </hibernate-mapping> 8
  • 9. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com Test Program TbEmployee emp = new TbEmployee(); emp.setEmpNo("E001"); emp.setId(new Short("1")); emp.setName("Allan"); emp.setAddress("Jaya Nagar"); TbStudent stu = new TbStudent(); stu.setId(new Short("2")); stu.setName("Johny"); stu.setRollNo("S001"); stu.setAddress("Blore"); Session sess = HibernateSessionFactory.getSession(); sess.beginTransaction(); sess.save(stu); 9
  • 10. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com 2 Table(Table-Only for SubClass) CREATE TABLE TB_EMPLOYEE_SUBCLS ( ID SMALLINT NOT NULL, EMP_NO VARCHAR ( 50 ), NAME VARCHAR ( 50 ) , ADDRESS VARCHAR ( 50 ) , PRIMARY KEY(ID) ); CREATE TABLE TB_STUDENT_SUBCLS ( ID SMALLINT NOT NULL, ROLL_NO CHAR ( 10 ) , NAME VARCHAR ( 50 ) , ADDRESS VARCHAR ( 50 ) , PRIMARY KEY(ID) ) ; WS:hibernatedemo Proj:oct9inheritencedesc DB:jftsampledb 10
  • 11. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com <hibernate-mapping> <class name="com.jft.demo.TbPerson" catalog="jftsampledb"> <id name="id" type="java.lang.Short"> <column name="ID" /> <generator class="assigned" /> </id> <union-subclass name="com.jft.demo.TbEmployee" table="TB_EMPLOYEE_SUBCLS"> <property name="empNo" type="java.lang.String" column="EMP_NO"></property> <property name="name" type="java.lang.String"> <column name="NAME" length="50" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="50" /> </property> </union-subclass> <union-subclass name="com.jft.demo.TbStudent" table="TB_STUDENT_SUBCLS"> <property name="rollNo" type="java.lang.String" column="ROLL_NO"></property> <property name="name" type="java.lang.String"> <column name="NAME" length="50" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" length="50" /> </property> </union-subclass> </class> </hibernate-mapping> 11
  • 12. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com Test Program 12
  • 13. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com Polymorphism TbPerson p1 = (TbPerson)sess.get(TbEmployee.class,new Short("1") ); if (p1 instanceof TbEmployee ) { TbEmployee e1 = (TbEmployee)p1; System.out.println(e1.getEmpNo()); } 13
  • 14. Copyright © 2010 Purple Desk All Rights Reserved. www.javafasttrack.com 14 Questions and Comments