Home > Java > Hibernate Inheritance [ Part 2 ]

Hibernate Inheritance [ Part 2 ]

Inheritance dengan strategi Table Per Subclass

1. Table

Table animal

Table Animal

Table Animal

Table cow

Table Cow

Table Cow

Table fish

Table Fish

Table Fish

Table bird

Table Bird

Table Bird

2. Entity Class

Animal class

package pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table(name="animal")
@Inheritance(strategy= InheritanceType.JOINED)
public class Animal {
@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="habitat_animal")
private String habitatAnimal;
@Column(name="food_animal")
private String foodAnimal;

public int getId() {
return id;
}

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

public String getHabitatAnimal() {
return habitatAnimal;
}

public void setHabitatAnimal(String habitatAnimal) {
this.habitatAnimal = habitatAnimal;
}

public String getFoodAnimal() {
return foodAnimal;
}

public void setFoodAnimal(String foodAnimal) {
this.foodAnimal = foodAnimal;
}

}

Cow class

package pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="cow")
@PrimaryKeyJoinColumn(name="id_cow", referencedColumnName="id")
public class Cow extends Animal{

@Column(name="cow_origin")
private String cowOrigin;
@Column(name="cow_usage")
private String cowUsage;

public String getCowOrigin() {
return cowOrigin;
}

public void setCowOrigin(String cowOrigin) {
this.cowOrigin = cowOrigin;
}

public String getCowUsage() {
return cowUsage;
}

public void setCowUsage(String cowUsage) {
this.cowUsage = cowUsage;
}

}

Fish class

package pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="fish")
@PrimaryKeyJoinColumn(name="id_fish", referencedColumnName="id")
public class Fish extends Animal  {

@Column(name="fish_usage")
private String fishUsage;
@Column(name="fish_price")
private int fishPrice;

public String getFishUsage() {
return fishUsage;
}

public void setFishUsage(String fishUsage) {
this.fishUsage = fishUsage;
}

public int getFishPrice() {
return fishPrice;
}

public void setFishPrice(int fishPrice) {
this.fishPrice = fishPrice;
}
}

Bird class

package pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name="bird")
@PrimaryKeyJoinColumn(name="id_bird", referencedColumnName="id")
public class Bird extends Animal {

@Column(name="bird_price")
private int birdPrice;
@Column(name="bird_population")
private String birdPopulation;

public int getBirdPrice() {
return birdPrice;
}

public void setBirdPrice(int birdPrice) {
this.birdPrice = birdPrice;
}

public String getBirdPopulation() {
return birdPopulation;
}

public void setBirdPopulation(String birdPopulation) {
this.birdPopulation = birdPopulation;
}
}

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.*;

public class InheritanceB {

public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Animal animal = new Animal();
animal.setFoodAnimal("Anything");
animal.setHabitatAnimal("World");
session.save(animal);

Cow cow = new Cow();
cow.setFoodAnimal("Grass");
cow.setHabitatAnimal("Savana");
cow.setCowOrigin("USA");
cow.setCowUsage("Industry");
session.save(cow);

Fish fish = new Fish();
fish.setFoodAnimal("Plankton");
fish.setHabitatAnimal("Sea");
fish.setFishPrice(200);
fish.setFishUsage("Industry");
session.save(fish);

Bird bird = new Bird();
bird.setFoodAnimal("Ant");
bird.setHabitatAnimal("Forest");
bird.setBirdPopulation("Rare");
bird.setBirdPrice(1000);
session.save(bird);
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: