Persiapan
- Berdoa sebelum melakukan aktifitas
- Download dan Install Java Editor Eclipse
- Download program Hibernate
- Download dan Install Oracle Database
- Kopi dan Snack untuk menemani Anda coding
1. Membuat Kelas POJO
Buatlah kelas POJO (Plant Old Java Object) dengan nama Pegawai.java seperti di bawah ini :
package com.nursalim.hibernate; public class Pegawai { private String kodePegawai; private String namaPegawai; private int umur; private String alamat; private String gender; private String departemen; public Pegawai(String kodePegawai, String namaPegawai, int umur, String alamat, String gender, String departemen){ this.kodePegawai = kodePegawai; this.namaPegawai = namaPegawai; this.umur = umur; this.alamat = alamat; this.gender = gender; this.departemen = departemen; } public String getKodePegawai() { return kodePegawai; } public void setKodePegawai(String kodePegawai) { this.kodePegawai = kodePegawai; } public String getNamaPegawai() { return namaPegawai; } public void setNamaPegawai(String namaPegawai) { this.namaPegawai = namaPegawai; } public int getUmur() { return umur; } public void setUmur(int umur) { this.umur = umur; } public String getAlamat() { return alamat; } public void setAlamat(String alamat) { this.alamat = alamat; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getDepartemen() { return departemen; } public void setDepartemen(String departemen) { this.departemen = departemen; } }
2. Membuat Tabel Di Database
Buatlah tabel di database Oracle dengan nama "pegawai" dengan menjalankan script di bawah ini:
CREATE TABLE PEGAWAI ( ID_PEGAWAI VARCHAR2(10 BYTE) NOT NULL, NAMA_PEGAWAI VARCHAR2(30 BYTE) NOT NULL, UMUR NUMBER(3) NOT NULL, ALAMAT VARCHAR2(30 BYTE) NOT NULL, GENDER CHAR(1 BYTE), DEPARTEMEN VARCHAR2(5 BYTE) )
3. Membuat File Mapping pegawai.hbm.xml
Buatlah file mapping dengan nama pegawai.hbm.xml untuk mapping antara kelas POJO dengan tabel "pegawai".
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate‐mapping PUBLIC "‐//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate‐mapping> <class name="com.nursalim.hibernate.Pegawai" table="PEGAWAI"> <property name="kodePegawai" column="id_pegawai" type="string" /> <property name="namaPegawai" column="nama_pegawai" type="string" /> <property name="umur" column="umur" type="int" /> <property name="alamat" column="alamat" type="string" /> <property name="gender" column="gender" type="string" /> <property name="departemen" column="departemen" type="string" /> </class> </hibernate‐mapping>
4. Membuat File Konfigurasi Hibernate
Buatlah file konfigurasi hibernat (hibernate.cfg.xml) untuk menyimpan konfigurasi hibernate seperti di bawah ini:<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="hibernate.connection.username">nano</property> <property name="hibernate.connection.password">nano</property> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="show_sql">true</property> <mapping resource="com/nursalim/hibernate/Pegawai.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
5. Membuat Program Java Untuk Aplikasi CRUD
Buatlah program Java dengan nama PegawaiCRUD.java, yang digunakan untuk membuat program aplikasi CRUD (Create Read Update Delete)package com.nursalim.hibernate; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.itextpdf.text.pdf.PdfStructTreeController.returnType; public class PegawaiCRUD { private static SessionFactory sessionFactory; public static void main(String[] args){ Pegawai pegawai = new Pegawai(); try{ sessionFactory = new Configuration().configure().buildSessionFactory(); }catch(ExceptionInInitializerError ex){ ex.printStackTrace(); } // add new pegawai PegawaiCRUD pegawaiCRUD = new PegawaiCRUD(); pegawaiCRUD.tambahPegawai("011","Danang", 30, "Kali Pasung", "L", "IT"); //get pegawai with id 011 pegawai = pegawaiCRUD.getPegawai("011"); //update with id 011 pegawaiCRUD.updatePegawai(pegawai, "Danang Kosing", 35, "Kali Mukti", "L", "FIN"); //delete pegawai with id 011 pegawaiCRUD.deletePegawai(pegawai); } public void tambahPegawai(String kodePegawai, String namaPegawai, int umur, String alamat, String gender, String departemen){ Session session = sessionFactory.openSession(); Transaction trx = null; try{ trx = session.beginTransaction(); Pegawai pegawai = new Pegawai(kodePegawai, namaPegawai, umur, alamat, gender, departemen); session.save(pegawai); trx.commit(); System.out.println("Record has been succesfully inserted"); }catch(HibernateException he){ if(trx != null){ trx.rollback(); } he.printStackTrace(); }finally{ session.close(); } } public Pegawai getPegawai(String kodePegawai){ Session session = sessionFactory.openSession(); Transaction trx = null; Pegawai pegawai = new Pegawai(); try{ trx = session.beginTransaction(); pegawai = (Pegawai) session.get(Pegawai.class, kodePegawai); System.out.println("ID pegawai == "+pegawai.getKodePegawai()); System.out.println("Nama pegawai == "+pegawai.getNamaPegawai()); System.out.println("Umur pegawai == "+pegawai.getUmur()); System.out.println("Alamat pegawai == "+pegawai.getAlamat()); System.out.println("Gender pegawai == "+pegawai.getGender()); System.out.println("Departemen pegawai == "+pegawai.getDepartemen()); }catch(HibernateException he){ if(trx != null){ trx.rollback(); } }finally{ session.close(); } return pegawai; } public void updatePegawai(Pegawai pegawai, String namaPegawai, int umur, String alamat, String gender, String departemen){ Session session = sessionFactory.openSession(); Transaction trx = null; try{ trx = session.beginTransaction(); pegawai.setNamaPegawai(namaPegawai); pegawai.setUmur(umur); pegawai.setAlamat(alamat); pegawai.setGender(gender); pegawai.setDepartemen(departemen); session.update(pegawai); trx.commit(); System.out.println("Record has been succesfully updated"); }catch(HibernateException he){ if(trx != null){ trx.rollback(); } }finally{ session.close(); } } public void deletePegawai(Pegawai pegawai){ Session session = sessionFactory.openSession(); Transaction trx = null; try{ trx = session.beginTransaction(); session.delete(pegawai); trx.commit(); System.out.println("Record has been succesfully deleted"); }catch(HibernateException he){ if(trx != null){ trx.rollback(); } }finally{ session.close(); } } }
8. Review Project Structure
9. Running
Coba Anda compile dan jalankan kelas PegawaiCRUD.java diatas, kalau tidak ada error maka akan keluar output seperti di bawah ini:
Hibernate: insert into PEGAWAI (nama_pegawai, umur, alamat, gender, departemen, ID_PEGAWAI) values (?, ?, ?, ?, ?, ?) Record has been succesfully inserted Hibernate: insert into PEGAWAI (nama_pegawai, umur, alamat, gender, departemen, ID_PEGAWAI) values (?, ?, ?, ?, ?, ?) Record has been succesfully inserted Hibernate: insert into PEGAWAI (nama_pegawai, umur, alamat, gender, departemen, ID_PEGAWAI) values (?, ?, ?, ?, ?, ?) Record has been succesfully inserted Hibernate: select pegawai0_.ID_PEGAWAI as ID_PEGAWAI1_0_0_, pegawai0_.nama_pegawai as nama_pegawai2_0_0_, pegawai0_.umur as umur3_0_0_, pegawai0_.alamat as alamat4_0_0_, pegawai0_.gender as gender5_0_0_, pegawai0_.departemen as departemen6_0_0_ from PEGAWAI pegawai0_ where pegawai0_.ID_PEGAWAI=? ID pegawai == 011 Nama pegawai == Danang Umur pegawai == 30 Alamat pegawai == Kali Pasung Gender pegawai == L Departemen pegawai == IT Hibernate: update PEGAWAI set nama_pegawai=?, umur=?, alamat=?, gender=?, departemen=? where ID_PEGAWAI=? Record has been succesfully updated Hibernate: select pegawai_.ID_PEGAWAI, pegawai_.nama_pegawai as nama_pegawai2_0_, pegawai_.umur as umur3_0_, pegawai_.alamat as alamat4_0_, pegawai_.gender as gender5_0_, pegawai_.departemen as departemen6_0_ from PEGAWAI pegawai_ where pegawai_.ID_PEGAWAI=? Hibernate: delete from PEGAWAI where ID_PEGAWAI=? Record has been succesfully deleted
~~~## Hibernate Tutorial ##~~~
Sekian tutorial singkat tentang Contoh Sederhana Aplikasi CRUD Menggunakan Hibernate. Semoga bermanfaat & Happy Learning Hibernate.
infonya sangat bermanfaat bagi ane
ReplyDeletepenjepit pcb