Over the years we have seen a paradigm shift in Programming Languages from the traditional Procedural programming approach to an Object-Oriented approach. However Databases have changed very little in terms of their fundamental principle - Set Theory. Databases are and have been Relational almost from their advent. Most of the Database Management Systems which we use like MySQL, Microsoft SQL Server and Oracle are Relational to a very large extent. Though there have been approaches like Object-Relational DBMS and Hierarchical DBMS, they have rarely been adopted in production environments.
This article focuses on creating a relationship between Object-Oriented Programming Languages and Relational Databases through a concept called Object-Relation Mapping. This article will be followed by two related articles - Using Hibernate in Java with NetBeans and Using ADO.NET Entity Framework in .NET (C#) with Visual Studio.
Before jumping into ORM, here are two common concepts which most of you must be familiar with -
Class - A class is a construct that is used as a blueprint (or template) to create objects of that class. A class defines the properties that each and every object possesses.
Table Schema - A table schema primarily defines the fields and relationships of a table. A table contains records which have the same structure.
Observing closely, we can notice that these two definitions are pretty similar. Both of them talk about a template which multiple instances follow (objects in OO and records in DBMS). Both of them talk about fields (properties) these instances possess.
This similarity is the basis of ORM. Table Schemas of Relational Databases correspond to Classes in an Object Oriented Programming Language and Records of these tables correspond to instances of the Classes (Objects).
Consider an example of a Student table in a Database created with the following schema -
CREATE TABLE Student_tbl
(
StudentId INT PRIMARY KEY,
Name VARCHAR(MAX),
Age INT
)
This table can be translated into a Class with the following structure -
class Student
{
Int32 StudentId;
String Name;
Int32 Age;
}
Each record of the Student_tbl will be an object of the Student class.
There are several free and commercial packages for Object Oriented languages that perform Object Relational Mapping. Most of these packages incorporate advanced features like -
- Automating the class generation process from the Database Schemas
- Maintaining foreign key dependencies using Lists
- Generating identity keys while inserting records and using these keys in subsequent insertions as foreign keys if required
- Creating methods for retrieving data and saving data directly as objects
The major advantages of ORM lie in -
- Minimal database dependency - most of the ORM packages use a concept called 'Dialect' to identify the DBMS the application is connecting to. So changing the dialect when the DBMS is changed is sufficient for the application to run. No application code has to be changed
- ORM reduces the amount of code that needs to be written by a developer
However it is often argued that ORM packages don't perform efficiently during bulk deletions and with joins. So generally it is recommended to check if there a hit in the efficiency of the application when ORM tools are introduced, especially when complex operations are involved.
Though ORM is a simple concept, it's a rapidly over-shadowing the traditional database connectivity models in Object Oriented Programming Languages like Java and C#. In my next post, I will be introducing Hibernate - an ORM package for Java and in the subsequent post I will introduce the ADO.NET Entity Framework - an ORM package for .NET.