Hai, pada materi kali ini saya mau membahas tentang 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