Example & Tutorial understanding programming in easy ways.

What are the Joined Subclasses in Hibernate?

Joined Subclasses in Hibernate: In table per class hierachy all the data was saved in a single table but here,


                         x number of classes = x number of tables in the database


If we save the CreditCard class object, then first hibernate will saves the data related to super class object into the super class related table in the database and then CreditCard object data in CreditCard related table in the database, So first base class data will be saved


We have required some files:


1. Payment.java (Base class)

2. CreditCard.java (Derived class)

3. Cheque.java (Derived class)

4. ClientForSave.java (for our logic)

5. Payment.hbm.xml

6. hibernate.cfg.xml


In Details of these file are as Below:


Payment.java:


package str;

public class Payment

{

    private int paymentId;

    private double amount;

    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;

    }      

}


CreditCard.java:


package str; 

public class CreditCard extends Payment

{

    private String CreditCardType; 

    public String getCreditCardType() 

{

        return CreditCardType;

    } 

    public void setCreditCardType(String creditCardType) 

{

        CreditCardType = creditCardType;

    } 

}


Cheque.java:


package str; 

public class Cheque extends Payment

    private String ChequeType;

    public String getChequeType() 

{

        return ChequeType;

    } 

    public void setChequeType(String chequeType) 

{

        ChequeType = chequeType;

    } 

}


ClientForSave.java


package str; 

import org.hibernate.*;

import org.hibernate.cfg.*;

public class ClientForSave 

    public static void main(String[] args)

    {

        Configuration cfg = new Configuration();

        cfg.configure("hibernate.cfg.xml"); 

        SessionFactory factory = cfg.buildSessionFactory();

        Session session = factory.openSession();

        CreditCard c=new CreditCard();

        c.setPaymentId(10);

        c.setAmount(2500);

        c.setCreditCardType("Visa");

        Cheque c1=new Cheque();

        c1.setPaymentId(11);

        c1.setAmount(2600);

        c1.setChequeType("ICICI");

        Transaction tx = session.beginTransaction();

        session.save(c);

        session.save(c1);

        System.out.println("Object saved successfully.....!!");

        tx.commit();

        session.close();

        factory.close();

    }

}


Payment.hbm.xml:


<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>

<class name="str.Payment" table="PAYMENT">

<id name="paymentId" column="pid" />

<property name="amount" column="amt" />

<joined-subclass name="str.CreditCard" table="CreditCard">

<key column="dummy1" />

<property name="CreditCardType" column="cctype" length="10" />

</joined-subclass>

<joined-subclass name="str.Cheque" table="Cheque">

<key column="dummy2" />

<property name="ChequeType" column="cqtype" length="10" />

</joined-subclass>

</class>

</hibernate-mapping>

 

hibernate.cfg.xml:


<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver

</property>

<property name="connection.url">jdbc:oracle:thin:1521:XE</property>

<property name="connection.username">system</property>

<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>

<property name="show_sql">true</property>

<property name="hbm2ddl.auto">update</property>

<mapping resource="Payment.hbm.xml" />

</session-factory>

</hibernate-configuration>

 

In case of Table Per Subclass, subclass mapped tables are related to parent class mapped table by primary key and foreign key relationship.


The element of class is used to map the child class with parent using the primary key and foreign key relation.


In this example, we are going to use hb2ddl.auto property to generate the table automatically. So we don't need to be worried about creating tables in the database.


Let's see the hierarchy of classes that we are going to map.


In case of table per subclass class, there will be three tables in the database, each representing a particular class.


The joined-subclass subelement of class, specifies the subclass. The key subelement of joined-subclass is used to generate the foreign key in the subclass mapped table. This foreign key will be associated with the primary key of parent class mapped table.

Read More →
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!