QA InfoTech » Java » ORM – Object Relational Mapping

ORM – Object Relational Mapping

Data Persistence has always been the main concern in all applications. In most of the applications we find relational databases(data is stored in tables) and as a object oriented programmer we always think in the terms of objects. This can create a ‘Object-Relational Impedance Mismatch‘ means object models and relational models can not work well together. Let me explain how..

In an object oriented application we have domain classes corresponding to each table in the database tables, for example In School Management System we have Student and Course classes corresponding to Student and Course tables in the database. Business logic of the applications interacts with the domain classes which allows business logic to make use of object-oriented concepts such as inheritance, polymorphism etc but these can not be applied to database tables.

So is there any way to bridge the gap between Object model and the Relational model so that programmers can focus on business logic only instead of playing with Database result sets? ORM(Object Relational Mapping) is the answer. Below mentioned are some mismatches between object model and relational model which can be eliminated by ORM.

1. Problem of Associations : Object-oriented languages represent associations using object references, but in the relational database, association is represented by a foreign key. Object references are directional i.e association is from one object to other. For example

 public class Student {

     private String studentName;

    private Course course;     <—— Course is navigable from Student..

     ……..

 }

 public class Course {

    private String courseName;

    ……..

 }

A association can be bi-directional then we have to include object referecne in to other class also.

   public class Course {

    private String courseName;

    private Set<Student> students;   <—— Student is navigable from Course.

    ……..

 }

On the other hand a navigation has no meaning for relational database. There is no unidirectional and bidirectional navigations but we can join two or more tables.

2. Problem of data Navigation :

    public class Subject {

       private String subjectName;

       private String code;

       ……..

   }

   public class Course {

    private String courseName;

    private Set<Student> students;   <—— Student is navigable from Course.

    private Set<Subject> subjects;

    ……..

 }

In object-oriented language to get student subjects, we call student.getCourse().getSubjects(). In relational model you have to join the tables to get the subjects. The number of joins determines the depth of the relational Model. For example to get a student with given id we will fire the following query

  Select * from Student where student_id = 120.

Later on you are interested in getting students for a particular course then you will have to fire

  Select s.* from Student s join course c on s.student_id = c.fk_student_id.

what i mean to say that to get data from relational model first you need to know to which depth you need to retrieve the data. then accordingly you perform join operation. But with ORM frameworks we can fetch all the student associations in one go.

3. Problem of subtypes:

In java, we can have inheritance in our domain objects. For example, for a user billing system we can have following domain objects.

                     User——————–BillingDetails

                                                |                  |

                                                |                  |

                                CreditCard     BankAccount (Sub classes of BillingDetails)

This can not be represented in the relational model. ORM frameworks removes this gap for programmers.

There are many ORM frameworks available for JAVA like Hibernate, iBatis, TopLink etc. Using ORM frameworks we can reduce a huge amount of code and programmers can concentrate on business logic only.

About the Author