What is Hibernate?
1- Its an ORM (object relational mapping) tool.
2- Used in data access layer of application.
3- Implements JPA(java Persistence API) - JPA is just a specification , meaning there is no implementation. However you can use JAP annotation but without the implemenation nothing will happen.Think of JPA as the guidelines that must be followed or an interface, while Hibernate's
JPA implementation is code that meets the API as defined by the JPA specification and provides the under the hood functionality.
Why Hibernate came into picture?
The Problem
Lets say we want to save the user data in a relation database system and we have
User class
| ID |
| Name |
| Address |
| Phone |
| DateofBirth |
Users table
| ID | Name | Address | Phone | DateOfBirth |
Now we have objects here in java and in order to save the user object we have to write a insert sql query in java code and setting the query parameters from the user object as:
//con – connection object
//psmt- prepared statement object
String sql = “insert into Users values(?,?,?,?,?)”;
psmt = con.prepareStatement(sql);
psmt.setString(1, user.getID());
psmt.setString(2,user.getName());
........
and then
psmt.executeUpdate();
to save the data. And similarly when we fetch data we need to set the data back into User object from the ResultSet which we get after running a select query.
We have objects in java but not in database. The User object in java needs to be converted into a ROW of the Users table. In JDBC we have to map the User class's attribute to database colums of Users table manually. Now this is the main pain point, we need to convert each object into a sql query in order to persist the User data and in case of retreiving we have to convert a recordset into User object.
Now some of the pain points that we face in jdbc:
1- Mapping member variable of the class to columns of the table .
2- Mapping relationships - Its a tough task to maintain the relationships in jdbc we need to write complex join queries if we have relationship between two entities.
3- Handling data types – Data types varry from database to database. The java code becomes specific to a database.
4- Managing changes to object state – For every change in the state of object we need to take care of it by updating in db again and again.
Generally we deviate from a pure object oriented design in case of jdbc. Our java code becomes specific to a particular RDBM as we have to write the data handling code according to the RDBMS.
To get rid of these pain points Hibernate came into picture.Its a framework that handles lots of things automatically and provides a pure object oriented solution to access data from database through java code.
Main Features of Hibernate
1- Object relational Mapping.
2- Dual layer cache architecture.
3- High Performance - Supports lazy initialization ,fetching strategies. Hibernate offers superior performance over straight JDBC in terms of developer productivity and run time performance.
4- Highly Extensible - Hibernate can be configured very easily and it is extensible. We can integrate Hibernate with any framework Spring, Struts etc.
There are lot more other features which we can learn while developing application and using Hibernate API.
No comments:
Post a Comment