Post by – Subhash Bhajankar
What is the problem that hibernate is trying to solve?
Let’s take an example of simple class that you have in your application. Referring Image 1, we have Employee object in our java class which has six fields. In our running application We would have lots of such objects in memory, What if I restart my application ? Everything will be lost. So to overcome this problem and persist data inside my application i will save it in Database.
As shown in below image every object in Java will be represented as single row. Here, class corresponds to table and object of class corresponds to row. This was something that we were been doing over the year (Off course before ORM Tool).
Normal approach to save data in Java is something like :
- Create JDBC Connection
- Connect Database
- Create SQL String dynamically by taking fields value one by one
- And also manage all transaction, Exception handling, rollback and commit
So problem here is I have object here in java but I do not have objects here in Database level. 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.
Wouldn’t it be great, if we have something, Some Tool, Some design pattern, which will take care of converting data between object and record?
If somehow, we try to create it by our own, We will face problems like :
- Mapping member variables to columns
- Mapping relationship
- Handling data types
- Managing changes to object state
- Offcource Performance also 🙂
To overcome all these problems we use ORM tool like Hibernate or Ibatis
Advantages of Hibernate :
- There are many advantages but I will list very few :
- One of Most common ORM accepted by Developers Community Worldwide
- Uses Java Persistance API (JPA)
- High Performance
- Reliability and Scalability
- Very easy to configure
This is my first post, so please share your thoughts.
Leave a Reply