Hai, di materi kali ini saya mau ngebahas tentang Inherintance Mapping dengan Metode MappedSuperclass. Untuk mencoba mapping ini, kita membutuhkan 2 tabel yang memiliki column yang hampir sama contohnya mobil sama motor berikut adalah perancangan databasenya:

erd

Implementasi Entity superclass:

Implementasi Entity child mobil:

Implementasi Entity child motor:

Implementasi DAO untuk mobil:

Implementasi DAO untuk motor:

Kemudian kita buat unit testingnya seperti berikut:

Jika di running maka hasilnya seperti berikut:

INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@1e1e837d] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: 
    
    create table inherintance.mobil_mappedsuperclass (
       id varchar(64) not null,
        cc int4,
        jumlah_cylinder int4,
        jumlah_roda int4,
        nama varchar(50),
        nama_pabrikan varchar(50) not null,
        is_all_wheel_drive boolean not null,
        jumlah_kursi int4,
        primary key (id)
    )
Hibernate: 
    
    create table inherintance.motor_mappedsuperclass (
       id varchar(64) not null,
        cc int4,
        jumlah_cylinder int4,
        jumlah_roda int4,
        nama varchar(50),
        nama_pabrikan varchar(50) not null,
        jenis_rantai varchar(255),
        primary key (id)
    )
Jan 16, 2021 12:00:08 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: 
    insert 
    into
        inherintance.mobil_mappedsuperclass
        (cc, jumlah_cylinder, jumlah_roda, nama, nama_pabrikan, is_all_wheel_drive, jumlah_kursi, id) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?)
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.inherintance.TestMappedSuperClass - mobil: MobilMappedSuperclass(super=KendaraanMappedSuperclass(nama=Honda BRIO, jumlahRoda=4, jumlahCylinder=4, cc=1000, namaPabrikan=PT. Honda Motor Company), id=c5a7398c-a79c-4abd-9c55-deb3ec1e2bad, jumlahKursi=4, allWheelDrive=false)
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.inherintance.TestMappedSuperClass - destroy hibernate session!