Wednesday, 14 February 2018

ORM - Object Relational Mapping

Overview

When we work on enterprise level application's in Java, most of the times we come across the various relational databases. For a application developer, apart from main business logic, putting efforts for database handling part, which will be time consuming and  tedious task for him. It may include lot of activities like DB configuration, DAO/DTO code creation, handling transactions and rollbacks, and exception handling for same.

Now is there anyone who can take developer out of this stuff, and let them only concentrate on Business Logic only?? Well!! Yes... its ORM tools (Object Relational Mapping).

Why ORM

In above scenario we are more clear why we need ORM. 
ORM tools may simplify efforts for data creation, its manipulation and access of persisted data in database, and more stuffs on transactions and sessions side.

Also you can think! in java, mainly we talk about OOPs (Object Oriented Programming), now what does it mean? it mean, everything in application we treat in terms of objects. And that object holds state for specific purpose, with its member variables.
Now while working with OOPs/ Java, when it comes to Relational Database for persistence and accessing, in conventional way (simple JDBC java code), we do deconstruct  object back to variables for making sqls/statements and while reading from database, again simple variables we put back in object instance variables to construct meaningful object back.
If we think of this as a whole, end to end, it somehow breaks OOPS feature for java. 
So ORM will also help in this case, to make everything as OOPS way. i.e. to ORM we pass a object for persistence, and from ORM we get object as row presentation DB.

Note that ORM tools internally uses JDBC API's to interact with underlying databases.

Some Examples for ORM tools are - 

  • Hibernate
  • TopLink
  • MyBatis
  • and many more...