Object Relational Mapping

Relevant categories: Software Architecture, Databases

There are many different tools to write your applications and various databases for storing data. But, we mostly use object technologies such as c# and java for developing software and relational databases for storing our data. Using object-oriented techniques on one hand and using relational database on the other, causes us to find a way for mapping these two. ORM (Object Relational mapping) is a programming technique for mapping data from relational database to object programming and vice versa.

In ORM, usually every table in database is mapped to a class in object programming, and every column of the table is a property of the class. Some of these properties could be an object themselves. Also we might have properties that are only for temporary usage.

For example, assume that for an Employee object its hours of working in every day of a month are saved in a table. Then the averageHoursInMonth property is a temporary property that is not saved in our database, but we could calculate it through our application. Also, the Employee object has an Address object as its property. Therefore, we would have another class for Address which we should map its relation to Employee. This Address class itself can also have properties that need mapping to its persistent relations.

Some programmers prefer to make their own ORM tools. But, recently there have been many tools that can help us through this mapping. For instance, Hibernate is an ORM tool which is vastly used in Java. NHibernate is the open-source port of Hibernate used for Microsoft .NET.

In general most ORM tools have the following characteristics. They:

  • Are able to model inheritance and polymorphism between objects.
  • Deal with different types of relations. (ex.1-1 or n-n )
  • Could use Grouping like GROUP BY in SQL.
  • Support different types of database and could easily substitute different types of RDBMS.
  • Imply no additional strict ways in design of the classes.


Links: