lishman levelup
«previous  next»

Getting Started


Getting Started with Hibernate

Hibernate is an object-relational persistence framework which maps plain old Java objects (POJOs) to relational database tables and transforms the data from one representation to the other.

Metadata (specified in XML or using annotations) maps the fields of a class to the columns of a table so that a persisted instance of the class is represented by a row in the table.

The SQL required to update and query the database is automatically generated by Hibernate, so the developer no longer needs to write repetitive CRUD style SQL statements by hand.

Hibernate consists of these components.
  • Metadata to describe the mapping between classes and tables.
  • An API for CRUD operations on the objects of persistent classes.
  • A query language and API.
  • Transaction management and optimization functions.

Hibernate and JPA

In the beginning there was Hibernate, an open source object-relational mapping tool (ORM) which used a proprietary API and XML for the mapping metadata.

Then, because of Hibernate's popularity, a standard Java specification for ORM called the Java Persistence API (JPA) was introduced as part of EJB 3.0. This specification favoured annotations over XML for the mapping metadata (although XML can still be used to override annotations at runtime) and introduced a new standardized API.

JPA was implemented by Hibernate as a thin wrapper around its existing core and the native Hibernate API was extended to allow JPA annotations to be used for the mapping metadata. Also, because JPA delivers only a subset of the functionality provided by Hibernate, additional Hibernate specific annotations were added to compensate for the difference.

So, when using Hibernate, we are faced with these options.
  • Use traditional Hibernate with the native API and XML metadata.
  • Use the native API with JPA annotations, with or without the Hibernate extensions.
  • Use JPA with standard annotations with the option to override the metadata with XML.
As a guideline, if cross-vendor standardization is a priority then use JPA. If additional features and more frequent product releases are more important than the ability to easily switch vendors, consider using native Hibernate.

This tutorial focuses on the second option.

Further Reading