Object Relational Framework for Java 4 minute read

Hai, di materi kali ini saya mau membahas tentang element collection dengan ORM Hibernate. Hibernate menggunakan collection yang memiliki fitur lazy-loading, caching atau state chnage detection semantics. untuk menggunakan Element Collection kita hanya bisa menggunakan class

  1. java.util.Collection
  2. java.util.List
  3. java.util.Set
  4. java.util.Map
  5. java.util.SortedSet
  6. java.util.SortedMap

selain yang di sebukan di atas, maka biasanya gak bisa atau akan menyebabkan throwing exception.

Element Collection pada dasarnya teridiri dari:

  1. Value Type Collection
  2. Embedded Type Collection
  3. Entity Collection

Element Collection As Value

berikut adalah perancangan tabelnya:

erd

Berikut adalah implementasi Entity:

Implementasi DAO:

Implementasi Unit Testing:

Jika di running pada method testSaveMahasiswa maka hasilnya seperti berikut:

INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@374c3975] 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 embedded.mahasiswa_hoby (
       mahasiswa_id int8 not null,
        hobi varchar(255)
    )
Hibernate: 
    
    create table embedded.mahasiswa_listasvalue (
       id int8 not null,
        alamat varchar(255) not null,
        nama varchar(255) not null,
        nim varchar(255),
        primary key (id)
    )
Hibernate: 
    
    alter table if exists embedded.mahasiswa_hoby 
       add constraint fk_hobi_mahasiswa_id 
       foreign key (mahasiswa_id) 
       references embedded.mahasiswa_listasvalue
Jan 19, 2021 2:40:10 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: 
    select
        nextval ('hibernate_sequence')
Hibernate: 
    insert 
    into
        embedded.mahasiswa_listasvalue
        (alamat, nama, nim, id) 
    values
        (?, ?, ?, ?)
Hibernate: 
    insert 
    into
        embedded.mahasiswa_hoby
        (mahasiswa_id, hobi) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        embedded.mahasiswa_hoby
        (mahasiswa_id, hobi) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        embedded.mahasiswa_hoby
        (mahasiswa_id, hobi) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        embedded.mahasiswa_hoby
        (mahasiswa_id, hobi) 
    values
        (?, ?)
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.collection.TestElementCollectionAsValue - mahasiswa: MahasiswaElementCollectionAsValue(id=12, nim=10511148, name=Dimas Maryanto, address=Bandung)
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.collection.TestElementCollectionAsValue - destroy hibernate session!

Kemudian kita coba jalankan method testFindById maka hasilnya seperti berikut:

INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: 
    select
        mahasiswae0_.id as id1_2_0_,
        mahasiswae0_.alamat as alamat2_2_0_,
        mahasiswae0_.nama as nama3_2_0_,
        mahasiswae0_.nim as nim4_2_0_ 
    from
        embedded.mahasiswa_listasvalue mahasiswae0_ 
    where
        mahasiswae0_.id=?
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.collection.TestElementCollectionAsValue - mahasiswa: MahasiswaElementCollectionAsValue(id=12, nim=10511148, name=Dimas Maryanto, address=Bandung)
Hibernate: 
    select
        hobies0_.mahasiswa_id as mahasisw1_1_0_,
        hobies0_.hobi as hobi2_1_0_ 
    from
        embedded.mahasiswa_hoby hobies0_ 
    where
        hobies0_.mahasiswa_id=?
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.collection.TestElementCollectionAsValue - hobies: [Gaming, Programing, Movies, Sharing]
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.collection.TestElementCollectionAsValue - destroy hibernate session!

Sekarang, kita coba check tabelnya. maka hasilnya seperti berikut:

select-table

Element Collection As Type

Berikut adalah perancangan tabelnya:

erd

Untuk model embedded kita masih menggunakan embedded alamat seperti sebelumnya yaitu seperti berikut:

Berikut adalah implementasi Entity:

Implementasi DAO:

Implementasi Unit Testing:

Kemudian coba running unit testingnya pada method testSaveMahasiswa maka hasilnya akan seperti berikut:

INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@51424203] 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 collections.mahasiswa_address (
       mahasiswa_id int8 not null,
        kecamatan varchar(100),
        kelurahan varchar(100),
        kode_pos int4,
        kota varchar(50),
        nama_jalan varchar(100),
        provinsi varchar(50),
        rt int4,
        rw int4
    )
Hibernate: 
    
    create table collections.mahasiswa_listastype (
       id int8 not null,
        nama varchar(255) not null,
        nim varchar(255),
        primary key (id)
    )
Hibernate: 
    
    alter table if exists collections.mahasiswa_address 
       add constraint fk_address_mahasiswa_id 
       foreign key (mahasiswa_id) 
       references collections.mahasiswa_listastype
Jan 19, 2021 2:59:09 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: 
    select
        nextval ('hibernate_sequence')
Hibernate: 
    insert 
    into
        collections.mahasiswa_listastype
        (nama, nim, id) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        collections.mahasiswa_address
        (mahasiswa_id, kecamatan, kelurahan, kode_pos, kota, nama_jalan, provinsi, rt, rw) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: 
    insert 
    into
        collections.mahasiswa_address
        (mahasiswa_id, kecamatan, kelurahan, kode_pos, kota, nama_jalan, provinsi, rt, rw) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.collection.TestElementCollectionAsType - mahasiswa: MahasiswaElementCollectionAsType(id=14, nim=10511148, name=Dimas Maryanto)
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.collection.TestElementCollectionAsType - destroy hibernate session!

Dan yang terakhir, coba jalankan method findById maka hasilnya akan seperti berikut:

INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: 
    select
        mahasiswae0_.id as id1_2_0_,
        mahasiswae0_.nama as nama2_2_0_,
        mahasiswae0_.nim as nim3_2_0_ 
    from
        collections.mahasiswa_listastype mahasiswae0_ 
    where
        mahasiswae0_.id=?
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.collection.TestElementCollectionAsType - mahasiswa: MahasiswaElementCollectionAsType(id=14, nim=10511148, name=Dimas Maryanto)
Hibernate: 
    select
        addresses0_.mahasiswa_id as mahasisw1_0_0_,
        addresses0_.kecamatan as kecamata2_0_0_,
        addresses0_.kelurahan as keluraha3_0_0_,
        addresses0_.kode_pos as kode_pos4_0_0_,
        addresses0_.kota as kota5_0_0_,
        addresses0_.nama_jalan as nama_jal6_0_0_,
        addresses0_.provinsi as provinsi7_0_0_,
        addresses0_.rt as rt8_0_0_,
        addresses0_.rw as rw9_0_0_ 
    from
        collections.mahasiswa_address addresses0_ 
    where
        addresses0_.mahasiswa_id=?
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.collection.TestElementCollectionAsType - alamat: [AlamatEmbeddable(provinsi=Jawa Barat, kota=Kab. Bandung, kelurahan=Cileunyi, kecamatan=Cinunuk, rw=16, rt=7, kodePos=40526, namaJalan=Jl. Bukit Indah NO B8), AlamatEmbeddable(provinsi=DKI Jakarta, kota=Jakarta Selatan, kelurahan=Melawai, kecamatan=Cipete, rw=10, rt=1, kodePos=42105, namaJalan=Jl. Damai Buntu)]
[main] INFO com.maryanto.dimas.bootcamp.test.mapping.collection.TestElementCollectionAsType - destroy hibernate session!

Ok sekarang kita check table di databasenya. maka hasilnya seperti berikut:

select-tables