About NewTechnoBuzz
Advertise
Contact Us

Monday, July 7, 2014

Hibernate Overview

Hibernate is an Object-relational mapping (ORM) tool. It raised as an open source persistent framework created by Gavin King in 2001. It is a powerful, high performance Object-Relational Persistence and Query service for any Java Application.

What is Object Relational Mapping (ORM)?
Object-relational mapping or ORM is a programming method for mapping the objects to the relational model where entities/classes are mapped to tables, instances are mapped to rows and attributes of instances are mapped to columns of table.

Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieve developer from data persistence related programming tasks.

Hibernate sits between traditional Java objects and database server to handle all the work in persisting those objects based on the appropriate O/R mechanisms and patterns.


The above diagram shows a comprehensive architecture of Hibernate. In order to persist data to a database, Hibernate create an instance of entity class (Java class mapped with database table). This object is called Transient object as they are not yet associated with the session or not yet persisted to a database. To persist the object to database, the instance of SessionFactory interface is created. SessionFactory is a singleton instance which implements Factory design pattern. SessionFactory loads Hibernate configuration (hibernate.cfg.xml) file.

Each database connection in Hibernate is created by creating an instance of Session interface. Session represents a single connection with the database. Session objects are created from SessionFactory object.

Hibernate also provides built-in Transaction APIs which abstracts away the application from underlying JDBC or JTA transaction. Each transaction represents a single atomic unit of work. One Session can span through multiple transactions.

Hibernate Advantages

  • Hibernate maps Java classes to database tables using XML files or annotations and without writing any line of code.
  • Provides simple APIs for storing and retrieving Java objects directly to and from the database.
  • If there is change in Database or in any table then the only need to change XML file properties or entity class.
  • Abstract away the unfamiliar SQL types and provide us to work around familiar Java Objects.
  • Hibernate does not require an application server to operate.
  • Manipulates Complex associations of objects of your database.
  • Minimize database access with smart fetching strategies.
  • Provides Simple querying of data.
  • Hibernate supports Inheritance, Associations, Collections.
  • Hibernate supports relationships like One-To-Many,One-To-One, Many-To-Many-to-Many, Many-To-One.
  • In jdbc all exceptions are checked exceptions, so we must write code in try, catch and throws, but in hibernate we only have un-checked exceptions, so no need to write try, catch, or no need to write throws. Actually in hibernate we have the translator which converts checked to un-checked.
  • Hibernate has capability to generate primary keys automatically while we are storing the records into database.
  • Hibernate supports caching mechanism by this, the number of round trips between an application and the database will be reduced, by using this caching technique an application performance will be increased automatically.
  • Hibernate provided Dialect classes, so we no need to write sql queries in hibernate, instead we use the methods provided by that API.

Hibernate Disadvantages

When a thing has advantages then it also has some disadvantages also and so does the hibernate:
  • It is little slower than pure JDBC, actually the reason being hibernate used to generate many SQL statements in run time. So, JDBC is more beneficial than hibernate for generating reports because of performance issues.
  • Little or no capabilities for remote access and distributability.

Supported Databases

Hibernate supports almost all the major RDBMS. Following is list of few of the database engines supported by Hibernate.
  • DB2
  • FrontBase
  • HyperSQL (HSQL)
  • Informix
  • Ingres
  • JDataStore
  • Microsoft SQL Server
  • MySQL
  • Oracle
  • PostgreSQL
  • SAP DB
  • Sybase
Please feel free to post your comments and queries along with your feedback about the article. It'll help us to improve the content.


0 comments