Wednesday, 11 January 2017

Introduction on Hibernate (ORM tool)

What actually hibernate means -> When we do hibernate operation in windows it logoff our computer i.e. it goes in a sleep mode.
This hibernate tutorial provides in-depth concepts of Hibernate Framework with simplified examples. It was started in 2001 by Gavin King as an alternative to EJB2 style entity bean. The stable release of Hibernate till July 16, 2014, is hibernate 4.3.6. It is helpful for beginners and experienced persons.
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.
If we see MVC based application then we divide it into View, Controller and Model. Hibernate in MVC helps us in model part. We can divide model into Business, DAO and Services. Business like business logic, DAO like database and services like security, transaction,mail etc. Bascically Hibernate helps us in DAO. To provide database service JDBC is there but still we use Hibernate. Hiberante is designed for Enterprise application. If we simply go with our simple java technology then our business logic may loss as well as security and all.
Earlier in 20s it was not easy to design our application without EJB but now we can write our application today without EJB. Even we write our application without EJB then it was not possible to build our model part. In case if we build it we should not call it as Enterprise Application. Because it cant be able to provide services like multi user access, then we may loss some data and also it cant be able to provide services like security. So if we go with simple java code our business logic may loss because of security, transaction etc.
Suppose if its an Banking system application then without security any body can access our data by doing URL rewriting and also if from one account to another if we want to transfer money and suddenly the server crashes then the receiver would not receive the money and it also gets deducted from the sender account.
So if the system crashes then my amount should be roll back and all this can be done by the help of transaction service.
So by using EJB-S(session) and EJB-E(entity) we can design our business and DAO part and we can buy the service from third-party. We need to buy our services from Application Servers like-Weblogic, Glassfish, JBose etc.
So lets understand clearly that why instead of JDBC we use Hibernate. Let us take an example that if we want to write our DAO and wants to interact with database using JDBC, then JDBC would allow us to write Statement, PreparedStatement or Callable Statement and whatever query we would write it would be database specific.
Suppose if we had an contract with Oracle for 1 year and using JDBC we design our application based on Oracle specific language query. But suppose after 1 year our contract was over then we want our database to be shifted to Teradata or any other kind of database, then at that time we would face problem while interacting with the database as our existing application is based on oracle and the query is not understand by Teradata or other database then we need to again rewrite our whole query. So here Hibernate provides a great role in just writing a HQL (Hibernate Query Language) instead of database specific code so that every database can interact with the same query without any changes in the existing query.
Let us take one more example if there is an Banking application and there are around 1 lakh employee and daily 1 employee needs to login and logout i.e. 2 times application needs to hit the server, so for 1 lakh employee 2 lakh times it will hit the server which may slow down our application. So to overcome with this problem there is one terminology called cache which helps to save the data from the server to our local machine, and this service is not provided by JDBC but its provided by Hibernate.
To understand it clearly lets take an example of GMail suppose when for the first time when you install the application into your local system then its take long time to get your all data. But on the second time when you login it wont take much time and you still get all the data. The technology behind that is our browser used to store those data into cache. If you want to try it clear your all history, session, cookies and cache from your browser then you will be able to see the actual real-time impact.
The main disadvantage of EJB-E is that it must require EJB-S and also application servers, without it we cannot work. So for big application like banking system we can use this things like we can buy services and all. But for small application we cannot afford the cost of buying services like servers and all, so they would not use EJB.
So for small Enterprise Application simple JDK and JRE is enough we would not buy services and all. So to run any small small application simply we can download Hibernate Jar files.
There are many services which requires to interact with EJB-S. So to run those services we can simply merge our Hibernate with EJB-S and for remaining we can directly go through Hibernate.
Again there is a framework called Spring which will fully remove the dependency of EJB-S.

1 comment: