Table Per Subclass Using Annotation

As we have specified earlier, in case of table per subclass strategy, tables are created as per persistent classes but they are created using primary and foreign key. So there will not be duplicate columns in the relation.
We need to specify @Inheritance(strategy=InheritanceType.JOINED) in the parent class and @PrimaryKeyJoinColumnannotation in the subclasses.

package beans;

import javax.persistence.Column;
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;

@Entity
@Table(name="tps4")
@Inheritance(strategy=InheritanceType.JOINED)

public class Emp {
@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;
}

}
========================================================================
package beans;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="tps5")
@PrimaryKeyJoinColumn(name="id")
public class Con_Emp extends Emp {
@Column(name="salary")
private int salary;
@Column(name="bonus")
private int bonus;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}

}

========================================================================
package beans;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="tps6")
@PrimaryKeyJoinColumn(name="id")
public class Reg_Emp extends Emp {
@Column(name="pey_per_hour")
private int pey_per_hour;
@Column(name="contract_duratition")
private String contract_duration;
public int getPey_per_hour() {
return pey_per_hour;
}
public void setPey_per_hour(int pey_per_hour) {
this.pey_per_hour = pey_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
}

}

========================================================================

<?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="hbm2ddl.auto">create</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
<property name="connection.url">jdbc:mysql://localhost:3306/db</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password">lk@9616918397y</property>  
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
   <mapping class="beans.Emp"/>
<mapping class="beans.Con_Emp"/>
   <mapping class="beans.Reg_Emp"/>
             
          </session-factory>
          </hibernate-configuration>

========================================================================
package test;

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

import beans.Con_Emp;
import beans.Emp;
import beans.Reg_Emp;

public class Test {
public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("rsc/configuration.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
Transaction t=s.beginTransaction();
Emp e=new Emp();
Con_Emp con=new Con_Emp();
Reg_Emp reg=new Reg_Emp();
 
e.setId(12);
e.setName("yadav");
 
reg.setPey_per_hour(2);
reg.setContract_duration("2 year");
 
con.setBonus(200);
con.setSalary(100000);
t.commit();
s.close();
System.out.println("everything is successfull");
}
}


No comments:

Post a Comment