Table per class hierarchy mapping in hibernate


Representing an inheritance relationship using Table per class hierarchy

An entire class hierarchy could be mapped to a single table. This table would include columns for all properties of all classes in the hierarchy. The concrete subclass represented by a particular row is identified by the value of a type discriminator column. This mapping strategy is a winner in terms of both performance and simplicity


How to persist object of Details in database
Steps:
1.       Create hibernate application and include hibernate library
2.       Create java bean class which represent inheritance relationship

public class User {

    String uname;
    String upass;

     public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpass() {
        return upass;
    }

    public void setUpass(String upass) {
        this.upass = upass;
    }
}

public class Detail extends User {

    String urole;
    String state;

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getUrole() {
        return urole;
    }

    public void setUrole(String urole) {
        this.urole = urole;
    }
}

3.       Create configuration file

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/data</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="tableperclass/hibernate.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


4.       Create mapping file

<subclass> sub element of <class> is used to represent the relationship with super class

<hibernate-mapping>
<class name="tableperclass.User" table="user" catalog="data">
    <id name="uname" column="uname" type="string" length="20">
        <generator class="assigned" />
    </id>
    <discriminator column="desc" type="string" length="20"/>
    <property name="upass" column="upass" type="string" length="20"/>
    <subclass name="tableperclass.Detail" discriminator-value="dc">
        <property name="state" column="state" type="string" length="20"/>
        <property name="urole" column="urole" type="string" length="20"/>
    </subclass>
</class>
</hibernate-mapping>

5.       Create persistence object
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Session session = null;
        Transaction tx = null;
        SessionFactory factory = null;
        try {
            Configuration cfg = new Configuration().configure();
            factory = cfg.buildSessionFactory();
            session = factory.openSession();
            tx = session.beginTransaction();
            Detail d = new Detail();
            d.setUname("raju");
            d.setUpass("pass");
            d.setUrole("admin");
            d.setState("bihar");
            session.save(d);
            tx.commit();
        } catch (Exception e) {
            System.err.println(e);
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (Exception e1) {
                    System.out.print("Error" + e1);
                }
            }

        } finally {
            if (session != null) {
                try {
                    session.flush();
                    session.close();
                } catch (Exception e2) {
                    System.out.print("Error" + e2);
                }
            }
        }

    }
}


CREATE TABLE `user` (
  `uname` varchar(20) NOT NULL,
  `upass` varchar(20) default NULL,
  `urole` varchar(20) default NULL,
  `state` varchar(20) default NULL,
  `desc` varchar(20) default NULL,
  PRIMARY KEY  (`uname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

No comments:

Post a Comment