Hello Guys,

Now i wanna write article about Hibernate.

What is Hibernate????

Ok i will explain about hibernate, hibernate is a framework for object relational mapping that placed at layer model in MVC concept.

Hibernate is layer which connects between application and database,beside of that hibernate make available connection to database with connecting class with table.

Table mapping can be done use xml or annotation. Ok Now i will be make a simple Hibernate Application with xml as table mapping..
Ok, You must download Hibernate Library

Yup.. you already have hibernate Library in your IDE, hmmm i use Netbeans 6.5 as my Favourite IDE.
# Make New Project with netbeans .. sorry i can’t upload the picture (my internet connection is low) but i trust you can follow this step by step …
# After that we must make hibernate configuration file, in netbeans we just choose New–>Other–>Categories–>Hibernate–>Hibernate Configuration File. And then you can set your dbms that you use. (in my example i use MySQL) . this is my hibernate configuration file :





org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/testHibernate
root
vertrigo
true
create




Now we have done make hibernate configuration file, and then we will create PODJO class which will be mapping to table

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.nuxprog.model;

/**
*
* @author nuxprog
*/
public class Mahasiswa {
private long id;
private String nim;
private String nama;
private String alamat;
private String telepon;

public String getAlamat() {
return alamat;
}

public void setAlamat(String alamat) {
this.alamat = alamat;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getNama() {
return nama;
}

public void setNama(String nama) {
this.nama = nama;
}

public String getNim() {
return nim;
}

public void setNim(String nim) {
this.nim = nim;
}

public String getTelepon() {
return telepon;
}

public void setTelepon(String telepon) {
this.telepon = telepon;
}

}



Plain Old Java Object has been created, now we must create mapping file which used to mapping PODJO class to table (Mahasiswa.hbm.xml)
















after created this file, at hibernate configuration file will be automatically add this file as mapping resource.

Ok now we create file HibernateUtil, in netbeans you only choose New–>Other–>Categories–>Hibernate–>HibernateUtil.java
Class HibernateUtil.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.nuxprog.model;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
* Hibernate Utility class with a convenient method to get Session Factory object.
*
* @author nuxprog
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}



Now we must test with make main function , in main function we can make CRUD operation from class that we make before. this is my example main function :

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.nuxprog.main;

import org.hibernate.Session;
import org.nuxprog.model.HibernateUtil;
import org.nuxprog.model.Mahasiswa;

/**
*
* @author nuxprog
*/
public class Main {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
try{
Mahasiswa mhs = new Mahasiswa();
mhs.setNama("Wahyu Sumartha");
mhs.setAlamat("Yogya");
mhs.setNim("06018124");
mhs.setTelepon("0817359298");
session.save(mhs);
session.getTransaction().commit();
}catch(Exception e){
session.getTransaction().rollback();
e.printStackTrace();
}finally{
session.close();
}
}
}



Now You can check in your database… any record added??????? hehe.. it’s so simple guys, ok i feel sleepy now, i will continue with other operation like retrieve, update , and delete,,

Be patient…

hehehe

This entry was posted on 07:28 and is filed under . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

0 comments: