Friday 26 September 2008

Hibernate short summary part-I, page 3

Read the Article Page-I, Page-II
Client Application:
Let us write a client application to persist our USER object... it would involve the following steps ...
1) Load the hibernate configuration and initiate Hibernate session factory. Hibernate session factory is responsible for providing the sessions to do the database interaction stuff. You can assume for the time the session factory to be equivalent of the JDBC connection pool and each session equivalent of JDBc connection ....

2) Once the session factory is instantiated ...get an active session from it..

3) We would create a User object which we want to save in the database ...

4) initiate a hibernate transaction, save the object and commit... :) we are done ...
In summary ... you need to learn some basic APIs on hibernate session.

Let us write the program now ...

Client.java:
package client;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import com.entities.User;

public class Client {
public static void main(String[] args) {

try{

Configuration conf = new Configuration().configure();
//by default configure tries to search
//hibernate.cfg.xml step-I is done

SessionFactory sessionFactory = conf.buildSessionFactory();

//step_II
Session session =sessionFactory.openSession();

//step_III
User user = new User();
user.setName("Test User");
user.setAge(24);
//note that we did not set userId because thats auto incriment
Transaction t = session.beginTransaction();
session.save(user);
//save inserts the row in the database...
t.commit();

System.out.println("User inserted with Id : " + user.getUserId());
session.close();

}
catch(Exception e){
e.printStackTrace();
}
}
}

As I already said for the above program to compile and run properly, two set of Jar files have to placed properly in the application's class-path. One is the set of Jar files that are available within the Hibernate Ditribution, which is normally the <HIBERNATE_INSTALLATION>\lib, where <HIBERNATE_INSTALLATION> refers to the installation of the Hibernate.

Following jar files from the Hibernate distribution have to be maintained in the class-path of the application,

cglib2.jar
commons-collections
commons-Logging
dom4j.jar
ehcache.jar
jdbc2.0-stdext
jta.jar
log4j.jar
odmg.jar
xalan.jar
xerces.jar
xml-apis.jar

The other set of the jar files are for the Jdbc Driver.

The above code establishes a Configuration object by calling new Configuration().configure() which scans the classpath for the presence of hibernate.cfg.xml file. It then creates an instance of SessionFactory object with the details populated from the Configuration File by calling Configuration.buildSessionFactory(). A new Session object is then created for persisting objects by calling SessionFactory.openSession().

A new Java object called 'User' is populated with some test values and the object is persisted into the Session object within a transactional context by calling Session.beginTransaction() which marks the beginning of the transaction. Remember that Session.save(object) only marks the object to be persisted. After a successful save operation and transaction is committed by calling Transaction.commit(), which means that the object will be synchronized with the database.

0 comments: