0% found this document useful (0 votes)
38 views4 pages

Hibernate Configuration Guide

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)
38 views4 pages

Hibernate Configuration Guide

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

HIBERNATE PROGRAMMING

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

[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link];
[Link]

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\[Link]
Tuliskan code configurasi hibernate dan Kita akan memakai database mysql :

<hibernate-configuration>
//hibernate-configuration berguna untuk mengkoneksikan class dengan
database
<session-factory>
<property
name="[Link]">jdbc:mysql://localhost:3306/perso
n</property>
//jdbc:mysql://localhost:3306/person=jdbc:nama
database://url/nama table pada database
<property
name="[Link].driver_class">[Link]</p
roperty>
//hibernate akan membaca driver pada mysql
<property name="[Link]">root</property>
<property name="[Link]">ibnu</property>
//pengisian username dan password pada database
<property
name="[Link]">[Link]</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/[Link]"/>
</session-factory>
</hibernate-configuration>

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

4. Buat lah file [Link] mapping dengan notepad
c:\WORKDIR\HelloWorldApp>notepad config\model\[Link]

<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="[Link]"
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 [Link] yang kemarin kita buat untuk memakai hibernate
configurasi

package [Link];

import [Link].*;
import [Link];
import [Link];
import [Link];
//import sessionFactory di ambil dari file .jar yang terdapat
pada direktori liblary

public class HelloWorld {

public static void main(String[] args) {

[Link]("Starting Hibernate Configuration and
Creating Session...");
SessionFactory sf = new
Configuration().configure("/hibernate/[Link]").b
uildSessionFactory ();
//mambuat session

[Link]("Opening Session...");
Session sess = [Link]();
//opening session

[Link]("Getting the persistence object...");
Person p = (Person) [Link]([Link], 1);
//mengambil presistence objek pada file [Link] yang
telah di compile,
parameter 1 berarti mengambil 1 baris data

int result_age = [Link]();
String result_name = [Link]();
//membuat variable

[Link]("Hello "+result_name+"
"+[Link](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/[Link] d
class source/com/HelloWorldApp/model/[Link]
source/com/HelloWorldApp/[Link]

3. Jalankan

c:\WORKDIR\HelloWorldApp>java -classpath config; class;lib/commons-collections-
[Link];lib/[Link];lib/[Link];lib/hibernate-
[Link];lib/[Link];lib/[Link];lib/ehcache1.2.
jar;lib/[Link];lib/[Link];lib/[Link];lib/jta-
[Link];lib/[Link];lib/[Link] [Link]

You might also like