0% found this document useful (0 votes)
11 views39 pages

Hibernate Inheritance Mapping Overview

Uploaded by

yaduvanshid44
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views39 pages

Hibernate Inheritance Mapping Overview

Uploaded by

yaduvanshid44
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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) [Link]
public class Student {
private int sid;
private String sname;
private String email;
private String phone;
private double totalfee;

}

2) [Link]
public class CurrentStudent extends Student {
private double feebal;
private String timings;
private String trainer;

}

[Link] 27 Hibernate5.4 Notes


3) [Link]
public class OldStudent extends Student {
private String ocompany;
private String ocemail;
private double octc;

}

4) [Link]
public class WeekdayStudent extends CurrentStudent {
private String qualification;
private String percentage;
private int yop;

}

5) [Link]

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.

[Link] 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

[Link] 29 Hibernate5.4 Notes


B) Hibernate Persistence Classes
1) [Link]
@Entity
@Table(name="students")
@Inheritance(strategy=[Link])
public class Student{
...
}
2) [Link]
@Entity
@Table(name="cstudents")
@PrimaryKeyJoinColumn(name="mysid")
public class CurrentStudent extends Student{
...
}
3) [Link]
@Entity
@Table(name="ostudents")
@PrimaryKeyJoinColumn(name="mysid")
public class OldStudent extends Student{
....
}
4) [Link]
@Entity
@Table(name="wdstudents")
@PrimaryKeyJoinColumn(name="mysid")
public class WeekdayStudent extends CurrentStudent{
....
}
5) [Link]
@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=[Link])
o Use the following for all the sub classes
@PrimaryKeyJoinColumn(name="mysid")

[Link] 30 Hibernate5.4 Notes


Lab 4: Table per Subclass Mapping Example

Lab4: Files Required:


1. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link] 6. [Link]
7. [Link] 8. [Link]*

1) [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class Lab4A {
public static void main(String[] args){

Transaction tx = null;
try {

SessionFactory sessionFactory = [Link]();


Session session = [Link]();
tx = [Link]();

//1. Save Student - Inserts 1 record


Student stu = new Student("Som", "Som@jtc", 12345);
[Link](stu);

//[Link] CurrentStudent - Inserts 2 records


CurrentStudent cstu= new CurrentStudent("aa","aa@jtc",111,5000,"11.00A.M","SomPrakash");
[Link](cstu);

//[Link] OldStudent - Inserts 2 records


OldStudent ostu= new OldStudent("bb", "bb@jtc",222, 99.9, "Google", "bb@[Link]");
[Link](ostu);

//[Link] WeekdayStudent - Inserts 3 records


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

[Link](wdstu);

[Link] 31 Hibernate5.4 Notes


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

[Link](westu);

[Link]();
[Link]();

} catch (Exception ex) {


[Link]();
[Link]();
}
}
}

2)[Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class Lab4B {
public static void main(String[] args) {

Transaction tx = null;
try {

SessionFactory sessionFactory = [Link]();


Session session = [Link]();
tx = [Link]();

//1. Load Student - fetching from 5 tables


Student stu = [Link]([Link], 1);
[Link](stu);

//[Link] CurrentStudent - fetching from 4 tables


CurrentStudent cstu=[Link]([Link], 2);
[Link](cstu);

//[Link] OldStudent - fetching from 2 tables


OldStudent ostu=[Link]([Link], 3);
[Link](ostu);

[Link] 32 Hibernate5.4 Notes


//[Link] WeekdayStudent - fetching from 3 tables
WeekdayStudent wdstu=[Link]([Link], 4);
[Link](wdstu);

//[Link] WeekendStudent - fetching from 3 tables


WeekendStudent westu=[Link]([Link], 5);
[Link](westu);

[Link]();
[Link]();

} catch (Exception ex) {


[Link]();
[Link]();
}
}
}

3)[Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */

@Entity
@Table(name = "mystudents")
@Inheritance(strategy = [Link])
public class Student {

@Id
@GeneratedValue(strategy = [Link])
@Column(name = "som")
private int sid;

@Column(name = "sname")
private String sname;

@Column(name = "email")
private String email;

@Column(name = "phone")
private int phone;

[Link] 33 Hibernate5.4 Notes


public Student() { }

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


[Link] = sname;
[Link] = email;
[Link] = phone;
}

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


[Link] = sid;
[Link] = sname;
[Link] = email;
[Link] = phone;
}

