Hai, di materi kali ini saya mau membahas tentang Join Entity menggunakan ORM Hiberate, Untuk join pada dasarnya ada 2 jenis yaitu

  1. Implicit
  2. Explicit

Preparation

Sebelum mencoba kita buat dulu satu record lagi di table mapping.mahasiswa_onetoone seperti berikut:

insert-one-record

Implicit JOIN

Implicit join pada dasarnya query yang sudah di deklarasikan di Model/Entity kita tidak perlu menggunakan query join lagi dalam HQL contoh penggunaanya seperti berikut:

Implementasi JOIN Implicit DAO:

Implementasi Unit Testing JOIN Implicit:

Setelah itu coba jalankan unit testing method testJoinImplicit, maka hasilnya seperti berikut:

Jan 21, 2021 5:06:03 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
        mahasiswao0_.id as id1_31_,
        mahasiswao0_.alamat as alamat6_31_,
        mahasiswao0_.nama as nama2_31_,
        mahasiswao0_.nim as nim3_31_,
        mahasiswao0_.tahun_masuk as tahun_ma4_31_,
        mahasiswao0_.tanggal_lahir as tanggal_5_31_ 
    from
        mapping.mahasiswa_onetoone mahasiswao0_ cross 
    join
        mapping.alamat alamatenti1_ 
    where
        mahasiswao0_.alamat=alamatenti1_.id 
        and alamatenti1_.provinsi=?
Hibernate: 
    select
        alamatenti0_.id as id1_24_0_,
        alamatenti0_.kecamatan as kecamata2_24_0_,
        alamatenti0_.kelurahan as keluraha3_24_0_,
        alamatenti0_.kode_pos as kode_pos4_24_0_,
        alamatenti0_.kota as kota5_24_0_,
        alamatenti0_.nama_jalan as nama_jal6_24_0_,
        alamatenti0_.provinsi as provinsi7_24_0_,
        alamatenti0_.rt as rt8_24_0_,
        alamatenti0_.rw as rw9_24_0_ 
    from
        mapping.alamat alamatenti0_ 
    where
        alamatenti0_.id=?
[main] INFO com.maryanto.dimas.bootcamp.test.query.hql.TestHQLJoinImplicit - data: [MahasiswaOneToOneEntity(id=58c440a0-ccb1-4bba-8ff4-b55295f8dd86, nim=10511148, nama=Dimas Maryanto, tanggalLahir=1993-03-01, tahunMasuk=2011, alamat=AlamatEntity(id=7521f263-8d5e-4a4f-81e7-4926ab321a2a, provinsi=Jawa Barat, kota=Kab. Bandung, kelurahan=Cileunyi, kecamatan=Cinunuk, rw=18, rt=6, kodePos=40526, namaJalan=Jl Bukit indah))]
[main] INFO com.maryanto.dimas.bootcamp.test.query.hql.TestHQLJoinImplicit - destroy hibernate session!

Explicit JOIN

Untuk Explicit kebalikannya dari Implicit, kita bisa menggunakan inner join, left outer join, dan right outer join. dan juga dengan Hibernate ada 2 implementasi yaitu menggunakan keywork

  1. With (join with column1 = column2) specification by HQL
  2. ON (join on column1 = column2) specification by JPQL

Berikut adalah implementasi DAO dengan menggunakan Explicit Join:

Implementasi Unit Testing untuk Explicit Join:

Sekarang coba jalankan method testJoinExplicitWithKeywordOn, maka hasilnya seperti berikut:

Jan 21, 2021 5:11:00 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
        mahasiswao0_.id as id1_31_,
        mahasiswao0_.alamat as alamat6_31_,
        mahasiswao0_.nama as nama2_31_,
        mahasiswao0_.nim as nim3_31_,
        mahasiswao0_.tahun_masuk as tahun_ma4_31_,
        mahasiswao0_.tanggal_lahir as tanggal_5_31_ 
    from
        mapping.mahasiswa_onetoone mahasiswao0_ 
    inner join
        mapping.alamat alamatenti1_ 
            on (
                mahasiswao0_.alamat=alamatenti1_.id
            ) 
    where
        alamatenti1_.provinsi=?
