Module 3 - Inheritance Mapping
Module 3 - Inheritance Mapping
When multiple Persistence classes are in inheritance relationship then use Inheritance
Mapping.
You can use Inheritance Mapping to represent the Type of Entities.
You can implement Inheritance Mapping in 3 ways
1. Table per Sub class mapping
2. Table per class mapping
3. Table per Concrete class mapping
Consider the following Hibernate Persistence Classes with inheritance relationship.
Student
CurrentStudent OldStudent
WeekdayStudent WeekendStudent
1) Student.java
public class Student {
private int sid;
private String sname;
private String email;
private String phone;
private double totalfee;
…
}
2) CurrentStudent.java
public class CurrentStudent extends Student {
private double feebal;
private String timings;
private String trainer;
…
}
4) WeekdayStudent.java
public class WeekdayStudent extends CurrentStudent {
private String qualification;
private String percentage;
private int yop;
…
}
5) WeekendStudent.java
In this mapping, Student is the main super class which will have the master table called
mystudents.
You need to take one table per one sub class.
i.e Every subclass will have its own table.
1) mystudents
2) Cstudents
3) ostudents
4) Wdstudents
5) westudents
1) Lab4A.java
package com.jtcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class Lab4A {
public static void main(String[] args){
Transaction tx = null;
try {
session.save(wdstu);
session.save(westu);
tx.commit();
session.close();
2)Lab4B.java
package com.jtcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class Lab4B {
public static void main(String[] args) {
Transaction tx = null;
try {
tx.commit();
session.close();
3)Student.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "mystudents")
@Inheritance(strategy = InheritanceType.JOINED)
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "som")
private int sid;
@Column(name = "sname")
private String sname;
@Column(name = "email")
private String email;
@Column(name = "phone")
private int phone;
@Override
public String toString() {
return sid + ", " + sname + ", " + email + ", " + phone ;
}
}
4)OldStudent.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "ostudents")
@PrimaryKeyJoinColumn(name = "mysid")
public class OldStudent extends Student {
@Column(name = "osalary")
private double osalary;
@Column(name = "ocompanyName")
private String ocompanyName;
@Column(name = "oemail")
private String oemail;
public OldStudent(String sname, String email, int phone, double osalary, String
ocompanyName, String oemail) {
public OldStudent(int sid, String sname, String email, int phone, double osalary, String
ocompanyName,String oemail) {
@Override
public String toString() {
return super.toString() + osalary + ", " + ocompanyName + ", " + oemail;
}
}
5)CurrentStudent.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "cstudents")
@PrimaryKeyJoinColumn(name = "mysid")
public class CurrentStudent extends Student{
@Column(name = "feebal")
private double feebal;
@Column(name = "timings")
private String timings;
public CurrentStudent() {}
public CurrentStudent(String sname, String email, int phone, double feebal, String timings,
String trainer) {
public CurrentStudent(int sid, String sname, String email, int phone, double feebal, String
timings,
String trainer) {
@Override
public String toString() {
return super.toString() + feebal + ", " + timings + ", " + trainer;
}
}
6)WeekdayStudent.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "wdstudents")
@PrimaryKeyJoinColumn(name = "mysid")
public class WeekdayStudent extends CurrentStudent {
@Column(name = "percentage")
private double percentage;
@Column(name = "qualification")
private String qualification;
public WeekdayStudent() { }
public WeekdayStudent(String sname, String email, int phone, double feebal, String timings,
String trainer, int yop,double percentage, String qualification) {
public WeekdayStudent(int sid, String sname, String email, int phone, double feebal, String
timings, String trainer,int yop, double percentage, String qualification) {
@Override
public String toString() {
return super.toString() + yop + ", " + percentage + ", " + qualification;
}
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "westudents")
@PrimaryKeyJoinColumn(name = "mysid")
public class WeekendStudent extends CurrentStudent {
@Column(name = "wesalary")
private double wesalary;
@Column(name = "wecompanyName")
private String wecompanyName;
@Column(name = "weemail")
private String weemail;
public WeekendStudent() { }
public WeekendStudent(String sname, String email, int phone, double feebal, String timings,
String trainer, double wesalary, String wecompanyName, String weemail) {
super(sname, email, phone, feebal, timings, trainer);
this.wesalary = wesalary;
this.wecompanyName = wecompanyName;
this.weemail = weemail;
}
public WeekendStudent(int sid, String sname, String email, int phone, double feebal, String
timings, String trainer, double wesalary, String wecompanyName, String weemail) {
super(sid, sname, email, phone, feebal, timings, trainer);
this.wesalary = wesalary;
this.wecompanyName = wecompanyName;
this.weemail = weemail;
}
//Setters and Getters
@Override
public String toString() {
return super.toString() + wesalary + ", " + wecompanyName + ", " + weemail;
}
}
A) Tables required
1) mystudents
sid stuType sname email phone totalfee feebal timings trainer oscompany
@Entity
@Table(name="mystudents")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="stuType",length=5)
@DiscriminatorValue(value="STU")
public class Student{
...
}
2) CurrentStudent.java
@Entity
@DiscriminatorValue(value="CSTU")
public class CurrentStudent extends Student{
....
}
3) OldStudent.java
@Entity
@DiscriminatorValue(value="OSTU")
public class OldStudent extends Student{
....
}
@Entity
@DiscriminatorValue(value="WDSTU")
public class WeekdayStudent extends CurrentStudent{
....
}
5) WeekendStudent.java
@Entity
@DiscriminatorValue(value="WESTU")
public class WeekendStudent extends CurrentStudent{
....
}
Note:
In the case of Table per class mapping,
o Use the following for super class
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="stuType",length=5)
@DiscriminatorValue(value="xx")
o Use the following for super class and all the sub classes
@DiscriminatorValue(value="xx")
1) Lab5A.java
package com.jtcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
Transaction tx = null;
try {
tx.commit();
session.close();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}
2)Lab4B.java
package com.jtcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class Lab5B {
public static void main(String[] args) {
Transaction tx = null;
try {
tx.commit();
session.close();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}
3)Student.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "mystudents")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "stuType",length = 5)
@DiscriminatorValue("STU")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "sid")
private int sid;
@Column(name = "sname")
private String sname;
@Column(name = "email")
private String email;
@Column(name = "phone")
private int phone;
@Override
public String toString() {
return sid + ", " + sname + ", " + email + ", " + phone ;
}
}
4)OldStudent.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@DiscriminatorValue("OSTU")
public class OldStudent extends Student {
@Column(name = "osalary")
private double osalary;
@Column(name = "ocompanyName")
private String ocompanyName;
@Column(name = "oemail")
private String oemail;
public OldStudent() { }
public OldStudent(int sid, String sname, String email, int phone, double osalary, String
ocompanyName,String oemail) {
@Override
public String toString() {
return super.toString() + osalary + ", " + ocompanyName + ", " + oemail;
}
}
5)CurrentStudent.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@DiscriminatorValue("CSTU")
public class CurrentStudent extends Student{
@Column(name = "feebal")
private double feebal;
@Column(name = "timings")
private String timings;
@Column(name = "trainer")
private String trainer;
public CurrentStudent(String sname, String email, int phone, double feebal, String timings,
String trainer) {
public CurrentStudent(int sid, String sname, String email, int phone, double feebal, String
timings,
String trainer) {
@Override
public String toString() {
return super.toString() + feebal + ", " + timings + ", " + trainer;
}
}
6)WeekdayStudent.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@DiscriminatorValue("WDSTU")
public class WeekdayStudent extends CurrentStudent {
@Column(name = "yop")
private int yop;
@Column(name = "percentage")
private double percentage;
public WeekdayStudent() { }
public WeekdayStudent(String sname, String email, int phone, double feebal, String timings,
String trainer, int yop,double percentage, String qualification) {
public WeekdayStudent(int sid, String sname, String email, int phone, double feebal, String
timings, String trainer,int yop, double percentage, String qualification) {
@Override
public String toString() {
return super.toString() + yop + ", " + percentage + ", " + qualification;
}
}
7)WeekendStudent.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@DiscriminatorValue("WESTU")
public class WeekendStudent extends CurrentStudent {
@Column(name = "wesalary")
private double wesalary;
@Column(name = "weemail")
private String weemail;
public WeekendStudent() { }
public WeekendStudent(String sname, String email, int phone, double feebal, String timings,
String trainer, double wesalary, String wecompanyName, String weemail) {
super(sname, email, phone, feebal, timings, trainer);
this.wesalary = wesalary;
this.wecompanyName = wecompanyName;
this.weemail = weemail;
}
public WeekendStudent(int sid, String sname, String email, int phone, double feebal, String
timings, String trainer, double wesalary, String wecompanyName, String weemail) {
super(sid, sname, email, phone, feebal, timings, trainer);
this.wesalary = wesalary;
this.wecompanyName = wecompanyName;
this.weemail = weemail;
}
//Setters and Getters
@Override
public String toString() {
return super.toString() + wesalary + ", " + wecompanyName + ", " + weemail;
}
}
8)HibernateUtil.java
package com.jtcindia.hibernate;
import java.util.Properties;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
static {
try {
cfg.setProperties(props);
cfg.addAnnotatedClass(Student.class);
cfg.addAnnotatedClass(CurrentStudent.class);
cfg.addAnnotatedClass(OldStudent.class);
cfg.addAnnotatedClass(WeekdayStudent.class);
cfg.addAnnotatedClass(WeekendStudent.class);
sessionFactory = cfg.buildSessionFactory(serviceReg);
}catch(Exception ex) {
ex.printStackTrace();
}
}
In this mapping, you need to take one table for one concrete class.
When you save Student class object then only one record will be inserted in mystudents.
When you save CurrentStudent class object then only one record will be inserted in
cstudents.
When you save OldStudent class object then only one record will be inserted in ostudents.
When you save WeekdayStudent class object then only one record will be inserted in
wdstudents.
When you save WeekendStudent class object then only one record will be inserted in
westudents.
A) Tables required
1) students
sid sname email phone
2) cstudents
sid sname email phone feebal timings trainer
3) ostudents
sid sname email phone oscompany osemail osctc
4) wdstudents
sid sname email phone feebal timings trainer qualification percentage yop
5) westudents
sid sname email phone feebal timings trainer wecompany weemail wectc
@Entity
@Table(name="students")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Student{
@Id
@Column(name="sid")
private int sid;
...
}
2) CurrentStudent.java
@Entity
@Table(name="cstudents")
public class CurrentStudent extends Student{
....
}
3) OldStudent.java
@Entity
@Table(name="ostudents")
public class OldStudent extends Student{
....
}
4) WeekdayStudent.java
@Entity
@Table(name="wdstudents")
public class WeekdayStudent extends CurrentStudent{
....
}
5) WeekendStudent.java
@Entity
@Table(name="westudents")
public class WeekendStudent extends CurrentStudent{
....
}
o Use the following for super class and all the sub classes
@Entity
@Table(name="cstudents1")
1) Lab6A.java
package com.jtcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class Lab6A {
public static void main(String[] args) {
Transaction tx = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
session.close();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}
2)Lab6B.java
package com.jtcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class Lab6B {
public static void main(String[] args) {
Transaction tx = null;
tx.commit();
session.close();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}
3)Student.java
package com.jtcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "mystudents")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Id
@Column(name = "sid")
private int sid;
@Column(name = "sname")
private String sname;
@Column(name = "email")
private String email;
@Column(name = "phone")
private int phone;
public Student() { }
@Override
public String toString() {
return sid + ", " + sname + ", " + email + ", " + phone ;
}
}
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "ostudents")
public class OldStudent extends Student {
@Column(name = "osalary")
private double osalary;
@Column(name = "ocompanyName")
private String ocompanyName;
@Column(name = "oemail")
private String oemail;
public OldStudent() { }
public OldStudent(String sname, String email, int phone, double osalary, String
ocompanyName, String oemail) {
super(sname, email, phone);
this.osalary = osalary;
this.ocompanyName = ocompanyName;
this.oemail = oemail;
}
public OldStudent(int sid, String sname, String email, int phone, double osalary, String
ocompanyName,String oemail) {
super(sid, sname, email, phone);
this.osalary = osalary;
this.ocompanyName = ocompanyName;
this.oemail = oemail;
}
@Override
public String toString() {
return super.toString() + osalary + ", " + ocompanyName + ", " + oemail;
}
}
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "cstudents")
public class CurrentStudent extends Student{
@Column(name = "feebal")
private double feebal;
@Column(name = "timings")
private String timings;
@Column(name = "trainer")
private String trainer;
public CurrentStudent() {}
public CurrentStudent(String sname, String email, int phone, double feebal, String timings,
String trainer) {
super( sname, email, phone);
this.feebal = feebal;
this.timings = timings;
this.trainer = trainer;
}
public CurrentStudent(int sid, String sname, String email, int phone, double feebal, String
timings,
String trainer) {
super(sid, sname, email, phone);
this.feebal = feebal;
this.timings = timings;
this.trainer = trainer;
}
//Setters and Getters
@Override
public String toString() {
return super.toString() + feebal + ", " + timings + ", " + trainer;
}
}
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "wdstudents")
public class WeekdayStudent extends CurrentStudent {
@Column(name = "yop")
private int yop;
@Column(name = "percentage")
private double percentage;
@Column(name = "qualification")
private String qualification;
public WeekdayStudent() { }
public WeekdayStudent(String sname, String email, int phone, double feebal, String timings,
String trainer, int yop,double percentage, String qualification) {
super(sname, email, phone, feebal, timings, trainer);
this.yop = yop;
this.percentage = percentage;
this.qualification = qualification;
}
public WeekdayStudent(int sid, String sname, String email, int phone, double feebal, String
timings, String trainer,int yop, double percentage, String qualification) {
super(sid, sname, email, phone, feebal, timings, trainer);
this.yop = yop;
this.percentage = percentage;
this.qualification = qualification;
}
@Override
public String toString() {
return super.toString() + yop + ", " + percentage + ", " + qualification;
}
}
import javax.persistence.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "westudents")
public class WeekendStudent extends CurrentStudent {
@Column(name = "wesalary")
private double wesalary;
@Column(name = "wecompanyName")
private String wecompanyName;
@Column(name = "weemail")
private String weemail;
public WeekendStudent() { }
public WeekendStudent(String sname, String email, int phone, double feebal, String timings,
String trainer, double wesalary, String wecompanyName, String weemail) {
super(sname, email, phone, feebal, timings, trainer);
this.wesalary = wesalary;
this.wecompanyName = wecompanyName;
this.weemail = weemail;
}
public WeekendStudent(int sid, String sname, String email, int phone, double feebal, String
timings, String trainer, double wesalary, String wecompanyName, String weemail) {
super(sid, sname, email, phone, feebal, timings, trainer);
this.wesalary = wesalary;
this.wecompanyName = wecompanyName;
this.weemail = weemail;
}
//Setters and Getters
@Override
public String toString() {
return super.toString() + wesalary + ", " + wecompanyName + ", " + weemail;
}
}
9)HibernateUtil.java
package com.jtcindia.hibernate;
import java.util.Properties;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
static {
try {
cfg.setProperties(props);
cfg.addAnnotatedClass(Student.class);
cfg.addAnnotatedClass(CurrentStudent.class);
cfg.addAnnotatedClass(OldStudent.class);
cfg.addAnnotatedClass(WeekdayStudent.class);
cfg.addAnnotatedClass(WeekendStudent.class);
sessionFactory = cfg.buildSessionFactory(serviceReg);
}catch(Exception ex) {
ex.printStackTrace();
}
}
Q46) How many types of Inheritance Mappings are there? What are they?
Ans:
Q48) How to consider the Tables in Table per Sub Class Mapping?
Ans:
Q49) What are the Annotations to be used for Super class in Table per Sub Class Mapping?
Ans:
Q50) What are the Annotations to be used for Sub class in Table per Sub Class Mapping?
Ans:
Q51) Can I use different P.K generation Strategies for different Types of Entities in Table per
Sub Class Mapping?
Ans:
Q52) Can I Specify @GeneratedValue for P.K generation in Table per Sub Class Mapping?
Ans:
Q53) What is the Default Loading Strategy for Table per Sub Class Mapping?
Ans:
Q54) What is the Default Fetching Strategy for Table per Sub Class Mapping?
Ans:
Q57) What are the Annotations to be used for Super class in Table per Class Mapping?
Ans:
Q58) What are the Annotations to be used for Sub class in Table per Class Mapping?
Ans:
Q59) Can I use different Primary Key generation Strategies for different Types of Entities in
Table per Class Mapping?
Ans:
Q60) Can I Specify @GeneratedValue for P.K generation in Table per Class Mapping?
Ans:
Q61) What is the Default Loading Strategy for Table per Class Mapping?
Ans:
Q62) What is the Default Fetching Strategy for Table per Class Mapping?
Ans:
Q64) How to consider the Tables in Table per Concrete Class Mapping?
Ans:
Q66) What are the Annotations to be used for Sub class in Table per Concrete Class
Mapping?
Ans:
Q67) Can I use different Primary Key generation Strategies for different Types of Entities in
Table per Concrete Class Mapping?
Ans:
Q68) Can I Specify @GeneratedValue for P.K generation in Table per Concrete Class
Mapping?
Ans:
Q69) What is the Default Loading Strategy for Table per Concrete Class Mapping?
Ans:
Q70) What is the Default Fetching Strategy for Table per Concrete Class Mapping?
Ans:
//Constructors
//Setters and Getters
}
//Constructors
//Setters and Getters
//Constructors
//Setters and Getters