Insert, Update dan Delete Statement dengan HQL
Hai, pada materi kali ini saya mau membahas tentang Insert, Update, dan Delete Statement menggunakan HQL (Hibernate Query Language). Ok nah jadi kita masih pake entity yang sama yaitu
Dan berikut adalah implementasi DAO:
Implementasi Unit Testing:
Jika teman-temen jalankan unit testing pada method testDeleteById
, maka hasilnya seperti berikut:
Jan 21, 2021 12:46:52 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
mapping.matakuliah
(nama_kuliah, jumlah_sks, id)
values
(?, ?, ?)
Hibernate:
delete
from
mapping.mahasiswa_matakuliah_list
where
(
matakuliah_id
) in (
select
id
from
mapping.matakuliah
where
id=?
)
Hibernate:
delete
from
mapping.matakuliah
where
id=?
[main] INFO com.maryanto.dimas.bootcamp.test.query.hql.TestHQLMatakuliahCreateUpdateDelete - Data berhasil di hapus? true
Hibernate:
select
matakuliah0_.id as id1_32_,
matakuliah0_.nama_kuliah as nama_kul2_32_,
matakuliah0_.jumlah_sks as jumlah_s3_32_
from
mapping.matakuliah matakuliah0_
where
matakuliah0_.id=?
[main] ERROR com.maryanto.dimas.bootcamp.example.query.hql.dao.HQLMatakuliahCUDDao - data not found
Kemudian coba jalankan method testUpdateData
, maka berikut hasilnya:
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
[main] INFO com.maryanto.dimas.bootcamp.test.query.hql.TestHQLMatakuliahCreateUpdateDelete - sebelum di updated: MataKuliahEntity(id=9e5ab8c0-fade-4da3-bcf1-9620db5b0412, nama=Matakuliah lama, sks=2)
Hibernate:
insert
into
mapping.matakuliah
(nama_kuliah, jumlah_sks, id)
values
(?, ?, ?)
Hibernate:
update
mapping.matakuliah
set
nama_kuliah=?,
jumlah_sks=?
where
id=?
Hibernate:
update
mapping.matakuliah
set
nama_kuliah=?,
jumlah_sks=?
where
id=?
Hibernate:
select
matakuliah0_.id as id1_32_,
matakuliah0_.nama_kuliah as nama_kul2_32_,
matakuliah0_.jumlah_sks as jumlah_s3_32_
from
mapping.matakuliah matakuliah0_
where
matakuliah0_.id=?
[main] INFO com.maryanto.dimas.bootcamp.test.query.hql.TestHQLMatakuliahCreateUpdateDelete - setelah di updated: MataKuliahEntity(id=9e5ab8c0-fade-4da3-bcf1-9620db5b0412, nama=Matakuliah baru, sks=2)
Hibernate:
delete
from
mapping.mahasiswa_matakuliah_list
where
(
matakuliah_id
) in (
select
id
from
mapping.matakuliah
where
id=?
)
Hibernate:
delete
from
mapping.matakuliah
where
id=?
[main] INFO com.maryanto.dimas.bootcamp.test.query.hql.TestHQLMatakuliahCreateUpdateDelete - destroy hibernate session!
Summary
Dengan perintah HQL, kita bisa melakukan update dan delete layaknya seperti perintah native query (sql). Tetapi disini untuk Insert statement ada limitasi yaitu tidak bisa menggunakan values
tetapi hanya bisa menggunakan insert into EntityName1 (props) select * from EntityName2
coba lihat ref disini
Yuk simak juga videonya,
Dan jika temen-temen belajar hal baru kali ini jangan lupa buat Like, Subcribe, dan Share ke temen kalian. Terimakasih!!!