0% found this document useful (0 votes)
25 views

Ket: JAR (Java Archive) Sebenarnya Adalah Kumpulan File .Class Yang Dibundel Dan Dikompres Seperti Halnya File ZIP

The document provides instructions for setting up Hibernate programming in Java. It includes: 1. Downloading required Hibernate libraries and copying them to a lib directory. 2. Creating a Hibernate configuration file that defines the database connection properties and mappings between classes and tables. 3. Creating a Person class and mapping file to define the mapping between the Person class and person database table. The code example then shows how to use the Hibernate configuration to open a session, retrieve an object by id, and display its properties, connecting the Java class to data in the MySQL database table.

Uploaded by

exztline
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Ket: JAR (Java Archive) Sebenarnya Adalah Kumpulan File .Class Yang Dibundel Dan Dikompres Seperti Halnya File ZIP

The document provides instructions for setting up Hibernate programming in Java. It includes: 1. Downloading required Hibernate libraries and copying them to a lib directory. 2. Creating a Hibernate configuration file that defines the database connection properties and mappings between classes and tables. 3. Creating a Person class and mapping file to define the mapping between the Person class and person database table. The code example then shows how to use the Hibernate configuration to open a session, retrieve an object by id, and display its properties, connecting the Java class to data in the MySQL database table.

Uploaded by

exztline
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

HIBERNATE PROGRAMMING

1. Download terlebih dahulu liblary yang di butuhkan oleh hibernate sebagai berikut :

commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate-3.0.3.jar
javassist-3.12.0.GA.jar
postgresql-9.0-801.jdbc4.jar
ehcache-1.2.jar
slf4j-api-1.6.1.jar
slf4j-simple-1.6.1.jar
commons-logging-1.1.1.jar
jta-1.1.jar
cglib-2.2.jar;
asm-all-3.0.jar

Setelah Download copy file .jar kedalam direktori lib : c:\WORKDIR\HelloWorldApp\lib
direktori.
Ket : JAR (Java Archive) sebenarnya adalah kumpulan file .class yang dibundel dan
dikompres seperti halnya file ZIP.
2. Buatlah konfigurasi hibernate di bawah direktori config :
c:\WORKDIR\HelloWorldApp>mkdir config\hibernate
c:\WORKDIR\HelloWorldApp>notepad config\hibernate\hibernate.cfg.xml
Tuliskan code configurasi hibernate dan Kita akan memakai database mysql :

<hibernate-configuration>
//hibernate-configuration berguna untuk mengkoneksikan class dengan
database
<session-factory>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/perso
n</property>
//jdbc:mysql://localhost:3306/person=jdbc:nama
database://url/nama table pada database
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</p
roperty>
//hibernate akan membaca driver pada mysql
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">ibnu</property>
//pengisian username dan password pada database
<property
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop
erty>
//class dialect pada mysql
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_outer_join">true</property>
//peroperty tambahan
<mapping resource="model/person.hbm.xml"/>
</session-factory>
</hibernate-configuration>

3. Buat Direktori model di bawah direktori config
c:\WORKDIR\HelloWorldApp>mkdir config\model

4. Buat lah file person.hbm.xml mapping dengan notepad
c:\WORKDIR\HelloWorldApp>notepad config\model\person.hbm.xml

<hibernate-mapping>
//Hibernate mapping in berguna untuk memberitahukan kepada
hibernate, class name = lokasi class dan nama class, table = nama
table yang ada pada database.
<class name="com.HelloWorldApp.model.Person"
table="person">
<id name="PersonId" column="personid">
//Id name = Nama primary key
<generator class="identity" />
//generator class=identity berguna untuk code
otomatis pada primary key
</id>
<property name="PersonName" column="personname"/>
<property name="Age" column="age"/>
//pada hibernate property adalah atribut, name = atribut
pada class yang kita buat
</class>
</hibernate-mapping>












Menggunakan Hibernate configurasi Model Class Database Table

1. Modifikasi file HelloWorld.java yang kemarin kita buat untuk memakai hibernate
configurasi

package com.HelloWorldApp;

import com.HelloWorldApp.model.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
//import sessionFactory di ambil dari file .jar yang terdapat
pada direktori liblary

public class HelloWorld {

public static void main(String[] args) {

System.out.println("Starting Hibernate Configuration and
Creating Session...");
SessionFactory sf = new
Configuration().configure("/hibernate/hibernate.cfg.xml").b
uildSessionFactory ();
//mambuat session

System.out.println("Opening Session...");
Session sess = sf.openSession();
//opening session

System.out.println("Getting the persistence object...");
Person p = (Person) sess.get(Person.class, 1);
//mengambil presistence objek pada file person.class yang
telah di compile,
parameter 1 berarti mengambil 1 baris data

int result_age = p.getAge();
String result_name = p.getPersonName();
//membuat variable

System.out.println("Hello "+result_name+"
"+Integer.toString(result_age));
//karna result_age adalah integer maka harus di convert
kedalam tipe data string
}
}

2. Kemudian compile file yang telah termodif

c:\WORKDIR\HelloWorldApp>javac -classpath config;class;lib/hibernate-3.0.3.jar d
class source/com/HelloWorldApp/model/Person.java
source/com/HelloWorldApp/HelloWorld.java

3. Jalankan

c:\WORKDIR\HelloWorldApp>java -classpath config; class;lib/commons-collections-
3.1.jar;lib/dom4j-1.6.1.jar;lib/hibernate-jpa-2.0-api-1.0.0.Final.jar;lib/hibernate-
3.0.3.jar;lib/javassist-3.12.0.GA.jar;lib/postgresql-9.0-801.jdbc4.jar;lib/ehcache1.2.
jar;lib/slf4j-api-1.6.1.jar;lib/slf4j-simple-1.6.1.jar;lib/commons-logging-1.1.1.jar;lib/jta-
1.1.jar;lib/cglib-2.2.jar;lib/asm-all-3.0.jar com.HelloWorldApp.HelloWorld

You might also like