//Setters and Getters

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

4)[Link]
package [Link];

import [Link].*;
/*
* @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;

[Link] 34 Hibernate5.4 Notes


public OldStudent() { }

public OldStudent(String sname, String email, int phone, double osalary, String
ocompanyName, String oemail) {

super(sname, email, phone);


[Link] = osalary;
[Link] = ocompanyName;
[Link] = oemail;
}

public OldStudent(int sid, String sname, String email, int phone, double osalary, String
ocompanyName,String oemail) {

super(sid, sname, email, phone);


[Link] = osalary;
[Link] = ocompanyName;
[Link] = oemail;
}

//Setters and Getters

@Override
public String toString() {
return [Link]() + osalary + ", " + ocompanyName + ", " + oemail;
}
}

5)[Link]
package [Link];

import [Link].*;
/*
* @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;

[Link] 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);


[Link] = feebal;
[Link] = timings;
[Link] = trainer;
}

public CurrentStudent(int sid, String sname, String email, int phone, double feebal, String
timings,
String trainer) {

super(sid, sname, email, phone);


[Link] = feebal;
[Link] = timings;
[Link] = trainer;
}

//Setters and Getters

@Override
public String toString() {
return [Link]() + feebal + ", " + timings + ", " + trainer;
}
}

6)[Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "wdstudents")
@PrimaryKeyJoinColumn(name = "mysid")
public class WeekdayStudent extends CurrentStudent {

[Link] 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);


[Link] = yop;
[Link] = percentage;
[Link] = 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);


[Link] = yop;
[Link] = percentage;
[Link] = qualification;
}

//Setters and Getters

@Override
public String toString() {
return [Link]() + yop + ", " + percentage + ", " + qualification;
}

[Link] 37 Hibernate5.4 Notes


7)[Link]
package [Link];

import [Link].*;
/*
* @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);
[Link] = wesalary;
[Link] = wecompanyName;
[Link] = 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);
[Link] = wesalary;
[Link] = wecompanyName;
[Link] = weemail;
}
//Setters and Getters
@Override
public String toString() {
return [Link]() + wesalary + ", " + wecompanyName + ", " + weemail;
}
}

[Link] 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) [Link]

@Entity
@Table(name="mystudents")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="stuType",length=5)
@DiscriminatorValue(value="STU")
public class Student{
...
}

2) [Link]

@Entity
@DiscriminatorValue(value="CSTU")
public class CurrentStudent extends Student{
....
}

3) [Link]

@Entity
@DiscriminatorValue(value="OSTU")
public class OldStudent extends Student{
....
}

[Link] 39 Hibernate5.4 Notes


4) [Link]

@Entity
@DiscriminatorValue(value="WDSTU")
public class WeekdayStudent extends CurrentStudent{
....
}

5) [Link]

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

[Link] 40 Hibernate5.4 Notes


Lab 5: Table per Class Mapping Example

Lab5: Files Required:


9. [Link] 10. [Link]
11. [Link] 12. [Link]
13. [Link] 14. [Link]
15. [Link] 16. [Link]*

1) [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */

