Thursday, January 18, 2007

Hibernate Story

Hibernate Story

Introduction

Hibernate is an object-relational mapping (ORM) solution for the Java language. It is free, open source software that is distributed under the GNU Lesser General Public License. It provides an easy to use framework for mapping an object-oriented domain model to a traditional relational database.

The purpose of Hibernate is to relieve the developer from a significant amount of common data persistence-related programming tasks. Hibernate adapts to the developer's development process, whether starting from scratch or from a legacy database.

Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities and can significantly reduce development time otherwise spent with manual data handling in SQL and JDBC. Hibernate generates the SQL calls and relieves the developer from manual result set handling and object conversion, keeping the application portable to all SQL databases.

Hibernate provides transparent persistence for "Plain Old Java Objects"; the only strict requirement for a persistent class is a no-argument constructor. (Proper behavior in some applications also requires special attention to the equals() and hashCode() methods.)

Hibernate is typically used both in standalone Java applications and in Java EE applications using servlets or EJB session beans.


Who founded Hibernate?

Hibernate was developed by a team of Java software developers around the world led by Gavin King. JBoss group later hired the lead Hibernate developers and worked with them in supporting Hibernate. This move brought Hibernate development under Java EE Specifications.The current version of Hibernate is Version 3.1, which enriched the platform with new features like a new Interceptor/Callback architecture, user defined filters, and JDK 5.0 Annotations (Java's metadata feature). Hibernate 3 is also very close to the EJB 3.0 specification (although it was finished before the EJB 3.0 specification was released by the Java Community Process) and serves as the backbone for the EJB 3.0 implementation of JBoss.
Interview With Gavin King
What is Object-Relational Mapping(ORM)?

Object-Relational mapping is a programming technique that links databases to object-oriented language concepts, creating (in effect) a "virtual object database." There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to code their own object-relational mapping for their systems.

Architecture

A (very) high-level view of the Hibernate architecture:


This diagram shows Hibernate using the database and configuration data to provide persistence services (and persistent objects) to the application.

We would like to show a more detailed view of the runtime architecture. Unfortunately, Hibernate is flexible and supports several approaches. We will show the two extremes. The "lite" architecture has the application provide its own JDBC connections and manage its own transactions. This approach uses a minimal subset of Hibernate's APIs:


The "full cream" architecture abstracts the application away from the underlying JDBC/JTA APIs and lets Hibernate take care of the details.


Heres some definitions of the objects in the diagrams:

SessionFactory (net.sf.hibernate.SessionFactory)

A threadsafe (immutable) cache of compiled mappings for a single database. A factory for Session and a client of ConnectionProvider. Might hold an optional (second-level) cache of data that is reusable between transactions, at a process- or cluster-level.

Session (net.sf.hibernate.Session)

A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC connection. Factory for Transaction. Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier.

Persistent Objects and Collections

Short-lived, single threaded objects containing persistent state and business function. These might be ordinary JavaBeans/POJOs, the only special thing about them is that they are currently associated with (exactly one) Session. As soon as the Session is closed, they will be detached and free to use in any application layer (e.g. directly as data transfer objects to and from presentation).

Transient Objects and Collections

Instances of persistent classes that are not currently associated with a Session. They may have been instantiated by the application and not (yet) persisted or they may have been instantiated by a closed Session.

Transaction (net.sf.hibernate.Transaction)

(Optional) A single-threaded, short-lived object used by the application to specify atomic units of work. Abstracts application from underlying JDBC, JTA or CORBA transaction. A Session might span several Transactions in some cases.

ConnectionProvider (net.sf.hibernate.connection.ConnectionProvider)

(Optional) A factory for (and pool of) JDBC connections. Abstracts application from underlying Datasource or DriverManager. Not exposed to application, but can be extended/implemented by the developer.

TransactionFactory (net.sf.hibernate.TransactionFactory)

(Optional) A factory for Transaction instances. Not exposed to the application, but can be extended/implemented by the developer.

Given a "lite" architecture, the application bypasses the Transaction/TransactionFactory and/or ConnectionProvider APIs to talk to JTA or JDBC directly.

Books for reference

Hibernate: A Developer's Notebook







No comments: