Joined Strategy in Hibernate using Annotations

In our previous example we have seen Single Table Strategy using Annotations. Here lets see the example of Joined Table in hibernate using Annotations

First let us understand the concept of Joined Strategy.

Joined Strategy

h1

 

Here we can see that for each class, Employee, Landline and Mobile separate tables are created and the data’s respective of those classes are saved. But the reference ID number is inserted in employee table. This is known as Joined Strategy

Lets see an example for this,

structure

employee.java

package com.javainfinite.pojo;

import java.io.Serializable;
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;
import javax.persistence.TableGenerator;

@Entity
@Table(name="Annotate_Employee")
@Inheritance(strategy=InheritanceType.JOINED)
public class employee {
    @Id
    @GeneratedValue
    private int id;
    private String ename;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    } 
}

landline.java

package com.javainfinite.pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class landline extends employee {
  
   
    private String tnumber;

    public String getTnumber() {
        return tnumber;
    }

    public void setTnumber(String tnumber) {
        this.tnumber = tnumber;
    }

    
}

mobile.java

package com.javainfinite.pojo;

import javax.persistence.Entity;

@Entity
public class mobile extends employee{
    
    private String tnumber;

    public String getTnumber() {
        return tnumber;
    }

    public void setTnumber(String tnumber) {
        this.tnumber = tnumber;
    }
    
}

NewHibernateUtil.java

package com.javainfinite.pojo;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;


public class NewHibernateUtil {

    private static final SessionFactory sessionFactory;
    
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

hibernate.cfg.xml

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.hbm2ddl.auto">create</property>  
    <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/hibernateannotation</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.show_sql">true</property>
    <mapping class="com.javainfinite.pojo.employee"/>
    <mapping class="com.javainfinite.pojo.landline"/>
    <mapping class="com.javainfinite.pojo.mobile"/>
  </session-factory>
</hibernate-configuration>

execution.java

package com.javainfinite.execute;

import com.javainfinite.pojo.NewHibernateUtil;
import com.javainfinite.pojo.employee;
import com.javainfinite.pojo.landline;
import com.javainfinite.pojo.mobile;
import org.hibernate.Session;
import org.hibernate.Transaction;


public class execution {
    
    public static void main(String args[])
    {
        
        employee emp=new employee();
        mobile mob=new mobile();
        landline land=new landline();
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        try
        {
            emp.setEname("Alpha");

            mob.setEname("Mobile Alpha");
            mob.setTnumber("Mob: 23432");
            
            land.setEname("LandLine Alpha");
            land.setTnumber("LandLine: 22343");
            
            Transaction trans=session.beginTransaction();
            session.save(emp);
            session.save(mob);
            session.save(land);
            session.close();
            trans.commit();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
}

Output:
op1

Annotate_Employee
op2

Here we can see the names and reference ID’s are inserted into Annotate_Employee table.

mobile
op3

landline
op4

 

 

 

By Sri

One thought on “Joined Strategy in Hibernate using Annotations”

Leave a Reply

Your email address will not be published. Required fields are marked *