Scaffold your ideas into application
Get your free alpha access now.
Only 445 left, Hurry!
11 Aug 2016

Object Relational Mismatch

Today ORM is almost become a default choice for implementing database connectivity in any technology, it started with Hibernate which was one of the first popular ORM then other languages followed the pack and now there is an ORM in possibly every language.

Some times we get so used to solution that we forget what the problem was, it may seems naive to say that, but trust me I have heard of people trying to use ORM with NoSQL databases.

Object Relational Impedance Mismatch is an issue arising due to the way data is stored and represented in RDBMS (Relational Databases Management System) and Object Oriented Languages.

RDBMS store data in form of tables, while OOP languages represent data in form of Classes. Normally we would map Class directly to a Table which works fine, the problem arises on storing Relationships . In OOPs languages like Java we would have relationships are nested objects while in Relational Databases we only have tables which are at same level, no nesting, which is why we have foreign keys as a way to represent relations.

Lets consider an example or User, Role and Permission model. This is something we would be using through out our articles and code examples. Lets use following conditions to define relations

  • User can have multiple Roles
  • Role can have multiple Permissions
  • Same Role can be assigned to multiple User

Using these condition, lets try to write some java code for this.

User.java

package com.scaffoldthis.model;
import java.util.List;

public class User { 

    private String name;

    private List<Role> roles;

}

Role.java

package com.scaffoldthis.model;

import java.util.List;

public class Role {

    private String name;

    private List<Permission> permissions;

}

Permission.java

package com.scaffoldthis.model;

public class Permission {

    private String name;

}

This model allows us to add as many Role as we want to user and we can also have the same role assigned to multiple users, as we do not have restriction that same Role object should be assigned to all user with same role. i.e. The identity of an object is not defined by instance but by attributes.

As we have discussed in RDBMS Relationship article, in database world the identity of object is defined by primary key which is always unique and there can only one instance of it in database.

When we try to map these two worlds we have a mismatch which is known as Object Relational Impedence Mismatch, In the next article we would explore how Hibernate helps bridge this mismatch.


Stats:
147 views
Scaffold your ideas into application
Get your free alpha access now.
Only 445 left, Hurry!

Angular Directives Demystified

Directives are the most misunderstood aspect of angular JS. Learn how directives can help you keep your code clean.

Third wave of digital transformation

Third wave of digital transformation

Angularjs Tutorial: Understanding Scope

Scopes are one of the most important and rarely understood concept in angular, in this part we explore how scope works in angularjs.

Comments:

Leave your comments