Introduction to Hibernate

hibernate

Hibernate, created by Gavin King in 2001 as an alternative to using EJB2 style entity beans.   Its main goal was to offer better ways of persisting data than that offered by EJB2 at the time.   Since then, it has grown into what is now the best and most widely used ORM tool for Java developers.

Hibernate is a powerful, yet high performance Object to Relational persistence and query framework.   Hibernate simplifies the development of database interactions between a Java application and the underlying database.

Hibernate enables the developer to persistent classes following natural Object-oriented paradigms including inheritance, polymorphism, association, composition, and the Java collections framework. Hibernate does not interfaces or base classes for persistent classes and enables any class or data structure to be persistent.

Hibernate supports lazy initialization and supports numerous fetching strategies.  Additionally, it requires no special database tables or fields and generates much of the SQL at system initialization time instead of at runtime.

Hibernate consistently offers superior performance over straight JDBC code, both in terms of developer productivity and runtime performance.

 What is Object Relational Mapping (ORM)?

ORM stands for Object/Relational Mapping.  It employs a technique that transforms one representation to another.  In this case, Hibernate provides a framework for mapping Java object-oriented domain models to that of a relational database.

hibernate_orm

Using ORM simplifies the data creation, data manipulation and data access and will significantly reduce development time.

What are some of the advantages of using Hibernate Framework?

Hibernate is database agnostic — You can simply exchange one database for another and your same code will work without make any changes.    You can easily substitute databases like Postgres, DB2, Ingres, Informix, Sybase, SQLServer, MySQL and Oracle.

Hibernate uses Objects — You don’t need to learn SQL as hibernate works with HQL and will generate all the SQL that is necessary for you to interact with the database.   This HQL (Hibernate Query Language) is an object-oriented version of SQL.  It generates the database independent queries for you.

Hibernate is Open Source and reliable  — The hibernate framework is open source under the GNU Lesser General Public License.  It has proven time-tested reliability as it is used by tens of thousands of Java developers and thousands of companies for over ten years now.

Hibernate can auto-create tables — Hibernate framework give you the ability to automatically create the database tables for you.  No need to create the database tables before hand using a separate database tool.

Hibernate provides fast performance — Hibernate is fast because it employs a first-level cache automatically for you.   Added performance may be gained by employing a second level cache that is shared across various sessions within the application.

Hibernate is extensible — Hibernate is highly configurable and extensible.

Overview of Hibernate

hibernate_persistence

Describe the main interfaces of hibernate framework?

  • Configuration Interface — Used to configure and bootstrap hibernate.  Allowing the application to specify properties and mapping documents.
  • Session Interface — Used to get a physical connection to the database.   Sessions are lightweight objects designed to be instantiated each time an interaction with the database is necessary.  These objects are not thread safe and as such, the recommendation is to use one session per request or one session per transaction.
  • SessionFactory — This is a factory that creates the session objects for the hibernate application.   Typically only one SessionFactory object is created and shared throughout the application.
  • Transaction — Transaction interface specifies an atomic unit of work. It maintains abstraction from the transaction implementation of JTA/JDBC.  A transaction is associated with Session object and contains commit() and rollback() methods to main consistency.
  • Query — This interface represents and object-oriented representation of a Hibernate query.  This interface more closely resembles the JDBC style query.
  • Criteria –This is a simplified interface for retrieving entities by composing criterion objects.  This is done by providing filtering rules via Restrictions and logical conditions.

What’s Next

In our next post, I will show you how to set up and configure hibernate in your java project in Eclipse and begin our very first hibernate example by configuring and connecting to our database.

Stay tuned…
Working on your First Hibernate Project is now available.

 

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *