Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old
Java objects to relational database tables using (XML) configuration files.Its purpose is to relieve the developer
from a significant amount of relational data persistence-related programming tasks.
Hibernate is a persistence framework which is used to persist data from Java environment to database. Persistence is a process of storing the data to some permanent medium and retrieving it back at any point of time even after the application that had created the data ended.
The above diagram shows minimal architecture of Hibernate. It creates a layer between Database and the Application. It loads the configuration details like Database connection string, entity classes, mappings etc. Hibernate creates persistent objects data between application and database. In order to persist data to a database, Hibernate create an instance of entity class. 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.cfg.xml file and with the help of TransactionFactory and ConnectionProvider implements all the configuration settings on a database.
Benefits of Hibernate
Hibernate's primary feature is mapping from Java classes to database tables (and from Java data types to SQL data types). Hibernate also provides data query and retrieval facilities. It generates SQL calls and relieves the developer from manual result set handling and object conversion. Applications using Hibernate are portable to supported SQL databases with little performance. Some of the other key benifits of Hibernate:
- Transparent persistence based on POJOs without byte code processing
- Powerful object-oriented hibernate query language
- Descriptive O/R Mapping through mapping file.
- Automatic primary key generation
- Hibernate cache : Session Level, Query and Second level cache.
- Performance: Lazy initialization, Outer join fetching, Batch fetching
Though Hibernate Framework is not the only persistence solution, it has become very famous over the recent past because of its huge variety of features when compared with its competitors. It takes much of the database related boiler-plate code from the developers, thereby asking the developers to concentrate on the core business logic of the application and not with the error-prone SQL syntax.
Saving Object Without Hibernate
Without hibernate you will follow below steps for saving object into database
- JDBC Database Configuration
- The model class
- Service method to create the model object
- Database Design
- DAO method to save the object using SQL quereies
Saving Object With Hibernate
If you are using hibernate, then you will follow below steps for saving object into database
- JDBC Database configuration - Hibernate configuration
- The Model object - Annotations
- Service method to create the model object - Use the Hibernate API
- Database design - Not needed!
- DAO method to save the object using SQL queries - Not needed!