Table per Concrete Class is one of the inheritance strategies in hibernate. In this tutorial we are going to implement the table per concrete class inheritance strategy. Here we are taking the same previous tutorial table per class strategy  example like Card, Cheque and Payment classes.

If we want to store each concrete class objects of inheritance in separate tables of database then we can go with table per concrete class strategy in hibernate.

Table per Concrete Class Strategy:

Table per ClassWhat is concrete class?

In our example, we have 3 classes in the hierarchy called Payment, Card and Cheque.  Here Card and Cheque classes are considered to be concrete classes. Because these classes are complete classes (We can create these objects and we can make use of them). Where as Payment class is not considered to be as concrete class because with out Card or Cheque there is no existence of Payment.

Example for Table per Concrete Class Strategy:

To implement the table per concrete class inheritance strategy in hibernate, we need to configure the all inheritance hierarchy classes in hbm.xml file. We need to configure the concrete classes with <union-subclass> tag under <class> tag.

Recommended : Table per Class Inheritance Strategy in hibernate

Data Base :

[sql]

CREATE TABLE `card_table` (
    `payid` INT(11) NULL DEFAULT NULL,
    `amount` DOUBLE NULL DEFAULT NULL,
    `paydate` DATE NULL DEFAULT NULL,
    `cardnumber` INT(11) NULL DEFAULT NULL,
    `cardtype` VARCHAR(50) NULL DEFAULT NULL
)
COLLATE=’latin1_swedish_ci’
ENGINE=InnoDB;

CREATE TABLE `cheque_table` (
    `payid` INT(11) NULL DEFAULT NULL,
    `amount` INT(11) NULL DEFAULT NULL,
    `paydate` DATE NULL DEFAULT NULL,
    `chqnumber` INT(11) NULL DEFAULT NULL,
    `chqtype` VARCHAR(50) NULL DEFAULT NULL
)
COLLATE=’latin1_swedish_ci’
ENGINE=InnoDB;

[/sql]

Payment.java

[java]
package com.onlinetutorialspoint.hibernate.model;

import java.util.Date;

public class Payment {

    private int paymentId;
    private double amount;
    private Date paymentDate;

    public int getPaymentId() {
        return paymentId;
    }

    public void setPaymentId(int paymentId) {
        this.paymentId = paymentId;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public Date getPaymentDate() {
        return paymentDate;
    }

    public void setPaymentDate(Date paymentDate) {
        this.paymentDate = paymentDate;
    }

}

[/java]

Card.java

[java]
package com.onlinetutorialspoint.hibernate.model;

public class Card extends Payment {

    private int cardNumber;
    private String cardType;

    public int getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(int cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getCardType() {
        return cardType;
    }

    public void setCardType(String cardType) {
        this.cardType = cardType;
    }

}

[/java]

Cheque.java

[java]
package com.onlinetutorialspoint.hibernate.model;

public class Cheque extends Payment {

    private int chequeNumber;
    private String chequeType;

    public int getChequeNumber() {
        return chequeNumber;
    }

    public void setChequeNumber(int chequeNumber) {
        this.chequeNumber = chequeNumber;
    }

    public String getChequeType() {
        return chequeType;
    }

    public void setChequeType(String chequeType) {
        this.chequeType = chequeType;
    }

}

[/java]

payment.hbm.xml

[xml highlight=”8-15″]</pre>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.onlinetutorialspoint.hibernate.model.Payment" table="payment">
<id name="paymentId" column="payid"/>
<property name="amount"/>
<property name="paymentDate" column="paydate" type="date"/>
<union-subclass name="com.onlinetutorialspoint.hibernate.model.Card" table="card_table">
<property name="cardNumber" column="cardnumber"/>
<property name="cardType" column="cardtype"/>
</union-subclass>
<union-subclass name="com.onlinetutorialspoint.hibernate.model.Cheque" table="cheque_table">
<property name="chequeNumber" column="chqnumber"/>
<property name="chequeType" column="chqtype"/>
</union-subclass>
</class>
</hibernate-mapping>

[/xml]

Recommended : Spring With Hibernate Integration Complete Example

PaymentDAO.java

[java]
package com.onlinetutorialspoint.hibernate.dao;

import com.onlinetutorialspoint.hibernate.model.Card;
import com.onlinetutorialspoint.hibernate.model.Cheque;

public interface PaymentDAO {

    public void saveCard(Card card);

    public void saveCheque(Cheque cheque);
}

[/java]

PaymentDAOImpl.java

[java]
package com.onlinetutorialspoint.hibernate.dao;

import com.onlinetutorialspoint.hibernate.model.Card;
import com.onlinetutorialspoint.hibernate.model.Cheque;
import com.onlinetutorialspoint.hibernate.util.HibernateUtil;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

public class PaymentDAOImpl implements PaymentDAO {

    public void saveCard(Card card) {
        SessionFactory factory = HibernateUtil.getInstnce();
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(card);
        tx.commit();
        session.close();
        System.out.println("Card Inserted Successfully..");

    }

    public void saveCheque(Cheque cheque) {
        SessionFactory factory = HibernateUtil.getInstnce();
        Session session = factory.openSession();
        Transaction tx = session.beginTransaction();
        session.save(cheque);
        tx.commit();
        session.close();
        System.out.println("Cheque Inserted Successfully..");
    }

}

[/java]

PaymentDAOFactory.java

[java]
package com.onlinetutorialspoint.hibernate.dao;

public class PaymentDAOFactory {

    public static PaymentDAO getInstance() {
        return new PaymentDAOImpl();
    }
}

[/java]

HibernateUtil.java

[java]
package com.onlinetutorialspoint.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private HibernateUtil() {

    }
    private static SessionFactory sessionFactory;

    public static synchronized SessionFactory getInstnce() {
        if (sessionFactory == null) {
            sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
        }
        return sessionFactory;

    }
}

[/java]

Main.java

[java]

import com.onlinetutorialspoint.hibernate.dao.PaymentDAO;
import com.onlinetutorialspoint.hibernate.dao.PaymentDAOFactory;
import com.onlinetutorialspoint.hibernate.model.Card;
import com.onlinetutorialspoint.hibernate.model.Cheque;
import java.util.Date;

public class Main {

    public static void main(String[] args) {

        Card card = new Card();
        card.setPaymentId(110006);
        card.setPaymentDate(new Date());
        card.setAmount(20000);
        card.setCardNumber(556613);
        card.setCardType("MASTRO");
        PaymentDAO dao = PaymentDAOFactory.getInstance();
        dao.saveCard(card);

        System.out.println("=========================");

        Cheque cheque = new Cheque();
        cheque.setPaymentId(2256125);
        cheque.setPaymentDate(new Date());
        cheque.setAmount(80000);
        cheque.setChequeNumber(456231);
        cheque.setChequeType("ORDER");
        dao.saveCheque(cheque);
    }
}

[/java]

Output :

Hibernate: insert into card_table (amount, paydate, cardnumber, cardtype, payid) values (?, ?, ?, ?, ?) 
Card Inserted Successfully.. 
========================= 
Hibernate: insert into cheque_table (amount, paydate, chqnumber, chqtype, payid) values (?, ?, ?, ?, ?) 
Cheque Inserted Successfully..

DataBase :

Table per Concrete Class ExampleBy running the above main class, we can get the above out put. And the data will be inserted in two different tables (card_table and cheque_table) in database.

Happy Learning 🙂