Senin, 12 Januari 2015

Hibernate Table Per Subclass using Annotation Tutorial with examples

As we have specified earlier, in case of table per subclass strategy, tables are created as per persistent classes but they are reated 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 @PrimaryKeyJoinColumn annotation in the subclasses.

Let's see the hierarchy of classes that we are going to map.

The table structure for each table will be as follows:
CREATE TABLE EMPLOYEE (
ID int IDENTITY,
NAME VARCHAR(50)

)


CREATE TABLE REGULAR_EMPLOYEE (
ID int ,
SALARY decimal(10,2),
BONUS decimal(10,2)
)

CREATE TABLE CONTRACT_EMPLOYEE (
ID int ,
PAY_PER_HOUR decimal(10,2),
CONTRACT_DURATION VARCHAR(50)
)

Example of Table per subclass class using Annotation

In this example we are creating the three classes and provide mapping of these classes in the hibernate.cfg.xml file.

Create the Persistent classes

You need to create the persistent classes representing the inheritance. Let's create the three classes for the above hierarchy:
File: Employee.java
package com.tutorialsdesk.inheritance;

import javax.persistence.*;

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

public class Employee{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)

@Column(name = "id")
private int id;

@Column(name = "name")
private String name;

public Employee() {

}

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;
}


}
File: Regular_Employee.java
package com.tutorialsdesk.inheritance;

import javax.persistence.*;

@Entity
@Table(name="REGULAR_EMPLOYEE")
@PrimaryKeyJoinColumn(name="ID")
public class Regular_Employee extends Employee{

@Column(name="salary")
private float salary;

@Column(name="bonus")
private int bonus;

public float getSalary() {
return salary;
}

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

public int getBonus() {
return bonus;
}

public void setBonus(int bonus) {
this.bonus = bonus;
}


}

File: Contract_Employee.java
package com.tutorialsdesk.inheritance;

import javax.persistence.AttributeOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="CONTRACT_EMPLOYEE")
@PrimaryKeyJoinColumn(name="ID")
public class Contract_Employee extends Employee{

@Column(name="pay_per_hour")
private float pay_per_hour;

@Column(name="contract_duration")
private String contract_duration;

public float getPay_per_hour() {
return pay_per_hour;
}

public void setPay_per_hour(float pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}

public String getContract_duration() {
return contract_duration;
}

public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
}


}

create configuration file

Open the hibernate.cgf.xml file, and add an entry of mapping resource like this:
<?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="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.password">PASSWORD</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433/DBNAME</property>
<property name="hibernate.connection.username">USERNAME</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.pool_size">1</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>

<mapping class="com.tutorialsdesk.inheritance.Employee"/>
<mapping class="com.tutorialsdesk.inheritance.Contract_Employee"/>
<mapping class="com.tutorialsdesk.inheritance.Regular_Employee"/>
</session-factory>
</hibernate-configuration>



Create the class that stores the persistent object

In this class, we are simply storing the employee objects in the database.
File: StoreData.java
package com.tutorialsdesk.inheritance;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class StoreData {
public static void main(String[] args) {
AnnotationConfiguration cfg=new AnnotationConfiguration();
Session session=cfg.configure("/conf/hibernate.cfg.xml").buildSessionFactory().openSession();

Transaction t=session.beginTransaction();

Employee e1=new Employee();
e1.setName("sonoo");

Regular_Employee e2=new Regular_Employee();
e2.setName("Vivek Kumar");
e2.setSalary(50000);
e2.setBonus(5);

Contract_Employee e3=new Contract_Employee();
e3.setName("Arjun Kumar");
e3.setPay_per_hour(1000);
e3.setContract_duration("15 hours");

session.persist(e1);
session.persist(e2);
session.persist(e3);

t.commit();
session.close();
System.out.println("success");
}
}

Hibernate Table Per Subclass using Annotation Tutorial with examples

Source:http://www.tutorialsdesk.com/2015/01/hibernate-table-per-subclass-using.html

Tidak ada komentar:

Posting Komentar