Inheritance Mapping using annotations
1) Table Per Hierarchy
In this strategy only one table will be created for the entire hierarchy.
Table will look like below
Player.java
package com.javatrainingschool;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Table(name = "player_1")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "player_type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue(value = "player")
public class Player {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Player [id=" + id + ", name=" + name + "]";
}
public Player(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Player() {
super();
// TODO Auto-generated constructor stub
}
}
Cricketer.java
package com.javatrainingschool;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue ("cricketer")
public class Cricketer extends Player {
//inherited properties
//id
//name
private String type;
private String jerseyNumber;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getJerseyNumber() {
return jerseyNumber;
}
public void setJerseyNumber(String jerseyNumber) {
this.jerseyNumber = jerseyNumber;
}
@Override
public String toString() {
return "Cricketer [type=" + type + ", jerseyNumber=" + jerseyNumber + "]";
}
public Cricketer(int id, String name, String type, String jerseyNumber) {
super(id, name);
this.type = type;
this.jerseyNumber = jerseyNumber;
}
public Cricketer() {
super();
// TODO Auto-generated constructor stub
}
public Cricketer(int id, String name) {
super(id, name);
// TODO Auto-generated constructor stub
}
}
TennisPlayer.java
package com.javatrainingschool;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue ("Tennis Player")
public class TennisPlayer extends Player{
//inherited properties
//id, name
private int noOfMatches;
private int wonMatches;
public int getNoOfMatches() {
return noOfMatches;
}
public void setNoOfMatches(int noOfMatches) {
this.noOfMatches = noOfMatches;
}
public int getWonMatches() {
return wonMatches;
}
public void setWonMatches(int wonMatches) {
this.wonMatches = wonMatches;
}
public TennisPlayer(int id, String name, int noOfMatches, int wonMatches) {
super(id, name);
this.noOfMatches = noOfMatches;
this.wonMatches = wonMatches;
}
public TennisPlayer() {
super();
// TODO Auto-generated constructor stub
}
public TennisPlayer(int id, String name) {
super(id, name);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "TennisPlayer [noOfMatches=" + noOfMatches + ", wonMatches=" + wonMatches + "]";
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9iDialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521/xepdb1</property>
<property name="connection.username">system</property>
<property name="connection.password">test</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping class="com.sks.entity.Player"/>
<mapping class="com.sks.entity.Cricketer"/>
<mapping class="com.sks.entity.TennisPlayer"/>
</session-factory>
</hibernate-configuration>
TestingMainClass.java
package com.javatrainingschool;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class TestingMainClass {
public static void main(String[] args) {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory factory = meta.getSessionFactoryBuilder().build();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Player p1 = new Player();
p1.setName("Neymar");
Cricketer c1 = new Cricketer();
c1.setJerseyNumber("18");
c1.setName("Virat Kohli");
c1.setType("Batsman");
TennisPlayer t1 = new TennisPlayer();
t1.setName("Rafael Nadal");
t1.setNoOfMatches(143);
t1.setWonMatches(101);
session.persist(p1);
session.persist(c1);
session.persist(t1);
t.commit();
session.close();
factory.close();
System.out.println("Data saved successfully.");
}
}
Verify Table Data
2) Table Per Concrete Class
In this approach, Player class annotations will change.
Player.java
package com.sks.entity;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Table(name = "player_1")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Player {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Player [id=" + id + ", name=" + name + "]";
}
public Player(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Player() {
super();
// TODO Auto-generated constructor stub
}
}
Cricketer.java
package com.sks.entity;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "cricketer")
public class Cricketer extends Player {
//inherited properties
//id
//name
private String type;
private String jerseyNumber;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getJerseyNumber() {
return jerseyNumber;
}
public void setJerseyNumber(String jerseyNumber) {
this.jerseyNumber = jerseyNumber;
}
@Override
public String toString() {
return "Cricketer [type=" + type + ", jerseyNumber=" + jerseyNumber + "]";
}
public Cricketer(int id, String name, String type, String jerseyNumber) {
super(id, name);
this.type = type;
this.jerseyNumber = jerseyNumber;
}
public Cricketer() {
super();
// TODO Auto-generated constructor stub
}
public Cricketer(int id, String name) {
super(id, name);
// TODO Auto-generated constructor stub
}
}
TennisPlayer.java
package com.sks.entity;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "tennisplayer")
public class TennisPlayer extends Player{
//inherited properties
//id, name
private int noOfMatches;
private int wonMatches;
public int getNoOfMatches() {
return noOfMatches;
}
public void setNoOfMatches(int noOfMatches) {
this.noOfMatches = noOfMatches;
}
public int getWonMatches() {
return wonMatches;
}
public void setWonMatches(int wonMatches) {
this.wonMatches = wonMatches;
}
public TennisPlayer(int id, String name, int noOfMatches, int wonMatches) {
super(id, name);
this.noOfMatches = noOfMatches;
this.wonMatches = wonMatches;
}
public TennisPlayer() {
super();
// TODO Auto-generated constructor stub
}
public TennisPlayer(int id, String name) {
super(id, name);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "TennisPlayer [noOfMatches=" + noOfMatches + ", wonMatches=" + wonMatches + "]";
}
}
In this approach, following 3 tables will be created
player_1
cricketer
tennis player
3) Table Per Sub Class
Player.java
package com.sks.entity;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Table(name = "player_1")
@Inheritance(strategy = InheritanceType.JOINED)
public class Player {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Player [id=" + id + ", name=" + name + "]";
}
public Player(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Player() {
super();
// TODO Auto-generated constructor stub
}
}
Cricketer.java
package com.sks.entity;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "cricketer")
public class Cricketer extends Player {
//inherited properties
//id
//name
private String type;
private String jerseyNumber;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getJerseyNumber() {
return jerseyNumber;
}
public void setJerseyNumber(String jerseyNumber) {
this.jerseyNumber = jerseyNumber;
}
@Override
public String toString() {
return "Cricketer [type=" + type + ", jerseyNumber=" + jerseyNumber + "]";
}
public Cricketer(int id, String name, String type, String jerseyNumber) {
super(id, name);
this.type = type;
this.jerseyNumber = jerseyNumber;
}
public Cricketer() {
super();
// TODO Auto-generated constructor stub
}
public Cricketer(int id, String name) {
super(id, name);
// TODO Auto-generated constructor stub
}
}
TennisPlayer.java
package com.sks.entity;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "tennisplayer")
public class TennisPlayer extends Player{
//inherited properties
//id, name
private int noOfMatches;
private int wonMatches;
public int getNoOfMatches() {
return noOfMatches;
}
public void setNoOfMatches(int noOfMatches) {
this.noOfMatches = noOfMatches;
}
public int getWonMatches() {
return wonMatches;
}
public void setWonMatches(int wonMatches) {
this.wonMatches = wonMatches;
}
public TennisPlayer(int id, String name, int noOfMatches, int wonMatches) {
super(id, name);
this.noOfMatches = noOfMatches;
this.wonMatches = wonMatches;
}
public TennisPlayer() {
super();
// TODO Auto-generated constructor stub
}
public TennisPlayer(int id, String name) {
super(id, name);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "TennisPlayer [noOfMatches=" + noOfMatches + ", wonMatches=" + wonMatches + "]";
}
}
player_1 table
cricketer table
tennis player table