Monday, 22 July 2013

An introduction to ORM and Hibernate

For years EJB Entity Beans was the only persistence framework which managed transaction and concurrency in java. Entity Beans required a server for running and was difficult to code and debug.

ORM (Object Relational Mapping) frameworks came into picture as a lightweight and simpler alternative to EJB 2.0 entity beans as a persistence framework that manages transaction and concurrency. All the ORM based persistence framework has the agenda of mapping a defined POJO (Plain Old Java Object) entity to a database table. There will be a mapping file defined for mapping the java object to database table. In the newer version of ORMs, there is alternate mapping via annotations. These ORMs can run in server and non-server environment.

Hibernate is arguably the most prominent ORM based persistence framework available in the market for java. There is NHibernate available for .NET solution.
Some of the most prominent ORMs in java are:

  • Java Persistence API (JPA) - We will discuss JPA also to some extent here.
  • Toplink
  • iBATIS
  • Java Data Objects (JDO)

Why ORM frameworks?

  • One of the biggest advantage of using an ORM comes while we require multiple database support in a project. The ORM will take care of converting the SQL query syntax based on the underlying database using SQL Dialect.
  • ORM frameworks saves a lot of development time as it generates the SQL files and takes care of the execution. So, developer needs to take care of just populating the data object and does not have to take care of the sql symantics, setting parameters etc. there by saving development and debugging time.
  • Developers can concentrate more on business logic than managing persistence as the business objects will get directly mapped to the data in database.

ORM Vs Plain JDBC

  • ORM frameworks are slower than JDBC as it is a wrapper over JDBC and the additional query generation work needs to be done at run time using reflection and the provided mapping metadata. But most of the ORM frameworks uses caching frameworks to cache the data managed by the framework and retrieve it from cache enhancing performance. Also, on application startup, these frameworks loads all the mapping/annotated data to enhance performance.
  • Developer needs to learn and have expertise in the ORM framework to deal with the practical issues.

JPA (Java Persistence API): 

We also have JPA which is the replacement for Entity Beans in EJB 3.0.
JPA is an abstraction layer above all other persistence frameworks. They provides a plugin to these persistence frameworks. So, if you want to use hibernate as your persistence provider for JPA, just we need to mention that in the config file. Tomorrow if you see some other persistence provider will be better to serve your requirement, you can do so without much impact using JPA.

High Level Architecture and Working of Hibernate:


Configuration:
Hibernate application would be having two configuration files

  1. hibernate.properties 
  2. hibernate-cfg.xml 
The connection details, customized connection pooling for non-server environments, mapping data of various database tables to java POJOs etc. are stored in these files. For modularisation, hibernate-cfg.xml may refer to multiple mapping files in which the table to POJO mapping exists. From hibernate 3.0 onwards, mapping can also be done via annotations.

The hibernate properties file needs to be loaded by creating a hibernate configuration object as below:
new Configuration().configure();

Session Factory Object:
Based on the hibernate configuration, a session factory is created. This session factory manages the connection pool and provides connection sessions to the user as requested.

Session factory is created using below code:
sessionFactory = new Configuration().configure().buildSessionFactory();

Session Object:

A session object represents a physical database connection session. Persistent objects are managed via session object.

Session object creation code:
Session session = sessionFactory.openSession();

Transaction Object:
Transaction represents a single unit of work with the database. Transactions in hibernate gets managed by the underlying transaction manager which will be JTA or JDBC.

Transaction tx = session.beginTransaction();

Entity POJO:
With the transaction ready, all you need is the entity POJO. This is a simple java bean having properties defined as the member variables with their respective getter/setters. The java bean to table mapping can either be annotated or mentioned in a mapping file along with the mapping/annotation of java class properties to database columns.

The session object has got various methods for the DML operations to take place and finally the transaction can be committed or rolled back based on its success.

Query Object:
The query objects use HQL (Hibernate Query Language) or SQL to fetch data from database.

Criteria Object:
Criteria object is used to create and execute where clause or the filter criteria to fetch data from database.

No comments:

Post a comment