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,
This table can be translated into a Class with the following structure –
Each record of the Student_tbl will be an object of the Student class.
- 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
- 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
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.