Published in · 2 min read · Jul 21, 2020
--
In this post, I will try to explain the concept of ORM which allows us to access a database, do manipulations on the data and querying over the data using object oriented programming languages.
ORM technique works on objects and it depends on the object oriented programming languages. In principle, ORM is the mapping between tables in the relational database and the objects that we have created in our application.
ORM adds an extra data layer between the application and the database. This layer is written generally as a framework for the object oriented programming language that we are using.
Why should we use ORM?
- ORM is a mapping technique and it is independent of the database system. Its specification is abstracted so that it can be implemented by any program that fulfills the requirements. By using ORM, the migration between databases can be done without any effort in the future, if necessary.
- Developers do not need to learn any query language in order to do data manipulation with ORM. (Since databases are a part of almost every application, I think every developer should at least be familiar with query languages)
- Developers can focus on the application instead of writing repeating CRUD (Create, Read, Update and Delete) operations. Because the CRUD operations are handled by ORM, the readability of the code is increased and it saves time.
- Since the object-mapping is handled by ORM, there will be less interaction with code which makes our application to have less bugs.
- ORM enables us to do data manipulation with the code that we are developing our application. (e.g. Java)
- ORM allows us to take advantage of the possibilities offered by object oriented programming. For example, the concept of inheritance can be mapped in the database effortlessly.
- ORM supports custom queries.
- There are many implementations of ORM for each object oriented programming language in the market. Each developer can choose the suitable implementation for their application requirements.
Drawbacks of ORM
- It may take some time to lean the theory and the technique of ORM. ORM libraries are quite complex.
- ORM tools need to be configured and integrated to the application. (This integration progress may be handled efficiently by using another framework. For example, Spring Framework, especially Spring Boot and Spring Data)
- If the application requires high efficiency or if it has complex queries performance might be a problem.
Some Popular ORM Tools
- Hibernate: Java
- JOOQ ORM: Java
- NHibernate: .NET Framework
- Django: Python
- Doctrine: PHP
I think using ORM in an application depends on the project requirements. Because of the learning curve and the complexity added to the project, ORM might be an overkill for simple projects. Also, if not optimized, it may perform poorly in a project which contains complex queries or requires high efficiency.