Table per hierarchy Using Annotation
Here in this we create one table by different object without using mapping file .In this we using only Annotation.
Here are one mapping file
here we don't create mapping we only provide mapping class in the configuration file.
<mapping class="beans.Emp"/>
<mapping class="beans.Con_Emp"/>
<mapping class="beans.Reg_Emp"/>
Here one Configuration file
name is configuration.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="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>
Persistent Super class
package beans;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
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="e123")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="emp")
public class Emp {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
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;
}
}
Persistent child class Contract_emp
package beans;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
@DiscriminatorValue("contract+employee")
public class Con_Emp {
@Column(name="per_hour")
private int per_hour;
@Column(name="con_duration")
private String con_duration;
public int getPer_hour() {
return per_hour;
}
public void setPer_hour(int per_hour) {
this.per_hour = per_hour;
}
public String getCon_duration() {
return con_duration;
}
public void setCon_duration(String con_duration) {
this.con_duration = con_duration;
}
}
Persistent Child class Regular_emp
package beans;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@DiscriminatorValue("reg_emp")
public class Reg_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;
}
}
Here in this we create one table by different object without using mapping file .In this we using only Annotation.
Here are one mapping file
here we don't create mapping we only provide mapping class in the configuration file.
<mapping class="beans.Emp"/>
<mapping class="beans.Con_Emp"/>
<mapping class="beans.Reg_Emp"/>
Here one Configuration file
name is configuration.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="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>
Persistent Super class
package beans;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
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="e123")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="emp")
public class Emp {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
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;
}
}
Persistent child class Contract_emp
package beans;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
@DiscriminatorValue("contract+employee")
public class Con_Emp {
@Column(name="per_hour")
private int per_hour;
@Column(name="con_duration")
private String con_duration;
public int getPer_hour() {
return per_hour;
}
public void setPer_hour(int per_hour) {
this.per_hour = per_hour;
}
public String getCon_duration() {
return con_duration;
}
public void setCon_duration(String con_duration) {
this.con_duration = con_duration;
}
}
package beans;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@DiscriminatorValue("reg_emp")
public class Reg_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;
}
}
Test class
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 Store {
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");
con.setPer_hour(2);
con.setCon_duration("2 year");
reg.setBonus(200);
reg.setSalary(100000);
t.commit();
s.close();
System.out.println("everything is successfull");
}
}
No comments:
Post a Comment