Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool.
An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.
The ORM tool internally uses the JDBC API to interact with the database.
Advantages of Hibernate Framework
There are many advantages of Hibernate Framework. They are as follows:
- Opensource and Lightweight
- Hibernate supports Inheritance, Associations, Collections
- In hibernate if we save the derived class object, then its base class object will also be stored into the database, it means hibernate supporting inheritance
- Hibernate supports relationships like One-To-Many,One-To-One, Many-To-Many-to-Many, Many-To-One
- This will also supports collections like List,Set,Map (Only new collections)
- 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 has its own query language, i.e hibernate query language which is database independent
- So if we change the database, then also our application will works as HQL is database independent
- HQL contains database independent commands
- While we are inserting any record, if we don’t have any particular table in the database, JDBC will rises an error like “View not exist”, and throws exception, but in case of hibernate, if it not found any table in the database this will create the table for us.
- 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 supports annotations, apart from XML
- Hibernate provided Dialect classes, so we no need to write sql queries in hibernate, instead we use the methods provided by that API.
- Getting pagination in hibernate is quite simple.
The Hibernate architecture includes many objects persistent object, session factory, transaction factory, connection factory, session, transaction etc.
There are 4 layers in hibernate architecture java application layer, hibernate framework layer, backhand api layer and database layer.Let’s see the diagram of hibernate architecture:
Hibernate framework uses many objects session factory, session, transaction etc. alongwith existing Java API such as JDBC (Java Database Connectivity), JTA (Java Transaction API) and JNDI (Java Naming Directory Interface).
Elements of Hibernate Architecture
For creating the first hibernate application, we must know the elements of Hibernate architecture. They are:
The SessionFactory is a factory of session and client of ConnectionProvider. It holds second level cache (optional) of data. The org.hibernate.SessionFactory interface provides factory method to get the object of Session.
The session object provides an interface between the application and data stored in the database. It is a short-lived object and wraps the JDBC connection. It is factory of Transaction, Query and Criteria. It holds a first-level cache (mandatory) of data. The org.hibernate.Session interface provides methods to insert, update and delete the object. It also provides factory methods for Transaction, Query and Criteria.
Allows the application to define units of work, while maintaining abstraction from the underlying transaction implementation (eg. JTA, JDBC).
A transaction is associated with a Session and is usually instantiated by a call to Session.beginTransaction(). A single session might span multiple transactions since the notion of a session (a conversation between the application and the datastore) is of coarser granularity than the notion of a transaction. However, it is intended that there be at most one uncommitted Transaction associated with a particular Session at any time.
The transaction object specifies the atomic unit of work. It is optional. The org.hibernate.Transaction interface provides methods for transaction management.
The ConnectionProvider interface is not intended to be exposed to the application. Instead it is used internally by Hibernate to obtain connections. It is a factory of JDBC connections. It abstracts the application from DriverManager or DataSource.
Contract for generating Hibernate Transaction instances. It is a factory of Transaction.