Sunday, June 14, 2015

Object Oriented vs Relational Model relationship

Object Oriented world abstracts data in form of objects (and provides method to read/manipulate) whereas Relational world abstracts data in form of (table) records. Both are quite different approaches and hence bringing them together is not so trivial. There have been different approaches but will discuss here from the perspective of object relational mapping. 

Entity is a well designed middle ground between two orthogonal concepts, object and table. To understand entity relationship, it's better to understand how objects relate with other object and similarly how tables relate with other tables. Let's dive deeper into both-

Object Relationship

In object oriented world, different classes associate among each other. This enables object of one type to perform some action on object of other type(i.e. obj.performAction(...)). Object Oriented uses UML to show the relationship and one of the most fundamental relationship is association which gets realized using object references. Association relationship between two classes has a direction as well as ownership. 

Unidirectional association between two classes (UML, class diagram)
Unidirectional relationship also has owner, in above diagram Employee class is owner of the relationship. It also shows multiplicity(or cardinality), which says that 1 instance of Employee class can have 0 or more instances of Address class. In Java, above relationship will be implemented by having attribute of Address class inside Employee. 

public class Employee{
      private String id;
      private List<Address> addresses = new ArrayList<>();

List of Address class enables you to have 0 or more address for a given employee. Java also supports bidirectional association which gets shown in UML diagram either by showing no arrow (just a plain line without any head) or arrow at both ends. Association is most fundamental relationship, and usually association relationship gets evolved into other relationship like(Generalization, Aggregation/Composition etc). For the scope of this post, I have considered only association relationship.

Relationship in relational DB

Relational world has tables which is collection of relations (among its columns). Tables don't have list/map or even concept of attributes to refer one table in another table. The reference here is modeled bit differently and uses below two approaches:

  1. By using foreign key (i.e. a join column)
  2. By using a join table
So in relational world, it would get implemented by having an EMPLOYEE table pointing to ADDRESS table either through a foreign key or through a join table as shown below:

Table relationship (foreign key vs join table)

So in first case, ADDRESS_ID is foreign key in EMPLOYEE table; but in second case (i.e. join table),  EMPLOYEE table doesn't store any reference for ADDRESS. In fact the relationship is stored in a separate table which just maps the primary keys of both the tables. 
Join tables are not ideal for one-to-one relationships. You should use them when you have one-to- many or many-to-many relationships. 

Final Note

Object references are inherently directional. The association is always from one object to the other. They're REFERENCES ( or POINTERS as called in C/C++). On the other hand, foreign-key association aren't directional by nature. Navigation has no meaning in relational world. 
Java associations can have many-to-many multiplicity, whereas table associations are always one-to-many or one-to-one. 


  1. Thanks for sharing nice information with us. i like your post and all you share with us is uptodate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job.