Home > Java > Hibernate Inheritance [ Part 3 ]

Hibernate Inheritance [ Part 3 ]

Inheritance dengan strategi Table per Concrete Class

1. Table

Table song

Table Song

Table Song

Table local_song

Table Local_Song

Table Local_Song

Table dangdut_song

Table Dangdut_Song

Table Dangdut_Song

Table pop_song

Table Pop_Song

2. Entity Class

Song class

package pojo;
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="song")
@Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
public class Song {

@Id
@Column(name="song_id")
private Long songId;

@Column(name="example_singer")
private String exampleSinger;
@Column(name="example_song")
private String exampleSong;

public Song(){

}

public Song(String eSinger, String eSong){
exampleSinger = eSinger;
exampleSong = eSong;
}

public Long getSongId() {
return songId;
}

public void setSongId(Long songId) {
this.songId = songId;
}

public String getExampleSinger() {
return exampleSinger;
}

public void setExampleSinger(String exampleSinger) {
this.exampleSinger = exampleSinger;
}

public String getExampleSong() {
return exampleSong;
}

public void setExampleSong(String exampleSong) {
this.exampleSong = exampleSong;
}
}

LocalSong class

package pojo;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="local_song")
@AttributeOverrides({@AttributeOverride(name="exampleSinger", column=@Column(name="example_singer")),@AttributeOverride(name="exampleSong", column=@Column(name="example_song"))})
public class LocalSong extends Song{
private String origin;

public LocalSong(){
}

public LocalSong(String eSinger, String eSong, String origin){
super(eSinger, eSong);
this.origin = origin;
}

public String getOrigin() {
return origin;
}

public void setOrigin(String origin) {
this.origin = origin;
}

}

DandutSong class

package pojo;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="dangdut_song")
@AttributeOverrides({@AttributeOverride(name="exampleSinger", column=@Column(name="example_singer")),@AttributeOverride(name="exampleSong", column=@Column(name="example_song"))})
public class DangdutSong extends Song {
private String creator;

public DangdutSong(){

}

public DangdutSong(String eSinger, String eSong, String creator){
super(eSinger, eSong);
this.creator = creator;
}

public String getCreator() {
return creator;
}

public void setCreator(String creator) {
this.creator = creator;
}

}

PopSong class

package pojo;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="pop_song")
@AttributeOverrides({@AttributeOverride(name="exampleSinger", column=@Column(name="example_singer")),@AttributeOverride(name="exampleSong", column=@Column(name="example_song"))})
public class PopSong extends Song {

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

public PopSong(){

}

public PopSong(String eSinger, String eSong, String label){
super(eSinger, eSong);
this.label = label;
}

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}
}

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 class="pojo.Song"> </mapping>
<mapping class="pojo.DangdutSong"> </mapping>
<mapping class="pojo.LocalSong"> </mapping>
<mapping class="pojo.PopSong"> </mapping>
</session-factory>
</hibernate-configuration>

4. Hibernate Utility Class

package inheritancec;

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

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

public class InheritanceC {

public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();

Song song = new Song("Sheila on 7", "Sephia");
song.setSongId(1L);
session.save(song);

PopSong popSong = new PopSong("Dcinnamons", "Super Girl", "Sony Studio");
popSong.setSongId(2L);
session.save(popSong);

LocalSong localSong = new LocalSong("Gesang", "Bengawan Solo","Jawa Tengah");
localSong.setSongId(3L);
session.save(localSong);

DangdutSong dangdutSong = new DangdutSong("Ayu Ting Ting", "Alamat Palsu", "Rhoma Irama");
dangdutSong.setSongId(4L);
session.save(dangdutSong);

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: