Home > DeveloperSection > Beginner > Object Relational Mapping

Object Relational Mapping


Java Database  Java  ORM  Hibernate 
Ratings:
1 Comment(s)
 984  View(s)
Rate this:

Object Relational Mapping

 

·         When we are working with an object –oriented systems, chances are that there may be a mismatch of object model and the relational database.

·         Since RDBMS represents data in a tabular format whereas object oriented languages like Java, C++, C# represents it as an interconnected graph of objects. 

For instance consider the case of Person class here, it consist of private properties, constructors and public methods.

 

public class Person {

     

      private int id;

      private String name;

      private String address;

      private int age;

     

      public Person(int id, String name, String address,int age){

            this.id = id;

            this.name = name;

            this.address = address;

            this.age = age;

      }

 

      public int getId() {

            return id;

      }

 

      public String getName() {

            return name;

      }

 

      public String getAddress() {

            return address;

      }

 

      public int getAge() {

            return age;

      }

     

 

}

 

 

Now this above object need to be stored and retrieved into a RDBMS table

create table PERSON (

   id INT NOT NULL auto_increment,

   name VARCHAR(20) default NULL,

   address  VARCHAR(20) default NULL,

   age     INT  default NULL,

   PRIMARY KEY (id)

);

 

1.       If we analyze this model carefully, there is one problem. What if we need to modify the design of our database after having developed few pages or our application?

2.       Also, loading and storing objects in a relational database exposes us to five other mismatch problems:

a.       Inheritance: RDBMSs don’t define anything similar to inheritance which is a natural paradigm in OOPS languages.

b.      Granularity: Sometimes we will have an object model which has more classes than the number of corresponding tables in the database.

c.       Navigation: The ways we access objects in java and in a RDBMS are fundamentally different.

d.      Identity: A RDBMS defines exactly one notion of ‘sameness’, the primary key. But java, defines both object identity (x == y) and object equality (equals(y)).

e.      Associations: Object-oriented languages represent associations using object references where as a RDBMS represent an association as a foreign key.

For all handling all these impedance mismatches we use Object Relational Mapping (ORM). It take care of all the differences between an object and an RDMS.

ORM Definition:

ORM is acronym for Object Relational Mapping, it is a programming technique for converting data between relational database and object oriented programming languages like Java, C++, and C#.

ORM Benefits:

·         ORM make us get rid of database implementations. There is no need to handle DB logics.

·         It hides details of SQL queries from Object Oriented logic.

·         Let business code access objects rather than DB tables.

·         It is uses JDBC as a base layer.

·         Entities are based on business concepts rather than database structure.

·         It is equipped with features like Transaction management and automatic key generation.

·         Increase the pace of application development.

 

ORM components:

ORM consists of four important component entities:

1.       An API to perform basic CRUD operations on objects of persistent classes.

2.       A language or API to specify queries that refer to classes and properties of classes.

3.       A configurable facility for specifying mapping Meta data.

4.       A technique to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions.

Java ORM Frameworks:

A persistence firework is an ORM service that stores and retrieves objects into a relational database.

·         Enterprise JavaBeans Entity Beans

·         Java Data Objects

·         Castor

·         TopLink

·         Spring DAO

·         Hibernate

 


good

By Chintoo Semi on   one year ago
this is good article.

Don't want to miss updates? Please click the below button!

Follow MindStick