Thursday, June 12, 2014

Object Relation Mapping

By definition from Wikipedia “Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages." In other words data or information represented in a non-object oriented fashion would need some intermediate mapping to be converted into object oriented representation.

Relational Database Management Systems (RDBMS) represents data in a tabular fashion where data is maintained across tables and views.  Relationships amongst tables aid in maintaining meaningful data most of the time. RDBMS are very viable and used most widely in the industry to persist and maintain information. Although there are other alternatives like Graph Databases and Big Data systems, RDBMS is likely to stay around for a few more years.   

In an Object Oriented Language like Java, information is represented by graphs of objects which are interconnected via key attributes representing a meaningful object graph. This Object Graph makes sense only when it load or stores meaningful information from some persistent source and in majority of cases this would be a RDBMS.

Object Relation Mapping addresses this Object-relational impedance mismatch by providing a mechanism to map information stored in a Relational Database Management System to an Objects which are used in programs created using Object Oriented languages like Java. 
  
There are some key terminologies used in Object Relational Mapping which are pretty straight forward:

Property - This is the data attribute that needs to be persisted or loaded from the Relational Database.

Example: Employee object could have attributes like empId, empFirstName, empLastName; all these data attributes constitute properties which are targets for object relational mapping.

Property Mapping - This is used to describe the mapping of the Object attribute to Relational Database Table and Column Name.

Example: Employee object could have attributes like empId which is mapped to the employee_id columns in the employee table found in the employee_app schema.

Relationship mapping - This is used to describes relationships between data (association, aggregation, or composition) between two or more objects or tables.

Example: Each employee belongs to single department, so there exists a relationship between employee and department via composition where employee may contain department information, an employee can be associated to multiple projects so there could be a an aggregation of project associations for the employee.

There are three types of relationships that can be applied to RDBMS and Objects namely:

One to One Relationship: In a One to One Relationship each row in one database table is associated to one and only one row in another database table. In terms of Objects the maximum multiplicity for each object in One to One Relationship is one.

Example: Employee can belong to only one Department; in the RDBMS each employee record in the employee table will have association to one and only one department in the department table generally enforced via foreign keys. In Object Relationship each employee will be associated to a department object via composition i.e. employee object contains composed department object.

One to Many Relationship (Many to One Relationship): In a One to Many Relationship each row in one database table is associated to one or more rows in another database table.  In terms of objects in One to Many Relationship, one object will have maximum multiplicity as one and the other one or more.

Example: One Department can have many Employees but one Employee can belong to only one Department; in the RDBMS each department record in the department table will have association one or more employees in the employee table. In Object Relationship each department will be associated to an employee aggregation i.e. department object contains an employee aggregation (collection).

Many to Many Relationship: In a Many to Many Relationship one or more rows in one database table is associated to zero or more rows in another database table.  Such relationships are generally expressed via intermediate association tables. In terms of objects in Many to Many Relationship, both objects will have maximum multiplicity as zero or more.

Example: One employee can have zero or more Projects, each Project can have zero or more employees; in the RDBMS each a Project Employee Association table could maintain employee/project associations. In Object Relationship each employee will be associated to a project aggregation i.e. employee object contains project aggregation (collection).

We will look at following technologies/frameworks that implement Object Relational Mapping looking at the strength and weakness of using each of those technologies.  

MyBatisMyBatis is the successor to the now retired IBatis Apache project, not truly considered a true Object Relational Mapping framework but more of a data mapper framework that makes it easier mapping RDBMS constructs to Objects.

HibernateHibernate is a pure Object Relational Mapping which has gained lot of traction over the years.We will use pure Hibernate O/RM implementation using SessionFactory and Criteria API.

Hibernate JPAHibernate also provides JPA implementation which is the de-facto standard of interfacing O/RM mapping.   

SpringData JPASpring Data provides a nice wrapper to interface with data access technologies. We will look at Spring Data JPA which will use Hibernate JPA implementation.

JOOQJOOQ does not falls into an O/RM category but promises to provide a complete abstraction of JDBC and all database interaction.


Entity Model

Let us first start by putting together an entity model for our RDBMS which will be MySQL for our evaluations. 

The following is the entity model. 


The model looks pretty straight forward but what’s even better is that it was generated using MySQL Workbench, which is completely free no strings attached. For those who used to think that ok MySQL is one of the good old free databases with good features and limited tool support think again, I think MySQL Workbench is a great product and even offers reverse engineering options to generate ER diagrams and model. 

Workbench Home UI:




Workbench Model UI:



Workbench ER Diagram Editor UI:

Workbench has many useful features to aid in database development work, more details are available on the MySQL workbench site.

The MySQL Workbench model file for the employee app RDBMS is available in github at the following location https://github.com/MalcolmPereira/com.malcolm.daotest/blob/master/employe_app_mySQL_Model.mwb.

For those who do not want to dive into understanding MySQL Workbench but rather stick to plain old sql scripts, mysql script file to create sample database is available at:
https://github.com/MalcolmPereira/com.malcolm.daotest/blob/master/src/main/scripts/employee_app.sql

Object Model

We wil use the following Object Model in the Object Relational Mapping:



The object model is pretty simple, Employee belongs to one department, has one designation and one manager,  Employee can play different roles and be part of zero or more projects. Department will have a manager which is an Employee and each project will have a project manager which is again an employee, this will give us a pretty neat object graph mapped from the relational model.

We will look at the MyBatis implementation in the next post.

Until next time,
Malcolm

Back to Java Thoughts

No comments:

Post a Comment