Table Per Concrete Using Annotation
It is same as tph annotation .In this some annotation are added.It create table in database for each persistence class super as well as child class.
Here some of annotation which are added in the tpc .
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
it shows that we are going to start table per class for child class.
@AttributeOverrides({
@AttributeOverride(name="string",column=@Column(name="column name"))
}):
This annotation is use for override super class data field (parent table column) in to child class field(child table )
Disadvantage:
This create duplicate column in the table.
Some of step to create table per class
Super class Emp
package beans;
import javax.annotation.Generated;
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="tpc0")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Emp {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="name")
private String name;
public Emp() {
}
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;
}
}
Child Class Con_Emp
package beans;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name="tpc3")
@PrimaryKeyJoinColumn(name="id")
@AttributeOverrides({
@AttributeOverride(name="id", column=@Column(name="id")),
@AttributeOverride(name="name", column=@Column(name="name"))
})
public class Con_Emp extends Emp{
@Column(name="pey_per_hour")
private int pey_per_hour;
@Column(name="contract_duration")
private String contract_duration;
public Con_Emp() {
super();
}
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;
}
}
Child Class Reg_Emp
package beans;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name="tpc2")
@PrimaryKeyJoinColumn(name="id")
@AttributeOverrides({
@AttributeOverride(name="id", column=@Column(name="id")),
@AttributeOverride(name="name",column=@Column(name="name"))
})
public class Reg_Emp extends Emp{
@Column(name="salary")
private int salary;
@Column(name="bonus")
private int bonus;
public Reg_Emp() {
super();
}
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;
}
}
It is same as tph annotation .In this some annotation are added.It create table in database for each persistence class super as well as child class.
Here some of annotation which are added in the tpc .
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
it shows that we are going to start table per class for child class.
@AttributeOverrides({
@AttributeOverride(name="string",column=@Column(name="column name"))
}):
This annotation is use for override super class data field (parent table column) in to child class field(child table )
Disadvantage:
This create duplicate column in the table.
Some of step to create table per class
- create super class and child class
- configuration file
- test class
Super class Emp
package beans;
import javax.annotation.Generated;
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="tpc0")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Emp {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="name")
private String name;
public Emp() {
}
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.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name="tpc3")
@PrimaryKeyJoinColumn(name="id")
@AttributeOverrides({
@AttributeOverride(name="id", column=@Column(name="id")),
@AttributeOverride(name="name", column=@Column(name="name"))
})
public class Con_Emp extends Emp{
@Column(name="pey_per_hour")
private int pey_per_hour;
@Column(name="contract_duration")
private String contract_duration;
public Con_Emp() {
super();
}
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;
}
}
package beans;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name="tpc2")
@PrimaryKeyJoinColumn(name="id")
@AttributeOverrides({
@AttributeOverride(name="id", column=@Column(name="id")),
@AttributeOverride(name="name",column=@Column(name="name"))
})
public class Reg_Emp extends Emp{
@Column(name="salary")
private int salary;
@Column(name="bonus")
private int bonus;
public Reg_Emp() {
super();
}
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;
}
}
Configuration file
<?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>
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 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");
con.setPey_per_hour(2);
con.setContract_duration("2 year");
reg.setBonus(200);
reg.setSalary(100000);
t.commit();
s.close();
System.out.println("everything is successfull");
}
}
No comments:
Post a Comment