Thursday, March 18, 2010

Hibernate Tutorial

Introduction

In today's programming world all contemporary, large applications are written using object oriented programming. So, J2EE is real choice for developers, where they are able to develop the Object Oriented programs. Since J2EE is powered by Java programming so object orientation for applications and relation of databases are totally natural, which provides the faster way to write the object systems, they are also easier to design and divide tasks. Also, implementation of this is usually very suitable for any kind of modifications. Similarly, relational databases seem to be irreplaceable, because they are very effective and introducing another model of storing databases.

Now, Major Issue
Java language comes with a feature called JDBC (Java Database connectivity), which gives us an opportunity to download data from a database any number of times, transfer them to an application and save the data converted by the system to a database.

Now, just imagine one day suddenly we need to change the database from the current database which we are using, e.g., today if we are using PostgreSQL to MySQL in this situation some commands and constructions working with PostgreSQL might not work with MySQL, which leads us to convert the whole code. Here the use comes for Hibernate.

What is Hibernate
Hibernate is an object-relational mapping(ORM) libray for the Java language, providing a framework for mapping an object oriented domain model to a traditional database relational database. Hibernate solves object-relational impedance mismatch problems by replacing direct presistence-related database accesses with high-level object handling functions.

Hibernate's primary feature is mapping from Java classes to database tables (and from Java data types to SQL data types). Hibernate also provides data query and retrieval facilities.

Hibernate generates the SQL calls and relieves the developer from manual result set handling and object conversion, keeping the application portable to all supported SQL databases, with database portability delivered at very little performance overhead.

About Object Relational Mapping

ORM - Object Relation Mapping. Almost all contemporary databases are relational and based on SQL language. Most programs using them are created in an object way. So some kind of incoherence appears at the meeting point of a database and software. We need some appropriate tools to translate data from relational language into an object language and the other way round.
Service of JDBC is extremely troublesome while dealing with more complex applications.

Tasks of ORM
Hibernate is currently one of the most popular ORM solutions.Its popularity and effectiveness were proved by the fact that e.g. implementation of CMP in EJB3 on JBoss server is based on Hibernate. What is more, a lot of solutions were copied during designing EJB3 specification.
Other, common solutions ORM include: CMP, JDO. Hibernate, as opposed to them, is not a standard but a concrete solution.


Advantages of ORM
Lightness, it is no need to use special containers, it is enough to attach an appropriate library. One can also use it to both: web and client applications. There is no need to generate manually an additional code. As opposed to e.g. JDO, Hibernate requires only presence of configuration files. Additional classes used by it are generated during performing a task.

Features of Hibernate

Hibernate is a solution for object relational mapping and a persistence management solution or persistent layer. This is probably not understandable for anybody learning Hibernate.

What you can imagine is probably that you have your application with some functions (business logic) and you want to save data in a database. When you use Java all the business logic normally works with objects of different class types. Your database tables are not at all objects.

Hibernate provides a solution to map database tables to a class. It copies the database data to a class. In the other direction it supports to save objects to the database. In this process the object is transformed to one or more tables.

Saving data to a storage is called persistence. And the copying of tables to objects and vice versa is called object relational mapping.

Practical View Point

The most important Hibernate's feature is mapping from Java classes to database tables (and from Java data types to SQL data types), but also to provides data query ( and retrieval facilities ).

It is important that Hibernate generates the SQL calls and relieves keeping the application portable to all SQL databases, with database portability delivered at very little performance overhead.This feature can significialy reduce development time that programmer would have to spent with manual data handling in SQL and JDBC.

You can use Hibernate as in standalone Java applications or as in Java EE applications using servlets or EJB session beans.