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

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 Tutorial

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

3 minutes read.

Hibernate Inheritance Joined Table

In this inheritance strategy, mapping of the child class fields is done with the common fields of the parent class in a separate table. In other words, the common entities between the child...

3 minutes read.

Hibernate One-to-One Mapping Example

One-to-One Hibernate Mapping In One-to-One association mapping, one object of a persistent class is related to one object of another persistent class. Here, we are going to create an example of one-to-one mapping using...

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 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 Query Language (HQL)

Hibernate provides its query language called Hibernate Query Language (HQL). HQL is an object-oriented query language, similar to the native SQL language, and it works with persistent objects. It is a database-independent and...

2 minutes read.

Hibernate N+1 Select Problem

Hibernate N + 1 Select Problem The N + 1 Select problem is a performance issue in Hibernate. In this problem, a Java application makes N + 1 database calls (N = number...

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 Inheritance - Single Table

Hibernate Inheritance In this inheritance strategy, only one table is created for all the classes involved in the hierarchy with an additional column known as a discriminator column. The discriminator column helps indifferentiating between...

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

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 Inverse

Hibernate Inverse An Inverse attribute is used to maintain the relationship between the parent and child class object. The inverse attribute is used only with bi-directional mappings such as one-to-many and many-to-many Hibernate mapping. In Hibernate, the...

2 minutes read.

Hibernate Lifecycle

Hibernate Lifecycle In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do...

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

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 merge() vs. update()

In Hibernate, both update() and merge() methods are used to convert the detached state object into the persistent state. session.merge() The merge() method is used when we want to change a detached entity into...

2 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 Merge

Hibernate Merge Merge is a method available in Hibernate which is used to update the existing records. However, it creates a copy from the passed entity objects and returns it. In other words, the merge()...

2 minutes read.