0% found this document useful (0 votes)
10 views

Module 3 - Inheritance Mapping

Uploaded by

yaduvanshid44
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module 3 - Inheritance Mapping

Uploaded by

yaduvanshid44
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

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

A) Hibernate Persistence Classes

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;

}

www.jtcindia.org 27 Hibernate5.4 Notes


3) OldStudent.java
public class OldStudent extends Student {
private String ocompany;
private String ocemail;
private double octc;

}

4) WeekdayStudent.java
public class WeekdayStudent extends CurrentStudent {
private String qualification;
private String percentage;
private int yop;

}

5) WeekendStudent.java

public class WeekendStudent extends CurrentStudent {


private String wcompany;
private String wcemail;
private double wctc;

}

1) Table per Subclass mapping

 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.

 When you save Student class object then


o Only one record will be inserted in students.

 When you save CurrentStudent class object then


o One record will be inserted in students.
o One record will be inserted in cstudents.

 When you save OldStudent class object then


o One record will be inserted in students.
o One record will be inserted in ostudents.

www.jtcindia.org 28 Hibernate5.4 Notes


 When you save WeekdayStudent class object then
o One record will be inserted in students.
o One record will be inserted in cstudents.
o One record will be inserted in wdstudents.

 When you save WeekendStudent class object then


o One record will be inserted in students.
o One record will be inserted in cstudents.
o One record will be inserted in westudents

A) Tables required for Table Per Sub Class Mapping

1) mystudents

sid sname email phone totalfee

2) Cstudents

mysid feebal timings trainer

3) ostudents

mysid oscompany osemail osctc

4) Wdstudents

mysid qualification percentage yop

5) westudents

mysid wecompany weemail wectc

www.jtcindia.org 29 Hibernate5.4 Notes


B) Hibernate Persistence Classes
1) Student.java
@Entity
@Table(name="students")
@Inheritance(strategy=InheritanceType.JOINED)
public class Student{
...
}
2) CurrentStudent.java
@Entity
@Table(name="cstudents")
@PrimaryKeyJoinColumn(name="mysid")
public class CurrentStudent extends Student{
...
}
3) OldStudent.java
@Entity
@Table(name="ostudents")
@PrimaryKeyJoinColumn(name="mysid")
public class OldStudent extends Student{
....
}
4) WeekdayStudent.java
@Entity
@Table(name="wdstudents")
@PrimaryKeyJoinColumn(name="mysid")
public class WeekdayStudent extends CurrentStudent{
....
}
5) WeekendStudent.java
@Entity
@Table(name="westudents")
@PrimaryKeyJoinColumn(name="mysid")
public class WeekendStudent extends CurrentStudent{
....
}
Note:
 In the case of Table per sub class mapping,
o Use the following for super class
@Inheritance(strategy=InheritanceType.JOINED)
o Use the following for all the sub classes
@PrimaryKeyJoinColumn(name="mysid")

www.jtcindia.org 30 Hibernate5.4 Notes


Lab 4: Table per Subclass Mapping Example

Lab4: Files Required:


1. Lab4A.java 2. Lab4B.java
3. Student.java 4. OldStudent.java
5. CurrentStudent.java 6. WeekdayStudent.java
7. WeekendStudent.java 8. HibernateUtil.java*

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 {

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();


Session session = sessionFactory.openSession();
tx = session.beginTransaction();

//1. Save Student - Inserts 1 record


Student stu = new Student("Som", "Som@jtc", 12345);
session.save(stu);

//2.Save CurrentStudent - Inserts 2 records


CurrentStudent cstu= new CurrentStudent("aa","aa@jtc",111,5000,"11.00A.M","SomPrakash");
session.save(cstu);

//3.Save OldStudent - Inserts 2 records


OldStudent ostu= new OldStudent("bb", "bb@jtc",222, 99.9, "Google", "[email protected]");
session.save(ostu);

//4.Save WeekdayStudent - Inserts 3 records


WeekdayStudent wdstu= new WeekdayStudent("cc",
"cc@jtc",333,9000,"5.00P.M","Prakash",2020,65,"B.Tech");

session.save(wdstu);

www.jtcindia.org 31 Hibernate5.4 Notes


//5.Save WeekendStudent - Inserts 3 records
WeekendStudent westu= new WeekendStudent("dd",
"dd@jtc",444,7500,"7.30A.M","Prakash",19,"TCS","[email protected]");

session.save(westu);

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 Lab4B {
public static void main(String[] args) {

Transaction tx = null;
try {

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();


Session session = sessionFactory.openSession();
tx = session.beginTransaction();

//1. Load Student - fetching from 5 tables


Student stu = session.load(Student.class, 1);
System.out.println(stu);

//2.Load CurrentStudent - fetching from 4 tables


CurrentStudent cstu=session.load(CurrentStudent.class, 2);
System.out.println(cstu);

//3.Load OldStudent - fetching from 2 tables


OldStudent ostu=session.load(OldStudent.class, 3);
System.out.println(ostu);

www.jtcindia.org 32 Hibernate5.4 Notes


//4.Load WeekdayStudent - fetching from 3 tables
WeekdayStudent wdstu=session.load(WeekdayStudent.class, 4);
System.out.println(wdstu);

//5.Load WeekendStudent - fetching from 3 tables


WeekendStudent westu=session.load(WeekendStudent.class, 5);
System.out.println(westu);

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

www.jtcindia.org 33 Hibernate5.4 Notes


public Student() { }

public Student(String sname, String email, int phone) {


this.sname = sname;
this.email = email;
this.phone = phone;
}

public Student(int sid, String sname, String email, int phone) {


this.sid = sid;
this.sname = sname;
this.email = email;
this.phone = phone;
}

//Setters and Getters

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

www.jtcindia.org 34 Hibernate5.4 Notes


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

//Setters and Getters

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

www.jtcindia.org 35 Hibernate5.4 Notes


@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;
}
}

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 {

www.jtcindia.org 36 Hibernate5.4 Notes


@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;
}

//Setters and Getters

@Override
public String toString() {
return super.toString() + yop + ", " + percentage + ", " + qualification;
}

www.jtcindia.org 37 Hibernate5.4 Notes


7)WeekendStudent.java
package com.jtcindia.hibernate;

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

www.jtcindia.org 38 Hibernate5.4 Notes


2) Table per Class Mapping
 In this mapping, you need to take only one table for all super and sub classes.
 It is also called as Single Table Mapping.

A) Tables required
1) mystudents
sid stuType sname email phone totalfee feebal timings trainer oscompany

osemail osctc qualification percentage yop wecompany weemail wectc

B) Hibernate Persistence Classes


1) Student.java

@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{
....
}

www.jtcindia.org 39 Hibernate5.4 Notes


4) WeekdayStudent.java

@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")

www.jtcindia.org 40 Hibernate5.4 Notes


Lab 5: Table per Class Mapping Example

Lab5: Files Required:


9. Lab5A.java 10. Lab5B.java
11. Student.java 12. OldStudent.java
13. CurrentStudent.java 14. WeekdayStudent.java
15. WeekendStudent.java 16. HibernateUtil.java*

1) Lab5A.java
package com.jtcindia.hibernate;

import org.hibernate.*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */

public class Lab5A {


public static void main(String[] args) {

Transaction tx = null;
try {

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();


Session session = sessionFactory.openSession();
tx = session.beginTransaction();

//1. Save Student - 1 record


Student stu = new Student("Som", "Som@jtc", 12345);
session.save(stu);

//2.Save CurrentStudent - 1 record


CurrentStudent cstu= new CurrentStudent("aa","aa@jtc",111,5000,"11.00A.M","Somprakash");
session.save(cstu);

//3.Save OldStudent - 1 record


OldStudent ostu= new OldStudent("bb", "bb@jtc",222, 99.9, "Google", "[email protected]");
session.save(ostu);

//4.Save WeekdayStudent - 1 record


WeekdayStudent wdstu= new WeekdayStudent("cc",
"cc@jtc",333,9000,"5.00P.M","Prakash",2020,65,"B.Tech");
session.save(wdstu);

www.jtcindia.org 41 Hibernate5.4 Notes


//5.Save WeekendStudent - 1 record
WeekendStudent westu= new WeekendStudent("dd",
"dd@jtc",444,7500,"7.30A.M","Prakash",19,"TCS","[email protected]");
session.save(westu);

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 {

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();


Session session = sessionFactory.openSession();
tx = session.beginTransaction();

//1. Load Student - fetching from Single Table


Student stu = session.load(Student.class, 1);
System.out.println(stu);

//2.Load CurrentStudent - fetching from Single Table


CurrentStudent cstu=session.load(CurrentStudent.class, 2);
System.out.println(cstu);

//3.Load OldStudent - fetching from Single Table


OldStudent ostu=session.load(OldStudent.class, 3);
System.out.println(ostu);

www.jtcindia.org 42 Hibernate5.4 Notes


//4.Load WeekdayStudent - fetching from Single Table
WeekdayStudent wdstu=session.load(WeekdayStudent.class, 4);
System.out.println(wdstu);

//5.Load WeekendStudent - fetching from Single Table


WeekendStudent westu=session.load(WeekendStudent.class, 5);
System.out.println(westu);

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;

www.jtcindia.org 43 Hibernate5.4 Notes


public Student() { }

public Student(String sname, String email, int phone) {


this.sname = sname;
this.email = email;
this.phone = phone;
}

public Student(int sid, String sname, String email, int phone) {


this.sid = sid;
this.sname = sname;
this.email = email;
this.phone = phone;
}

//Setters and Getters

@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() { }

www.jtcindia.org 44 Hibernate5.4 Notes


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

//Setters and Getters

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

www.jtcindia.org 45 Hibernate5.4 Notes


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

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;

www.jtcindia.org 46 Hibernate5.4 Notes


@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;
}

//Setters and Getters

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

www.jtcindia.org 47 Hibernate5.4 Notes


@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;
}
}

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
* */

www.jtcindia.org 48 Hibernate5.4 Notes


public class HibernateUtil {

static SessionFactory sessionFactory;

static {
try {

Configuration cfg = new Configuration();

Properties props =new Properties();


props.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
props.put(Environment.URL, "jdbc:mysql://localhost:3306/myjtcdb");
props.put(Environment.USER, "root");
props.put(Environment.PASS, "sompraksh");
props.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
props.put(Environment.SHOW_SQL, "true");
props.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
props.put(Environment.HBM2DDL_AUTO, "update");

cfg.setProperties(props);

cfg.addAnnotatedClass(Student.class);
cfg.addAnnotatedClass(CurrentStudent.class);
cfg.addAnnotatedClass(OldStudent.class);
cfg.addAnnotatedClass(WeekdayStudent.class);
cfg.addAnnotatedClass(WeekendStudent.class);

StandardServiceRegistryBuilder ssrbuilder=new StandardServiceRegistryBuilder();


ServiceRegistry serviceReg=ssrbuilder.applySettings(cfg.getProperties()).build();

sessionFactory = cfg.buildSessionFactory(serviceReg);

}catch(Exception ex) {
ex.printStackTrace();
}
}

public static SessionFactory getSessionFactory() {


return sessionFactory;
}
}

www.jtcindia.org 49 Hibernate5.4 Notes


3) Table per Concrete Class Mapping

 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