public class Lab5A {


public static void main(String[] args) {

Transaction tx = null;
try {

SessionFactory sessionFactory = [Link]();


Session session = [Link]();
tx = [Link]();

//1. Save Student - 1 record


Student stu = new Student("Som", "Som@jtc", 12345);
[Link](stu);

//[Link] CurrentStudent - 1 record


CurrentStudent cstu= new CurrentStudent("aa","aa@jtc",111,5000,"11.00A.M","Somprakash");
[Link](cstu);

//[Link] OldStudent - 1 record


OldStudent ostu= new OldStudent("bb", "bb@jtc",222, 99.9, "Google", "bb@[Link]");
[Link](ostu);

//[Link] WeekdayStudent - 1 record


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

[Link] 41 Hibernate5.4 Notes


//[Link] WeekendStudent - 1 record
WeekendStudent westu= new WeekendStudent("dd",
"dd@jtc",444,7500,"7.30A.M","Prakash",19,"TCS","dd@[Link]");
[Link](westu);

[Link]();
[Link]();
} catch (Exception ex) {
[Link]();
[Link]();
}

}
}

2)[Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class Lab5B {
public static void main(String[] args) {

Transaction tx = null;
try {

SessionFactory sessionFactory = [Link]();


Session session = [Link]();
tx = [Link]();

//1. Load Student - fetching from Single Table


Student stu = [Link]([Link], 1);
[Link](stu);

//[Link] CurrentStudent - fetching from Single Table


CurrentStudent cstu=[Link]([Link], 2);
[Link](cstu);

//[Link] OldStudent - fetching from Single Table


OldStudent ostu=[Link]([Link], 3);
[Link](ostu);

[Link] 42 Hibernate5.4 Notes


//[Link] WeekdayStudent - fetching from Single Table
WeekdayStudent wdstu=[Link]([Link], 4);
[Link](wdstu);

//[Link] WeekendStudent - fetching from Single Table


WeekendStudent westu=[Link]([Link], 5);
[Link](westu);

[Link]();
[Link]();
} catch (Exception ex) {
[Link]();
[Link]();
}
}
}

3)[Link]
package [Link];

import [Link].*;
/*
* @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 = [Link])
@Column(name = "sid")
private int sid;

@Column(name = "sname")
private String sname;

@Column(name = "email")
private String email;

@Column(name = "phone")
private int phone;

[Link] 43 Hibernate5.4 Notes


public Student() { }

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


[Link] = sname;
[Link] = email;
[Link] = phone;
}

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


[Link] = sid;
[Link] = sname;
[Link] = email;
[Link] = phone;
}

//Setters and Getters

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

4)[Link]
package [Link];

import [Link].*;
/*
* @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() { }

[Link] 44 Hibernate5.4 Notes


public OldStudent(String sname, String email, int phone, double osalary, String
ocompanyName, String oemail) {

super(sname, email, phone);


[Link] = osalary;
[Link] = ocompanyName;
[Link] = oemail;
}

public OldStudent(int sid, String sname, String email, int phone, double osalary, String
ocompanyName,String oemail) {

super(sid, sname, email, phone);


[Link] = osalary;
[Link] = ocompanyName;
[Link] = oemail;
}

//Setters and Getters

@Override
public String toString() {
return [Link]() + osalary + ", " + ocompanyName + ", " + oemail;
}
}

5)[Link]
package [Link];

import [Link].*;
/*
* @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;

[Link] 45 Hibernate5.4 Notes


public CurrentStudent() {}

public CurrentStudent(String sname, String email, int phone, double feebal, String timings,
String trainer) {

super( sname, email, phone);


[Link] = feebal;
[Link] = timings;
[Link] = trainer;
}

public CurrentStudent(int sid, String sname, String email, int phone, double feebal, String
timings,
String trainer) {

super(sid, sname, email, phone);


[Link] = feebal;
[Link] = timings;
[Link] = trainer;
}

//Setters and Getters

@Override
public String toString() {
return [Link]() + feebal + ", " + timings + ", " + trainer;
}
}

6)[Link]
package [Link];

import [Link].*;
/*
* @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;

[Link] 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);


[Link] = yop;
[Link] = percentage;
[Link] = 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);


[Link] = yop;
[Link] = percentage;
[Link] = qualification;
}

//Setters and Getters

@Override
public String toString() {
return [Link]() + yop + ", " + percentage + ", " + qualification;
}
}

7)[Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@DiscriminatorValue("WESTU")
public class WeekendStudent extends CurrentStudent {

@Column(name = "wesalary")
private double wesalary;

[Link] 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);
[Link] = wesalary;
[Link] = wecompanyName;
[Link] = 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);
[Link] = wesalary;
[Link] = wecompanyName;
[Link] = weemail;
}
//Setters and Getters
@Override
public String toString() {
return [Link]() + wesalary + ", " + wecompanyName + ", " + weemail;
}
}

8)[Link]
package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */

[Link] 48 Hibernate5.4 Notes


public class HibernateUtil {

static SessionFactory sessionFactory;

static {
try {

Configuration cfg = new Configuration();

Properties props =new Properties();


[Link]([Link], "[Link]");
[Link]([Link], "jdbc:mysql://localhost:3306/myjtcdb");
[Link]([Link], "root");
[Link]([Link], "sompraksh");
[Link]([Link], "[Link].MySQL5Dialect");
[Link](Environment.SHOW_SQL, "true");
[Link](Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
[Link](Environment.HBM2DDL_AUTO, "update");

[Link](props);

[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);

StandardServiceRegistryBuilder ssrbuilder=new StandardServiceRegistryBuilder();


ServiceRegistry serviceReg=[Link]([Link]()).build();

sessionFactory = [Link](serviceReg);

}catch(Exception ex) {
[Link]();
}
}

public static SessionFactory getSessionFactory() {


return sessionFactory;
}
}

[Link] 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

[Link] 50 Hibernate5.4 Notes


B) Hibernate Persistence Classes
1) [Link]

@Entity
@Table(name="students")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Student{
@Id
@Column(name="sid")
private int sid;
...
}
2) [Link]

@Entity
@Table(name="cstudents")
public class CurrentStudent extends Student{
....
}

3) [Link]

@Entity
@Table(name="ostudents")
public class OldStudent extends Student{
....
}

4) [Link]

@Entity
@Table(name="wdstudents")
public class WeekdayStudent extends CurrentStudent{
....
}

5) [Link]

@Entity
@Table(name="westudents")
public class WeekendStudent extends CurrentStudent{
....
}

[Link] 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. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link] 6. [Link]
7. [Link] 8. [Link]
9. [Link]*

1) [Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class Lab6A {
public static void main(String[] args) {

Transaction tx = null;
try {
SessionFactory sessionFactory = [Link]();
Session session = [Link]();
tx = [Link]();

//1. Save Student - 1 record


int stuId=[Link]();
Student stu = new Student(stuId,"Som", "Som@jtc", 12345);
[Link](stu);

[Link] 52 Hibernate5.4 Notes


//[Link] CurrentStudent - 1 record
int currentStuId = [Link]();
CurrentStudent cstu= new
CurrentStudent(currentStuId,"aa","aa@jtc",111,5000,"11.00A.M","prakash");
[Link](cstu);

//[Link] OldStudent - 1 record


int oldStuId = [Link]();
OldStudent ostu= new OldStudent(oldStuId,"bb", "bb@jtc",222, 99.9, "Google",
"bb@[Link]");
[Link](ostu);

//[Link] WeekdayStudent - 1 record


int wdStuId = [Link]();
WeekdayStudent wdstu= new WeekdayStudent(wdStuId,"cc",
"cc@jtc",333,9000,"5.00P.M","Prakash",2020,65,"[Link]");
[Link](wdstu);

//[Link] WeekendStudent - 1 record


int weStuId = [Link]();
WeekendStudent westu= new WeekendStudent(weStuId,"dd",
"dd@jtc",444,7500,"7.30A.M","Prakash",19,"TCS","dd@[Link]");
[Link](westu);

[Link]();
[Link]();
} catch (Exception ex) {
[Link]();
[Link]();
}
}
}

2)[Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
public class Lab6B {
public static void main(String[] args) {

Transaction tx = null;

[Link] 53 Hibernate5.4 Notes


try {
SessionFactory sessionFactory = [Link]();
Session session = [Link]();
tx = [Link]();

//1. Load Student - fetching from Single Table


Student stu = [Link]([Link], 101);
[Link](stu);

//[Link] CurrentStudent - fetching from Single Table


CurrentStudent cstu=[Link]([Link], 201);
[Link](cstu);

//[Link] OldStudent - fetching from Single Table


OldStudent ostu=[Link]([Link], 301);
[Link](ostu);

//[Link] WeekdayStudent - fetching from Single Table


WeekdayStudent wdstu=[Link]([Link], 401);
[Link](wdstu);

//[Link] WeekendStudent - fetching from Single Table


WeekendStudent westu=[Link]([Link], 501);
[Link](westu);

[Link]();
[Link]();
} catch (Exception ex) {
[Link]();
[Link]();
}
}
}

3)[Link]
package [Link];

