What is Hibernate?
Hibernate is a Java framework that simplifies the development of Java application to interact with the database. It is an open source, lightweight, ORM (Object Relational Mapping) tool. Hibernate implements the specifications of JPA (Java Persistence API) for data persistence.Hibernate facilitates the storage and retrieval of Java domain objects via Object/Relational Mapping.
Why Hibernate?
Ever since I have started my career I have been using jdbc to work with database. Then why this hibernate got so much popularity over jdbc in recent years.
We use age old approach to save data in Java using jdbc like :
1.Create JDBC Connection.
2.Connect Database.
3.Create SQL String dynamically by taking fields value one by one.
4.And manage all transaction, Exception handling, rollback and commit.
The problem is I have objects in java but I do not have objects in database . So for saving I have to convert each object’s data into SQL query. Also for retrieving we have to do opposite like convert Record set into object. So at the end we are repeating converting Object data to SQL and SQL result to object.
To overcome this problem Hibernate is used.
Hibernate automatically gets connected to database using connection properties. It uses
HQL(Hibernate query language) to execute the queries and maps the java objects with database objects.
Mapping is done based on properties specified in Hibernate.cfg.xml file.Session is used to persist and retrieve object from database.
Session factory is an interface that helps to create an instance of a session. There must be only one session factory per database. For example, if you are using MySQL and Oracle in your application, one session factory for MySQL and one session factory for Oracle is maintained. There will not be more than one session factory for MySQL alone.
Advantages of Hibernate
1.Database Independent
Hibernate uses HQL queries which are database independent in most cases.Hence, it is easy to migrate to a new database. It is achieved by using a friendly dialect to communicate with the database. The database can be specified using a dialect in the Hibernate properties file as follows.
hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
For example, consider the following scenario. You need to fetch the first 10 entries of a table. How this is implemented in different databases is explained below:
#MySQL
SELECT column_name FROM table_name ORDER BY column_name ASC LIMIT 10;
#SQL Server
SELECT TOP 10 column_name FROM table_name ORDER BY column_name ASC;
In Hibernate, this can be done as follows
Session.CreateQuery(“SELECT E.id FROM Employee E ORDER BY E.id ASC”).SetMaxResults(10).List();
2.Transaction Management
Transaction is group of actions done to complete one task.If all the actions succeed then task is finish and transaction is completed successfully.If some actions fail then the whole task is failure and thus the transaction also fails. In JDBC, if a transaction is a success you need to commit. Otherwise, you need to perform a rollback with the following commands:
con.commit();
con.rollback();
In Hibernate, you don’t have to commit and roll back these transactions, as it is implicitly provided.
3.Hibernate Caching
Hibernate provides a caching mechanism, which helps reduce the number of hits, that your application makes to the database server. There is no such caching mechanism available in JDBC. This is because Hibernate stores the object in session, which is available until the transaction is active. When a particular query is executed repeatedly, the value stored in the session is used. When a new transaction begins, the object is fetched again from the database and is stored in the session. In fact, two levels of caches are provided by Hibernate
first level cache (by default available) and second level cache.
4.Audit Functionality
This functionality I have not used in my project but it is very interesting feature in hibernate.Hibernate provides a library called “Envers”, which enables us to easily gain audit functionality. It creates tables and stores the audited data with version number from where we can track the changes made to the entity.
Proverb says “No one in this world is perfect” . Same holds true with hibernate.There are some drawbacks of hibernate which may hamper your application performance if not tuned properly.
- Lots of API to learn: A lot of effort is required to learn Hibernate. So, not very easy to learn hibernate easily.
- Debugging: Sometimes debugging and performance tuning becomes difficult.
- Slower than JDBC: Hibernate is slower than pure JDBC as it is generating lots of SQL statements in runtime.
- Not suitable for Batch processing: It advisable to use pure JDBC for batch processing.
- Not suitable for Small projects : For small project having few tables it is not recommended to work with hibernate.
- Does not allow multiple inserts : Hibernate does not allow some type of queries which are supported by JDBC. For example It does not allow to insert multiple objects to same table using single query. Developer has to write separate query to insert each object.
- Generates complex queries with many joins : For complex data, mapping from Object-to-tables and vise versa reduces performance and increases time of conversion. References:-https://javabeginnerstutorial.com/hibernate/hibernate-framework-basic/https://www.roseindia.net/hibernate/Introduction-to-Hibernate-Framework.shtml