We can map each class in individual table in database using JOINED strategy.
using @Inheritance(strategy=InheritanceType.JOINED) annotation, we can define this strategy. In this approach, primary key of the super class works as foreign key in sub class.So at the time of dealing with data, Hibernate Joins the tables for getting consolidated values for Subclass Object.
you can use @PrimaryKeyJoinColumn and @PrimaryKeyJoinColumns annotations to define the primary key(s) of the joined subclass table.
This approach is better than single table strategy.
package domain;
import javax.persistence.*;
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Employee{
private int empId;
private String name;
private String email;
@Id @GeneratedValue
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package domain;
import javax.persistence.*;
@Entity
public class FullTimeEmployee extends Employee{
private String designation;
private String empCode;
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getEmpCode() {
return empCode;
}
public void setEmpCode(String empCode) {
this.empCode = empCode;
}
@Override
public String toString() {
return "FullTimeEmployee [designation=" + designation + ", empCode="
+ empCode + ", getEmpId()=" + getEmpId() + ", getName()="
+ getName() + ", getEmail()=" + getEmail() + "]";
}
}
package domain;
import javax.persistence.*;
@Entity
public class PartTimeEmployee extends Employee{
private int duration;
private String reportTo;
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public String getReportTo() {
return reportTo;
}
public void setReportTo(String reportTo) {
this.reportTo = reportTo;
}
@Override
public String toString() {
return "PartTimeEmployee [duration=" + duration + ", reportTo="
+ reportTo + ", getEmpId()=" + getEmpId() + ", getName()="
+ getName() + ", getEmail()=" + getEmail() + "]";
}
}
Tables :
using @Inheritance(strategy=InheritanceType.JOINED) annotation, we can define this strategy. In this approach, primary key of the super class works as foreign key in sub class.So at the time of dealing with data, Hibernate Joins the tables for getting consolidated values for Subclass Object.
you can use @PrimaryKeyJoinColumn and @PrimaryKeyJoinColumns annotations to define the primary key(s) of the joined subclass table.
This approach is better than single table strategy.
package domain;
import javax.persistence.*;
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Employee{
private int empId;
private String name;
private String email;
@Id @GeneratedValue
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package domain;
import javax.persistence.*;
@Entity
public class FullTimeEmployee extends Employee{
private String designation;
private String empCode;
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getEmpCode() {
return empCode;
}
public void setEmpCode(String empCode) {
this.empCode = empCode;
}
@Override
public String toString() {
return "FullTimeEmployee [designation=" + designation + ", empCode="
+ empCode + ", getEmpId()=" + getEmpId() + ", getName()="
+ getName() + ", getEmail()=" + getEmail() + "]";
}
}
package domain;
import javax.persistence.*;
@Entity
public class PartTimeEmployee extends Employee{
private int duration;
private String reportTo;
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public String getReportTo() {
return reportTo;
}
public void setReportTo(String reportTo) {
this.reportTo = reportTo;
}
@Override
public String toString() {
return "PartTimeEmployee [duration=" + duration + ", reportTo="
+ reportTo + ", getEmpId()=" + getEmpId() + ", getName()="
+ getName() + ", getEmail()=" + getEmail() + "]";
}
}
Tables :
Comments
@DiscriminatorColumn(name="EMP_TYPE" , discriminatorType=DiscriminatorType.STRING) is not required.