import [Link].*;
/*
* @Author : Som Prakash Rai
* @Company: Jtc India
* */
@Entity
@Table(name = "mystudents")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

[Link] 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) {


[Link] = sname;
[Link] = email;
[Link] = phone;
}

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


[Link] = sid;
[Link] = sname;
[Link] = email;
[Link] = phone;
}

//Setters and Getters

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

[Link] 55 Hibernate5.4 Notes


4) [Link]
package [Link];

import [Link].*;
/*
* @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);
[Link] = osalary;
[Link] = ocompanyName;
[Link] = oemail;
}

public OldStudent(int sid, String sname, String email, int phone, double osalary, String
ocompanyName,String oemail) {
super(sid, sname, email, phone);
[Link] = osalary;
[Link] = ocompanyName;
[Link] = oemail;
}

//Setters and Getters

@Override
public String toString() {
return [Link]() + osalary + ", " + ocompanyName + ", " + oemail;
}
}

[Link] 56 Hibernate5.4 Notes


5) [Link]
package [Link];

import [Link].*;
/*
* @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);
[Link] = feebal;
[Link] = timings;
[Link] = trainer;
}

public CurrentStudent(int sid, String sname, String email, int phone, double feebal, String
timings,
String trainer) {
super(sid, sname, email, phone);
[Link] = feebal;
[Link] = timings;
[Link] = trainer;
}
//Setters and Getters
@Override
public String toString() {
return [Link]() + feebal + ", " + timings + ", " + trainer;
}
}

[Link] 57 Hibernate5.4 Notes


6) [Link]
package [Link];

import [Link].*;
/*
* @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);
[Link] = yop;
[Link] = percentage;
[Link] = 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);
[Link] = yop;
[Link] = percentage;
[Link] = qualification;
}

//Setters and Getters

@Override
public String toString() {
return [Link]() + yop + ", " + percentage + ", " + qualification;
}
}

[Link] 58 Hibernate5.4 Notes


7) [Link]
package [Link];

import [Link].*;
/*
* @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);
[Link] = wesalary;
[Link] = wecompanyName;
[Link] = 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);
[Link] = wesalary;
[Link] = wecompanyName;
[Link] = weemail;
}
//Setters and Getters
@Override
public String toString() {
return [Link]() + wesalary + ", " + wecompanyName + ", " + weemail;
}
}

[Link] 59 Hibernate5.4 Notes


8)[Link]
package [Link];
/*
* @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)[Link]
package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

[Link] 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();


[Link]([Link], "[Link]");
[Link]([Link], "jdbc:mysql://localhost:3306/myjtcdb");
[Link]([Link], "root");
[Link]([Link], "Somprakash");
[Link]([Link], "[Link].MySQL5Dialect");
[Link](Environment.SHOW_SQL, "true");
[Link](Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
[Link](Environment.HBM2DDL_AUTO, "update");

[Link](props);

[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);

StandardServiceRegistryBuilder ssrbuilder=new StandardServiceRegistryBuilder();


ServiceRegistry serviceReg=[Link]([Link]()).build();

sessionFactory = [Link](serviceReg);

}catch(Exception ex) {
[Link]();
}
}

public static SessionFactory getSessionFactory() {


return sessionFactory;
}
}

[Link] 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:

[Link] 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:

[Link] 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:

[Link] 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

[Link] 65 Hibernate5.4 Notes

You might also like