ORM (Object Relational Mapping)


1. What is Object Relational Mapping (ORM)?

ORM or Object Relational Mapping is a system that implements the responsibility of mapping the Object to Relational Model. That means it is responsible to store Object Model data into Relational Model and further read the data from Relational Model into Object Model.

ORM object relational mapping

2. Why ORM (Object Relational Mapping)?

When work with object oriented programming to persist data in RDBMS, there is mismatches between object model and relational model if work with traditional techniques like JDBC. ORM fills gap in the following mismatches between Object model and relational model.

3. Impedance Mismatch between Object Model and Relational Model

The Object Oriented (Domain) model use classes whereas the relational database use tables. This creates a gap (The Impedance Mismatch). Due to the difference between the two different models, getting the data and associations from objects into relational table structure and vice versa requires a lot of tedious programming.

Loading and storing objects using a tabular relational database exposes us to 5 mismatch problems.

  1. Granularity
  2. Inheritance
  3. Identity
  4. Associations
  5. Data navigation

3.1. Granularity :

Granularity is the extent to which a system could be broken down into small  parts. Lets take an example of Person details, we could broke down person details into two classes one is Person and another is Address for code re-usability and code maintainability purpose. But assume that to store Person details in database there is only one table called Person.

Sometimes you will have an object model which has more classes than the number of corresponding tables in the database (we says the object model is more granular than the relational model). This is Granularity Mismatch between Object Model and Relational Model.

3.2. Inheritance or Sub-types :

Inheritance is a natural concept in object-oriented programming languages. However, RDBMSs do not define anything similar on the whole (yes some databases do have subtype support but it is completely non-standardized). This is Inheritance Mismatch between Object Model and Relational Model.

3.3. Identity :

A RDBMS defines exactly one notion of ‘sameness’: the primary key. Java, however, defines both object defines both object identity a==b and object equality a.equals(b). This is Identity Mismatch between Object Model and Relational Model.

3.4. Associations :

Associations are represented as unidirectional references in Object Oriented languages whereas RDBMSs associations are bidirectional by using foreign keys. If you need bidirectional relationships in Java, you must define the association twice. Likewise, you cannot determine the multiplicity of a relationship by looking at the object domain model.

3.5. Data Navigation :

The way you access data in Java is fundamentally different than the way you do it in a relational database. In Java, you navigate from one association to an other walking the object network.

This is not an efficient way of retrieving data from a relational database. You typically want to minimize the number of SQL queries and thus load several entities via JOINs and select the targeted entities before you start walking the object network.

The solution for above problems is use ORM tool :

An Object Relational Mapping Tool provides a simple API for storing and retrieving Java objects directly to and from the relational database. ORM is technique that allows an application written in an object oriented language to deal with information as objects, rather than using database specific concepts such as Rows, Columns and Tables which facilitated by Object/Relational Mapper.

4. Advantages of ORM tools :

Following are the main features of ORM tools.

  1. Better System Architecture
  2. Reduce Coding Time
  3. Caching And Transaction management

4.1. Better System Architecture :

A good ORM tool designed by very experienced software architects will implement effective design patterns that almost force you to use good programming practices in an application. This can help support a clean separation of concerns and independent development that allows parallel, simultaneous development of application layers. If you create a class library ORM-generated data access code, you can easily reuse the data objects in a variety of applications. This way, code re-usability will increase. If use in this way there will be a cleaner separation of data access layer.

4.2. Reduce Coding Time :

Most of the time database access code spec is simple insert, update or delete. These are SQL statements sometimes are quite tedious to code, if you use JDBC it does not allow you to store objects directly to the database, you must convert the objects to a relational format. ORM tool helps here, by generating them on fly and there by saves a lot of time.

4.3. Caching and Transactions :

Most ORM tools such as hibernate come with features such as Caching and Transactions. Caching enables you to improve performance of data access operations. Most of the ORM tools like Hibernate provides support for multiple transactions, they has locking mechanism and versioning, which enables an application to have multiple transactions and it does not affect the application performance.

Following are the popular ORM implementations.

  1. Hibernate – Open Source
  2. Top Link – By Oracle
  3. Eclipse Link – Eclipse Persistence Platform
  4. Open JPA – By Apache
  5. MyBatis – Open Source – Formerly known as iBATIS

You might be interested in following articles :

  1. What is jpa (Java persistence API)?
  2. JPA architecture
  3. Persist an entity in database using JPA with Hibernate.