How to reduce costs of access to relational data



Programmers very often face a problem with access to data, which are stored in a relational database. Although it is in contradiction to an object Java programming that somehow forces object way of dealing with data ( with those stored in a database, as well) a lot of us still stick to relational system of storing data. We can write almost for sure that very few programmers experienced the simplicity of storing data in object databases and nothing indicates that the number of those who did should increase.
The equally amazing is the obstinacy of programmers who write own solutions without first evaluating those which already exist ( above all, free frameworks of data persisitence.
It is hard to believe when we take into consideration the time needed for execution of project, which is said to be usually insufficient.
Without shadow of a doubt we can say that Hibernate is the most popular and free software, which realizes approach of mapping objects into structures of data stored in relational databases (ORM = Object-to-Relational Mapping). The latest, finished 3.1.2 version introduces so many conveniences that it is hard to imagine that anyone would need to create an alternative solution. Provided documetation is sufficiently complete to base next project on Hibernate without any risk.

Let’s start studying Hibernate from a simple example. Imagine that we create an application – a users’ catalog. The main element of our application is a term of a user. We create a class some.test.User.

package some.test;

public final class User {
private int id;

private String name;

private String surname;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id; }

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;


public void setSurnameo(String surname) {
this.surname = surname;

}
}
Our user consists of an identifier, a name and a surname. Even for such a simple class, stored in a database, we have to create a structure of a database and then serve an operation of storing and reading data by means of JDBC. Even for a professional and advanced programmer it is a task for a few quarters. Moreover, if the programmer did it several times what’s the point in doing it one more time? What a bore! So this advanced programmer will look for some timesaving solutions and sooner or later they will come across Hibernate. Is there anything more pleasant than when most of a paid task is done by free software? Thanks to that our quarter can be spent on creative work instead of writing service of access to data again. In this way we can concentrate on actual writing a source code for our brand new application.


Having finished very creative work, during which we learnt arcana of using Hibernate at the time of its early evaluation, an examplary class using conveniences of Hibernate to store data about users - some.test.HibernateExample – looks as follows:

package some.test;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateExample {

private final static SessionFactory factory;
static {
// 1. Initiation of Hibernate
Configuration cfg = new Configuration().configure();

// 2. Formation of a Hibernate session factory
factory = cfg.buildSessionFactory();
}

public static void main(String[] args) {
HibernateExample m = new HibernateExample();
m.createUsers();
m.displayUsers();
}

public void createUsers() {
// 3. Opening of Hibernate session
Session session = factory.openSession();

// 4. Beginning of a transaction
Transaction tx = session.beginTransaction();

// 5. Formation of a user
User u = new User();
u.setName("John");
u.setSurname("Smith");

// 6. Saving the user in a database
session.save(u);

// 7. Confirmation of the transaction
tx.commit();

// 8. Closure of the Hibernate session
session.close();

// 9. Closure of the Hibernate session
session.close();

}
}
What does exactly Hibernate do? Hibernate is responsible for mapping (transfer) data in the form of Java objects to records in a database and vice versa. Work with Hibernate always starts with its configuration – calling an appropriate method - Configuration.configure(), which finds a configuration file and follows the instructions included in it ( more about the Hibernate’s configuration file in a moment). It is a line described as: "1. Initiation of Hibernate ". Next, Hibernate session factory is formed with Configuration.buildSessionFactory() method. Then, if no exception was put ( our exemplary class is indecently poor in service of extraordinary situations) Hibernate is thought to be fully configured. Both steps: calling Configuration.configure() methods and Configuration.buildSessionFactory() – we make once in whole our application and therefore they are both usually put in a static block of a certain utility class (e.g. HibernateUtils).

We should remember that usage of this approach is connected with certain consequences in multithreading environment like J2EE application server and it would mean using another approach e.g. putting a factory in JNDI tree to secure it a single creation. No matter what approach we have decided to use, we should bear in mind that both methods form objects like Configuration and SessionFactory, which are enough to appear singly in our application. The suggested solution ( formation of the object session in the static block) is entirely enough for our solution.


Next parts - see archived articles