Introduction to Hibernate API

Hibernate ORM is an object-relational mapping tool for the Java which provides a framework for mapping an object-oriented domain model to a relational database. Basically, mapping informs the ORM tool of what Java class object to store in which database table.

Hibernate is a free software and is distributed under the GNU Lesser General Public License 2.1.

Hibernate’s primary feature is mapping from Java class objects to database tables, and mapping from Java data types to SQL data types. It also provides database query and retrieval facilities. Hibernate generates SQL calls and relieves the developer from the manual handling and object conversion of the result set.

As a matter of fact, objects in an object-oriented application follow OOP principles, while objects in the back-end follow database normalization principles, resulting in different representation requirements. This problem is called “object-relational impedance mismatch“.

Hibernate employs mapping technique to resolve the object-relational impedance mismatch problems by substituting direct, persistent database accesses with high-level object handling functions.

In fact, Hibernate provides an SQL inspired language called Hibernate Query Language (HQL) for writing SQL-like queries against Hibernate’s data objects. It is the object-oriented version of SQL. It generates database independent queries so that there is no need to write database-specific queries. Without this capability, changing the database would require individual SQL queries to be changed as well, leading to maintenance issues.


Hibernate API

The Hibernate API is provided in the Java package org.hibernate. It has two important interfaces which are given below:

1) org.hibernate.SessionFactory interface

It references immutable and thread-safe object creating new Hibernate sessions. Hibernate-based applications are usually designed to make use only of a single instance of the class implementing this interface (often exposed using a singleton design pattern).

2)  org.hibernate.Session interface

It represents a Hibernate session i.e. the main point of the manipulation performed on the database entities. The latter activities include (among the other things) managing the persistence state (transient, persisted, detached) of the objects, fetching the persisted ones from the database and the management of the transaction demarcation.

A session is intended to last as long as the logical transaction on the database. Due to the latter feature Session implementations are not expected to be thread-safe nor to be used by multiple clients.