Monday, December 2, 2013

Introduction to Object Relational mapping...

We hear about Objects, we hear about relational databases. What is ORM then ?

We use relational databases as a backbone of our middle tier applications written in Object Oriented Programming Languages. In most of the scenarios we manipulate over data through our applications. To design the functionality of the application we generally use high level languages such as C++, Java,, C# etc. Now, to deal with relational database in back-end we need specific language which are based on relational tables, widely known as SQL (Structured Query Language).

Like java applications understand only objects, databases understand only tables and relations between them.

Hey wait, I have a question here, if they can talk different languages, how do they exchange information ?
Yes you are right, there is a gap between these two. To address this gap between  object oriented and relational model in traditional way we used JDBC. Java to Database Connectivity can be successfully achieved through JDBC but JDBC returns data in form of  resultset which have certain limitations like it will be more complex for large projects where we have multiple database tables and multiple objects to synchronize. Always programatically developer has to change relational data to java objects. Same functionality we can achieve in an effective way through ORM. Object relational mapping is the technique to convert data between relational data and object oriented data.
Now, we can go for a formal definition of ORM,
ORM - ORM is a technique to convert Objects into Database relations and vice-versa. That's it. Following is a visual representation for t he same,
ORM technique

Hmmm...(Awww...), it makes things simpler. Interesting. So, how do we do it ?
In the world of Java, Object relational mapping can be achieved through many tools such as Hibernate, Java Data Objects (JDO),Spring ORM and many more.
These tools are highly configurable, thus they are highly maintainable, reusable and really makes life easy while working with data.

So, which tool is the best ?
Well, every one has its own pros and cons. All come in different features and functionality. Each has their own implementations and . I will be talking about Hibernate here. So, you are free to chose anyone based on your application requirement.

Now, I have an example on how Hibernate helps in achieving this feature,

Lets say you are an Odia guy and you want some books from your friend who is a Bengali guy i.e your friend has Bengali version of the books you want. So to read those books you can do two things either you yourself convert the books in to Odia and then read else ask any translator to translate the book and provide you with the translated version of the Bengali books.
Here the first way is working as a normal JDBC flow where developer has the whole responsibility of translating the back-end data into Java objects. On the other hand, the second way is treated as Hibernate translation. Where the translator is working on translating the back-end data into Java objects and developer is more concentrated on the business logic.

Well, I would like to thank one of my readers who has contributed this article.
Article contributed by: Amrita Pattnaik

Palash Kanti Kundu