Hibernate: 
    select
        alamatenti0_.id as id1_24_0_,
        alamatenti0_.kecamatan as kecamata2_24_0_,
        alamatenti0_.kelurahan as keluraha3_24_0_,
        alamatenti0_.kode_pos as kode_pos4_24_0_,
        alamatenti0_.kota as kota5_24_0_,
        alamatenti0_.nama_jalan as nama_jal6_24_0_,
        alamatenti0_.provinsi as provinsi7_24_0_,
        alamatenti0_.rt as rt8_24_0_,
        alamatenti0_.rw as rw9_24_0_ 
    from
        mapping.alamat alamatenti0_ 
    where
        alamatenti0_.id=?
[main] INFO com.maryanto.dimas.bootcamp.test.query.hql.TestHQLJoinExplicit - data: [MahasiswaOneToOneEntity(id=58c440a0-ccb1-4bba-8ff4-b55295f8dd86, nim=10511148, nama=Dimas Maryanto, tanggalLahir=1993-03-01, tahunMasuk=2011, alamat=AlamatEntity(id=7521f263-8d5e-4a4f-81e7-4926ab321a2a, provinsi=Jawa Barat, kota=Kab. Bandung, kelurahan=Cileunyi, kecamatan=Cinunuk, rw=18, rt=6, kodePos=40526, namaJalan=Jl Bukit indah))]
[main] INFO com.maryanto.dimas.bootcamp.test.query.hql.TestHQLJoinExplicit - destroy hibernate session!

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

Jan 21, 2021 5:11:42 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
        mahasiswao0_.id as id1_31_,
        mahasiswao0_.alamat as alamat6_31_,
        mahasiswao0_.nama as nama2_31_,
        mahasiswao0_.nim as nim3_31_,
        mahasiswao0_.tahun_masuk as tahun_ma4_31_,
        mahasiswao0_.tanggal_lahir as tanggal_5_31_ 
    from
        mapping.mahasiswa_onetoone mahasiswao0_ 
    inner join
        mapping.alamat alamatenti1_ 
            on (
                mahasiswao0_.alamat=alamatenti1_.id
            ) 
    where
        alamatenti1_.provinsi=?
Hibernate: 
    select
        alamatenti0_.id as id1_24_0_,
        alamatenti0_.kecamatan as kecamata2_24_0_,
        alamatenti0_.kelurahan as keluraha3_24_0_,
        alamatenti0_.kode_pos as kode_pos4_24_0_,
        alamatenti0_.kota as kota5_24_0_,
        alamatenti0_.nama_jalan as nama_jal6_24_0_,
        alamatenti0_.provinsi as provinsi7_24_0_,
        alamatenti0_.rt as rt8_24_0_,
        alamatenti0_.rw as rw9_24_0_ 
    from
        mapping.alamat alamatenti0_ 
    where
        alamatenti0_.id=?
[main] INFO com.maryanto.dimas.bootcamp.test.query.hql.TestHQLJoinExplicit - data: [MahasiswaOneToOneEntity(id=58c440a0-ccb1-4bba-8ff4-b55295f8dd86, nim=10511148, nama=Dimas Maryanto, tanggalLahir=1993-03-01, tahunMasuk=2011, alamat=AlamatEntity(id=7521f263-8d5e-4a4f-81e7-4926ab321a2a, provinsi=Jawa Barat, kota=Kab. Bandung, kelurahan=Cileunyi, kecamatan=Cinunuk, rw=18, rt=6, kodePos=40526, namaJalan=Jl Bukit indah))]
[main] INFO com.maryanto.dimas.bootcamp.test.query.hql.TestHQLJoinExplicit - destroy hibernate session!