Home > Java > Hibernate Inheritance [ Part 1 ]

Hibernate Inheritance [ Part 1 ]

Inheritance dengan strategi Table per class hierarchy

1. Table

Table human

Table Human

Table Human

2. Entity Class

Human class

package pojo;

import java.util.Date;
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.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.Temporal;

@Entity
@Table(name="human")
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator", discriminatorType= DiscriminatorType.STRING)
@DiscriminatorValue(value="Human")
public class Human {

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

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

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

@Temporal(javax.persistence.TemporalType.DATE)
@Column(name="dob")
private Date dob;

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public int getId() {
return id;
}

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

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Date getDob() {
return dob;
}

public void setDob(Date dob) {
this.dob = dob;
}

public String getWorkLocation() {
return workLocation;
}

public void setWorkLocation(String workLocation) {
this.workLocation = workLocation;
}

}

Employee class

package pojo;

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

@Entity
@Table(name="human")
@DiscriminatorValue(value="Employee")
public class Employee extends Human {

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

public String getActivityTarget() {
return activityTarget;
}

public void setActivityTarget(String activityTarget) {
this.activityTarget = activityTarget;
}
}

Farmer class

package pojo;

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

@Entity
@Table(name="human")
@DiscriminatorValue(value="Farmer")
public class Farmer extends Human {

@Column(name="sum_income")
private int incomeSum;

public int getIncomeSum() {
return incomeSum;
}

public void setIncomeSum(int incomeSum) {
this.incomeSum = incomeSum;
}
}

Seller class

package pojo;

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

@Entity
@Table(name="human")
@DiscriminatorValue(value="Seller")
public class Seller extends Human {

@Column(name="selling_target")
private int sellingTarget;

public int getSellingTarget() {
return sellingTarget;
}

public void setSellingTarget(int sellingTarget) {
this.sellingTarget = sellingTarget;
}
}

3. Hibernate Configuration File

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jointrain?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true</property>
<mapping></mapping>
<mapping></mapping>
<mapping></mapping>
<mapping></mapping>

</session-factory>
</hibernate-configuration>

4. Hibernate Utility Class

package inheritancea;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

5. Main Class

package inheritancea;

import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.Employee;
import pojo.Farmer;
import pojo.Human;
import pojo.Seller;

/**
*
* @author Kukuh Utama
* Strategy Table per class hierarchy
*/
public class InheritanceA {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();

Human human = new Human();
human.setFirstName("Kukuh");
human.setLastName("Utama");
human.setWorkLocation("Bumi Allah");
human.setDob(new Date());

Farmer farmer = new Farmer();
farmer.setFirstName("Toni");
farmer.setLastName("Stark");
farmer.setWorkLocation("Rice Field");
farmer.setIncomeSum(100);
farmer.setDob(new Date());

Seller seller = new Seller();
seller.setFirstName("Jon");
seller.setLastName("Campion");
seller.setWorkLocation("Matahari Store");
seller.setSellingTarget(1000);
seller.setDob(new Date());

Employee employee = new Employee();
employee.setFirstName("Jim");
employee.setLastName("Beglin");
employee.setWorkLocation("Grha XL");
employee.setActivityTarget("Meeting with client");
employee.setDob(new Date());

session.save(human);
session.save(farmer);
session.save(seller);
session.save(employee);

session.getTransaction().commit();
session.close();
}
}

6. Project Structure

Project Structure

Project Structure

7. Console Output

Console Output

Console Output

Semoga bermanfaat.

Referensi : dari berbagai sumber.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: