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.  


Related Topics

Hibernate Session Interface

Hibernate Session Interface A Session is an interface between the Java application and the Hibernate. It is available in the org.hibernate.session package. It should not be kept open for a long time, as it...

4 minutes read.

Hibernate Second-Level Cache

The Second-level cache is related to the SessionFactory object. Once the SessionFactory is closed, all the second-level cache data associated with it will be lost. The cache manager will also get closed. Cache provider The...

5 minutes read.

Hibernate Named Query Using XML

A named query is a technique used to assemble all the queries (Native SQL and HQL) in a specific location and refer them by some name. It helps in reducing the mess...

3 minutes read.

Hibernate Caching

What is Caching? Caching is a process of saving data into the cache memory. A cache is a temporary storage layer which is used to increase the speed of data access. It allows...

2 minutes read.

Hibernate Many-to-One Mapping

Many-to-One Hibernate Mapping with Example A Many-to-One association mapping is the reverse of One-to-Many association mapping. For example, many (customers) are associated with one (vendor). In Hibernate, Many-to-One association mapping is applied from child class...

3 minutes read.

Dirty Checking in Hibernate

Dirty checking is an essential concept of Hibernate. The Dirty checking concept is used to keep track of the objects. It automatically detects whether an object is modified (or not) or wants to...

6 minutes read.

Lazy Loading in Hibernate

Lazy loading is a fetching technique used for all the entities in Hibernate. It decides whether to load a child class object while loading the parent class object. When we use association mapping...

3 minutes read.

Hibernate Criteria Query Language (HCQL)

Hibernate Criteria Query Language (HCQL) is mainly used for searching and fetching records. It works on the filtration rules and logical conditions. The Criteria interface, Restriction class, and Order class are available in...

4 minutes read.

Hibernate History and Versions

Next → History of Hibernate Hibernate was developed in 2001 by Gavin king with his colleagues from Circus Technologies. The main aim was to provide better persistence capabilities than those of EJB2, by reducing the...

2 minutes read.

Cascade in Hibernate

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/...

3 minutes read.

Hibernate first Example

Now we are going to create the first example in Hibernate. We will proceed step by step to develop the first Java application in Hibernate. The steps are listed below:- Create the POJO...

2 minutes read.

Hibernate Features

1. Lightweight Hibernate is a lightweight framework as it does not contains additional functionalities; it uses only those functionalities required for object-relational mapping. It is a lightweight framework because it uses persistent...

2 minutes read.

Hibernate Web Application with Example

Let us create a hibernate web Application. Here we need JSP file for presentation. There are steps given below for the web app creation. index.jsp This page will show a registration form. It takes the...

2 minutes read.

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

3 minutes read.

Hibernate Web Application Example

Web Application Example with Hibernate In this section, we create a hibernate web Application. Here we are using a JSP file for presentation. Example of creating a web application using hibernate index.jsp This page will show a...

2 minutes read.

Hibernate One-to-Many Mapping

One-to-Many Hibernate Mapping Example In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related...

3 minutes read.

Hibernate vs. JDBC

What is Hibernate? Hibernate is an Object-Relational Mapping (ORM) tool which reduces the difficulty in the application development. Hibernate provides a framework that interacts with the data stored in the databases. It also uses...

2 minutes read.

Hibernate First-Level Cache

The first-level cache is related to the Session object. First level cache objects of one Session are not visible to the other Sessions, and all the cache data is lost when the Session...

2 minutes read.

Hibernate Dialects

Hibernate Dialects Dialect is a Java class available in org.hibernate.dialect package, which helps to map Java Application with the database. To interact with the database, we need to define the required database dialect in...

2 minutes read.

Hibernate Configuration Properties

Hibernate Configuration Hibernate Configuration is a Java class, which allows a Java application to specify configuration parameters used in the application. As Hibernate is designed to serve in different environments, it needs a broad...

6 minutes read.