Hibernate Inheritance Table Per Class

In this inheritance strategy, table per class is generated. It means a separate table is generated for each POJO class involved in the hierarchy. Unlike the SINGLE_TABLE strategy, there are no nullable values present in the tables. So, to overcome the disadvantages of the SINGLE_TABLE strategy, we use TABLE_PER_CLASS.

Syntax of TABLE_PER_CLASS Inheritance

@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)

Here, Inheritance Type defines the inheritance strategy we are using.

Example of TABLE_PER_CLASS Inheritance

In this example, we are going to take three classes; i.e., Payment.java, Card.java, and Cheque.java.

The class hierarchy is given below:-

Hibernate Inheritance Table Per Class

1) Create all POJO Classes

In this step, we are going to create all the POJO classes, i.e., Payment.java, Card.java, and Cheque.java.

Payment.java

 import javax.persistence.Column;
 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="p1")
 @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
 public class Payment {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="payid")
private int id;
@Column(name="amount")
private int amount;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
} 

Card.java

 import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="c1")
public class Card extends Payment {
@Column(name="cardnum")
private int cardno;
@Column(name="cardtype")
private String card_type;
public int getCardno() {
return cardno;
}
public void setCardno(int cardno) {
this.cardno = cardno;
}
public String getCard_type() {
return card_type;
}
public void setCard_type(String card_type) {
this.card_type = card_type;
}
} 

Cheque.java

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Table;
 @Entity
 @Table(name="ch1")
 public class Cheque extends Payment {
@Column(name="chequeno")
private int chequeno;
@Column(name="chequetype")
private String cheque_type;
public int getChequeno() {
return chequeno;
}
public void setChequeno(int chequeno) {
this.chequeno = chequeno;
}
public String getCheque_type() {
return cheque_type;
}
public void setCheque_type(String cheque_type) {
this.cheque_type = cheque_type;
}
} 

2) Create the Configuration file.

The configuration file contains the information of mapping classes and database. We are going to map all the POJO classes in the configuration file (hibernate.cfg.xml).

hibernate.cfg.xml

   
  
  
  
  
update  
org.hibernate.dialect.MySQL5Dialect

jdbc:mysql://
localhost:3306/example
com.mysql.jdbc.Driver

root  
root  
        



   
  
  

3) Create the main class that stores the object of POJO object

In this step, we are going to create the main class (which contains the main method) that stores the object of the POJO class.

App.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
  
public class App 
{
public static void main( String[] args )
{
Configuration cfg= new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory s= cfg.buildSessionFactory();
ssion session=s.openSession();
session.beginTransaction();
     
Payment pay=new Payment();
pay.setAmount(19800);
         
Card card= new Card();
card.setCardno(540213);
card.setCard_type("MASTER");
card.setAmount(8956);
        
Cheque cheque= new Cheque();
cheque.setChequeno(45630);
cheque.setCheque_type("ORDER");
cheque.setAmount(13654);
        
session.save(pay);
session.save(card);
session.save(cheque);
                                 
session.getTransaction().commit();
System.out.println("Successfull!!");     
}
}  

4. OUTPUT

Hibernate Inheritance Table Per Class 1

5.  DATABASE TABLES

payment table

Hibernate Inheritance Table Per Class 2

card table

Hibernate Inheritance Table Per Class 3

cheque table

Hibernate Inheritance Table Per Class 4

Disadvantages of TABLE_PER_CLASS inheritance strategy

Following are the problems in TABLE_PER_CLASS inheritance strategy:

  • The data belongs to the superclass is scattered across many subclasses. Hence, the repeated column (amount) is present in the subclasses.
  • Any changes made to the superclass will affect the tables of subclasses.

Although this strategy is better than the SINGLE_TABLE inheritance strategy, it also has a few disadvantages. To overcome these disadvantages, we use JOINED_TABLE inheritance strategy.  



ADVERTISEMENT
ADVERTISEMENT