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

Module 3 - Inheritance Mapping (1)

useful for inheritance
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)
8 views39 pages

Module 3 - Inheritance Mapping (1)

useful for inheritance
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
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;

}

Java Learning Center 29 Hibernate Study Guide


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.

Java Learning Center 30 Hibernate Study Guide


 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

Java Learning Center 31 Hibernate Study Guide


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

Java Learning Center 32 Hibernate Study Guide


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

import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
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("Sri", "Sri@jlc", 12345);
session.save(stu);

//2.Save CurrentStudent - Inserts 2 records


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

//3.Save OldStudent - Inserts 2 records


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

//4.Save WeekdayStudent - Inserts 3 records


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

session.save(wdstu);

Java Learning Center 33 Hibernate Study Guide


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

session.save(westu);

tx.commit();
session.close();

} catch (Exception ex) {


tx.rollback();
ex.printStackTrace();
}
}
}

2)Lab4B.java
package com.jlcindia.hibernate;

import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
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);

Java Learning Center 34 Hibernate Study Guide


//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.jlcindia.hibernate;

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

@Entity
@Table(name = "mystudents")
@Inheritance(strategy = InheritanceType.JOINED)
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;

Java Learning Center 35 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

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

Java Learning Center 36 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

@Entity
@Table(name = "cstudents")
@PrimaryKeyJoinColumn(name = "mysid")
public class CurrentStudent extends Student{

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

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

Java Learning Center 37 Hibernate Study Guide


@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.jlcindia.hibernate;

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@Entity
@Table(name = "wdstudents")
@PrimaryKeyJoinColumn(name = "mysid")
public class WeekdayStudent extends CurrentStudent {

Java Learning Center 38 Hibernate Study Guide


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

Java Learning Center 39 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@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;
}
}

Java Learning Center 40 Hibernate Study Guide


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

Java Learning Center 41 Hibernate Study Guide


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

Java Learning Center 42 Hibernate Study Guide


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

import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

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("Sri", "Sri@jlc", 12345);
session.save(stu);

//2.Save CurrentStudent - 1 record


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

//3.Save OldStudent - 1 record


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

//4.Save WeekdayStudent - 1 record


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

Java Learning Center 43 Hibernate Study Guide


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

tx.commit();
session.close();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
}

}
}

2)Lab4B.java
package com.jlcindia.hibernate;

import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
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);

Java Learning Center 44 Hibernate Study Guide


//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.jlcindia.hibernate;

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@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;

Java Learning Center 45 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

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

Java Learning Center 46 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@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;

Java Learning Center 47 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@Entity
@DiscriminatorValue("WDSTU")
public class WeekdayStudent extends CurrentStudent {

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

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

Java Learning Center 48 Hibernate Study Guide


@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.jlcindia.hibernate;

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@Entity
@DiscriminatorValue("WESTU")
public class WeekendStudent extends CurrentStudent {

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

Java Learning Center 49 Hibernate Study Guide


@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.jlcindia.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 : Srinivas Dande
* @Company: Java Learning Center
* */

Java Learning Center 50 Hibernate Study Guide


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/myjlcdb");
props.put(Environment.USER, "root");
props.put(Environment.PASS, "srinivas");
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;
}
}

Java Learning Center 51 Hibernate Study Guide


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

Java Learning Center 52 Hibernate Study Guide


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

Java Learning Center 53 Hibernate Study Guide


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

import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
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,"Sri", "Sri@jlc", 12345);
session.save(stu);

Java Learning Center 54 Hibernate Study Guide


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

//3.Save OldStudent - 1 record


int oldStuId = PKGenerator.getNextOldStudentId();
OldStudent ostu= new OldStudent(oldStuId,"bb", "bb@jlc",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@jlc",333,9000,"5.00P.M","Dandes",2020,65,"B.Tech");
session.save(wdstu);

//5.Save WeekendStudent - 1 record


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

tx.commit();
session.close();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}

2)Lab6B.java
package com.jlcindia.hibernate;

import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Lab6B {
public static void main(String[] args) {

Transaction tx = null;

Java Learning Center 55 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@Entity
@Table(name = "mystudents")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

Java Learning Center 56 Hibernate Study Guide


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

Java Learning Center 57 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@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;
}
}

Java Learning Center 58 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@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;
}
}

Java Learning Center 59 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@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;
}
}

Java Learning Center 60 Hibernate Study Guide


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

import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@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;
}
}

Java Learning Center 61 Hibernate Study Guide


8)PKGenerator.java
package com.jlcindia.hibernate;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
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.jlcindia.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;

Java Learning Center 62 Hibernate Study Guide


/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
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/myjlcdb");
props.put(Environment.USER, "root");
props.put(Environment.PASS, "srinivas");
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;
}
}

Java Learning Center 63 Hibernate Study Guide


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:

Java Learning Center 64 Hibernate Study Guide


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:

Java Learning Center 65 Hibernate Study Guide


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:

Java Learning Center 66 Hibernate Study Guide


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;

//Constrcutors
//Setters and Getters
}

class SilverCustomer extends Customer{


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

//Constrcutors
//Setters and Getters

class GoldCustomer extends Customer{


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

//Constrcutors
//Setters and Getters

Java Learning Center 67 Hibernate Study Guide

You might also like