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

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

4. DATABASE TABLES
payment table

card table

cheque table

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.