www.jtcindia.org 50 Hibernate5.4 Notes


B) Hibernate Persistence Classes
1) Student.java

@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{
....
}

www.jtcindia.org 51 Hibernate5.4 Notes


Note:
 In the case of Table per Concrete class mapping,
o Use the following for super class
@Entity
@Table(name="students1")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)

o Use the following for super class and all the sub classes
@Entity
@Table(name="cstudents1")

o Don’t specify the Primary key generation for sid.

Lab 6: Table per Concrete Class Mapping Example

Lab6: Files Required:


1. Lab6A.java 2. Lab6B.java
3. Student.java 4. OldStudent.java
5. CurrentStudent.java 6. WeekdayStudent.java
7. WeekendStudent.java 8. PKGenerator.java
9. HibernateUtil.java*

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();

//1. Save Student - 1 record


int stuId=PKGenerator.getNextStudentId();
Student stu = new Student(stuId,"Som", "Som@jtc", 12345);
session.save(stu);

www.jtcindia.org 52 Hibernate5.4 Notes


//2.Save CurrentStudent - 1 record
int currentStuId = PKGenerator.getNextCurrentStudentId();
CurrentStudent cstu= new
CurrentStudent(currentStuId,"aa","aa@jtc",111,5000,"11.00A.M","prakash");
session.save(cstu);

//3.Save OldStudent - 1 record


int oldStuId = PKGenerator.getNextOldStudentId();
OldStudent ostu= new OldStudent(oldStuId,"bb", "bb@jtc",222, 99.9, "Google",
"[email protected]");
session.save(ostu);

//4.Save WeekdayStudent - 1 record


int wdStuId = PKGenerator.getNextWDStudentId();
WeekdayStudent wdstu= new WeekdayStudent(wdStuId,"cc",
"cc@jtc",333,9000,"5.00P.M","Prakash",2020,65,"B.Tech");
session.save(wdstu);

//5.Save WeekendStudent - 1 record


int weStuId = PKGenerator.getNextWEStudentId();
WeekendStudent westu= new WeekendStudent(weStuId,"dd",
"dd@jtc",444,7500,"7.30A.M","Prakash",19,"TCS","[email protected]");
session.save(westu);

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;

www.jtcindia.org 53 Hibernate5.4 Notes


try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction();

//1. Load Student - fetching from Single Table


Student stu = session.load(Student.class, 101);
System.out.println(stu);

//2.Load CurrentStudent - fetching from Single Table


CurrentStudent cstu=session.load(CurrentStudent.class, 201);
System.out.println(cstu);

//3.Load OldStudent - fetching from Single Table


OldStudent ostu=session.load(OldStudent.class, 301);
System.out.println(ostu);

//4.Load WeekdayStudent - fetching from Single Table


WeekdayStudent wdstu=session.load(WeekdayStudent.class, 401);
System.out.println(wdstu);

//5.Load WeekendStudent - fetching from Single Table


WeekendStudent westu=session.load(WeekendStudent.class, 501);
System.out.println(westu);

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)

www.jtcindia.org 54 Hibernate5.4 Notes


public class Student {

@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() { }

public Student(String sname, String email, int phone) {


this.sname = sname;
this.email = email;
this.phone = phone;
}

public Student(int sid, String sname, String email, int phone) {


this.sid = sid;
this.sname = sname;
this.email = email;
this.phone = phone;
}

//Setters and Getters

@Override
public String toString() {
return sid + ", " + sname + ", " + email + ", " + phone ;
}
}

www.jtcindia.org 55 Hibernate5.4 Notes


4) OldStudent.java
package com.jtcindia.hibernate;

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

//Setters and Getters

@Override
public String toString() {
return super.toString() + osalary + ", " + ocompanyName + ", " + oemail;
}
}

www.jtcindia.org 56 Hibernate5.4 Notes


5) CurrentStudent.java
package com.jtcindia.hibernate;

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

www.jtcindia.org 57 Hibernate5.4 Notes


6) WeekdayStudent.java
package com.jtcindia.hibernate;

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

//Setters and Getters

@Override
public String toString() {
return super.toString() + yop + ", " + percentage + ", " + qualification;
}
}

www.jtcindia.org 58 Hibernate5.4 Notes


7) WeekendStudent.java
package com.jtcindia.hibernate;

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

www.jtcindia.org 59 Hibernate5.4 Notes


8)PKGenerator.java
package com.jtcindia.hibernate;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class PKGenerator {

public static int getNextStudentId() {


//Write the for generating ID
return 101;
}

public static int getNextCurrentStudentId() {


//Write the for generating ID
return 201;
}

public static int getNextOldStudentId() {


//Write the for generating ID
return 301;
}

public static int getNextWDStudentId() {


//Write the for generating ID
return 401;
}

public static int getNextWEStudentId() {


//Write the for generating ID
return 501;
}
}

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;

www.jtcindia.org 60 Hibernate5.4 Notes


/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class HibernateUtil {

static SessionFactory sessionFactory;

static {
try {

Configuration cfg = new Configuration();

Properties props =new Properties();


props.put(Environment.DRIVER, "com.mysql.jdbc.Driver");
props.put(Environment.URL, "jdbc:mysql://localhost:3306/myjtcdb");
props.put(Environment.USER, "root");
props.put(Environment.PASS, "Somprakash");
props.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
props.put(Environment.SHOW_SQL, "true");
props.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
props.put(Environment.HBM2DDL_AUTO, "update");

cfg.setProperties(props);

cfg.addAnnotatedClass(Student.class);
cfg.addAnnotatedClass(CurrentStudent.class);
cfg.addAnnotatedClass(OldStudent.class);
cfg.addAnnotatedClass(WeekdayStudent.class);
cfg.addAnnotatedClass(WeekendStudent.class);

StandardServiceRegistryBuilder ssrbuilder=new StandardServiceRegistryBuilder();


ServiceRegistry serviceReg=ssrbuilder.applySettings(cfg.getProperties()).build();

sessionFactory = cfg.buildSessionFactory(serviceReg);

}catch(Exception ex) {
ex.printStackTrace();
}
}

public static SessionFactory getSessionFactory() {


return sessionFactory;
}
}

www.jtcindia.org 61 Hibernate5.4 Notes


Interview Questions:
Q45) When to Use Inheritance Mapping?
Ans:

Q46) How many types of Inheritance Mappings are there? What are they?
Ans:

Q47) When to use Table per Sub Class Mapping?


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:

www.jtcindia.org 62 Hibernate5.4 Notes


Q55) When to use Table per Class Mapping?
Ans:

Q56) How to consider the Tables in Table per 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:

Q63) When to use Table per Concrete Class Mapping?


Ans:

Q64) How to consider the Tables in Table per Concrete Class Mapping?
Ans:

www.jtcindia.org 63 Hibernate5.4 Notes


Q65) What are the Annotations to be used for Super class 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:

www.jtcindia.org 64 Hibernate5.4 Notes


Assignment #1
Implemen the following Assignment with
• Collection Mapping
• Table per Sub class Inheritance Mapping.

abstract class Customer{


int cid;
String cname;
String email;
long phone;
Set<Integer> cardNumbers;
List<String> ewalletNames;
Map<String,String> reviews;

//Constructors
//Setters and Getters
}

class SilverCustomer extends Customer{


String supportEmail;
double discounts;
Double shippingFee;
double handlingChanges;

//Constructors
//Setters and Getters

class GoldCustomer extends Customer{


long supportPhone;
int rpoints;
double cashBack;
String earlyAccess;

//Constructors
//Setters and Getters

www.jtcindia.org 65 Hibernate5.4 Notes

You might also like