Alphalearningschool
        

<<< Prev

Next >>>

Up
Table Per Hierarchy using Annotation

Hibernate Table Per Hierarchy using Annotation: Here we will use annotation. Here we need to use @Inheritance (strategy=InheritanceType.SINGLE_TABLE), @DiscriminatorColumn and @DiscriminatorValue annotations for mapping table per hierarchy strategy. Here are the steps:

  1. Create the persistence classes.
  2. Edit pom.xml file.
  3. Create the configuration file.
  4. Create the class to store the fetch the data.

      1.  Employee.java

package qualde.com;

import javax.persistence.*;

@Entity  
	@Table(name = "Employee")  
	@Inheritance(strategy=InheritanceType.SINGLE_TABLE)  
	@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)  
	@DiscriminatorValue(value="employee")  
	  
	public class Employee {  
	@Id  
	@GeneratedValue(strategy=GenerationType.AUTO)  
	      
	@Column(name = "id")  
	private int id;  
	  
	@Column(name = "name")  
	private String name;

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}  
	    
	}

       Permanent_Employee.java

package qualde.com;

import javax.persistence.*;

@Entity  
@DiscriminatorValue("Permanentemployee")  
public class Permanent_Employee extends Employee{  
      
@Column(name="salary")    
private float salary;  
  
@Column(name="Overtime")     
private int Overtime;

public float getSalary() {
	return salary;
}

public void setSalary(float salary) {
	this.salary = salary;
}

public int getOvertime() {
	return Overtime;
}

public void setOvertime(int overtime) {
	Overtime = overtime;
}
} 

        Temporary_Employee class :

package qualde.com;
import javax.persistence.*;
@Entity  
@DiscriminatorValue("Temporaryemployee")  
public class Temporary_Employee extends Employee{  
      
    @Column(name="pay_per_day")  
    private float pay_per_day;  
      
    @Column(name="contractPeroid")  
    private String contractPeroid;

	public float getPay_per_day() {
		return pay_per_day;
	}

	public void setPay_per_day(float pay_per_day) {
		this.pay_per_day = pay_per_day;
	}

	public String getContractPeroid() {
		return contractPeroid;
	}

	public void setContractPeroid(String contractPeroid) {
		this.contractPeroid = contractPeroid;
	}
}

     

     2.Pom.xml file :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>qualde.com</groupId>
  <artifactId>TablePerHierarchy</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>TablePerHierarchy</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>  
    <groupId>org.hibernate</groupId>  
    <artifactId>hibernate-core</artifactId>  
    <version>5.3.1.Final</version>  
</dependency>


<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>
    
    
    
  </dependencies>
</project>

 

    3. hibernate.cfg.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">  
  
<hibernate-configuration>  
    <session-factory>  
        <property name="hbm2ddl.auto">update</property>  
       <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/emp2</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property> 
   
        <mapping class="qualde.com.Employee"/>  
        <mapping class="qualde.com.Permanent_Employee"/>  
        <mapping class="qualde.com.Temporary_Employee"/>  
    </session-factory>  
  
</hibernate-configuration>

 

      4.Storedata.java

package qualde.com;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class StoreTest {  
	  
    public static void main(String args[])  
    {  
        StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
        Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();  
          
        SessionFactory factory=meta.getSessionFactoryBuilder().build();  
        Session session=factory.openSession();  
          
         Transaction t=session.beginTransaction();    
            
            Employee e1=new Employee();    
            e1.setName("Manish Kumar");    
                
            Permanent_Employee e2=new Permanent_Employee();    
            e2.setName("Chintan Joshi");    
            e2.setSalary(20000);    
            e2.setOvertime(5);    
                
            Temporary_Employee e3=new Temporary_Employee();    
            e3.setName("Harsh Kumar");    
            e3.setPay_per_day(30);    
            e3.setContractPeroid("15 Days");    
                
            session.persist(e1);    
            session.persist(e2);    
            session.persist(e3);    
                
            t.commit();    
            session.close();    
            System.out.println("success");        
    }  
}

 

        Now check your database.(run storedata.java class)