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 classes and the parent class are mapped in the separate database table. To overcome the disadvantages of TABLE_PER_CLASS strategy, we are using JOINED table strategy. Hence, there is no repeated column present in the subclass tables.

Syntax of JOINED Table Inheritance

@Inheritance(strategy=InheritanceType.JOINED)

Here, InheritanceType defines the inheritance strategy we are using.

Example of JOINED table inheritance

Let’s understand this strategy by an example. Here, we are taking three classes that are Payment.java, Card.java, and Cheque.java.

The class hierarchy is given below:-

Hibernate Inheritance Joined Table

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

Card.java

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Table;
 
 @Entity
 @Table(name="ccd")
 
 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="ch")
 
 public class Cheque extends Payment {
  
  @Column(name="chqno") 
  private int chqno;
  @Column(name="chqtype")
  private String chq_type;
  
  public int getChqno() {
  return chqno;
  }
  public void setChqno(int chqno) {
  this.chqno = chqno;
  }
  public String getChq_type() {
  return chq_type;
  }
  public void setChq_type(String chq_type) {
  this.chq_type = chq_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/jyotika

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();
Session sess= s.openSession();
sess.beginTransaction();
     
Payment payment= new Payment();
payment.setAmount(6900);
    
         
Card card= new Card();
card.setCardno(645325);
card.setCard_type("Visa");
card.setAmount(1500);
         
Cheque cheque=new Cheque();
cheque.setAmount(1600);
cheque.setChqno(102265);
cheque.setChq_type("Order");
         
sess.save(payment);
sess.save(card);
sess.save(cheque);
sess.getTransaction().commit();
     
System.out.println("successfully inserted");
}
} tem.out.println("successfully inserted");
}
} 

OUTPUT

Hibernate Inheritance Joined Table 1

4. DATABASE TABLES

payment table

Hibernate Inheritance Joined Table 2

card table         

Hibernate Inheritance Joined Table 3

cheque table

Hibernate Inheritance Joined Table 4

The JOINED table is the best strategy among all the inheritance strategies. Following are the advantages of this strategy:

  • Repeated columns are not present in the database tables.
  • Nullable values are not present in the table.

Related Topics

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 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 Annotation with Example

Hibernate is an ORM (Object-Relational Mapping) tool which simplifies the application development. Hibernate provides a framework which interacts with the data stored in the databases and it also uses the specifications of the...

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

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

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 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 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 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 save() vs persist()

Session.save() The session.save() method is used to save the transient object by assigning a generated value or using a current value of the identifier property. In other words, the save() method is used...

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

Next → Hibernate SessionFactory A SessionFactory is an interface in Hibernate that create the instances of Session interface. It is a heavy weight object, and it is usually created during the application...

3 minutes read.