Monday, March 3, 2014

Introduction To Hibernate

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.







Saturday, December 7, 2013

Install Java on Ubuntu


Setting up java environment is very easy on Linux. I am using Ubuntu 12 . I am going to demonstrate the steps .

Step 1 - First thing you need to do is to download the Java Development Kit (jdk) from Oracle's Site .You need to download the package as per your machines configuration whether it's 32 bit or 64 bit. You can easily figure out the system architecture by running the following command

lscpu

Step 2 - I downloaded the jdk 7 and I got the following tar file.

/home/mudit/Downloads/jdk-7u45-linux-i586.tar.gz .

Step 3- Next you need to extract the content of the above mentioned file by running the following command:

tar -xvf ~/home/mudit/Downloads/jdk-7u45-linux-i586.tar.gz 

it resulted ~/home/mudit/Downloads/jdk1.7.0_45

Step 4- Once you get the folder jdk1.7.0_45 you need to run the following command:

sudo mkdir -p /usr/lib/jvm/jdk1.7.0

The above command will create a folder for your java installation.

Step 5- Next , you need to run the following command:

sudo mv ~/home/mudit/Downloads/jdk1.7.0_45/* /usr/lib/jvm/jdk1.7.0

Step 6- Now it's time to set path for java. You need to edit the startup file by running following command:

For Bash Shell
            
sudo gedit ~/.bashrc

Then you need write:

export JAVA_HOME="/usr/lib/jvm/jdk1.7.0"
   
then you need append JAVA_HOME value in PATH variable.
           
export PATH="/usr/local/apache-maven-3.1.0/bin:$JAVA_HOME/bin"

Then save and exit.

We are done with all the setup. You can check you setup by running

java -version
javac -version

both commands will run successfully.