object-relational mapping (ORM)
What is object-relational mapping (ORM)?
Object-relational mapping (ORM) is a way to align programming code with database structures. ORM uses metadata descriptors to create a layer between the programming language and a relational database. It thus connects object-oriented program (OOP) code with the database and simplifies the interaction between relational databases and OOP languages.
The idea of ORM is based on abstraction. The ORM mechanism makes it possible to address, access and manipulate objects without having to consider how those objects relate to their data sources. ORM lets programmers maintain a consistent view of objects over time, even as the sources that deliver them, the sinks that receive them and the applications that access them change.
Developers can also perform various data creating, reading, updating and deleting (CRUD) operations in relational databases without using SQL. This capability is particularly useful for developers who either don't know SQL or don't want to waste time writing SQL code. With ORM, they don't have to understand and write SQL or rely on SQL query builders to add an abstraction layer to the SQL code.
How object-relational mapping works
ORM translates information about states and codes object-oriented programs create that are often difficult to understand. It creates a structured map that explains how objects are related to different tables (data) without knowing how the data is structured. In other words, it creates a logical model of the program with a high level of abstraction, i.e., without specifying the underlying code details.
This information helps developers understand the underlying database structure. When the application makes changes to the data object, the relational database will insert, update, create or delete data in response to these changes. This happens because the ORM converts data between tables and generates the SQL code needed by the database to respond to application changes and to manage data activities.
ORM manages the mapping details between a set of objects and underlying relational databases, XML repositories or other data sources and sinks. It simultaneously hides the often changing details of related interfaces from developers and the code they create. In many cases, ORM changes can incorporate new technology and capabilities without requiring changes to the code for related applications.
Advantages and benefits of object-relational mapping
ORM hides and encapsulates changes in the data source, so that when data sources or their APIs change, only ORM needs to change to preserve the appropriate associations -- not the applications that use ORM. This capacity makes it easier to maintain applications, lets developers take advantage of new classes as they become available and makes it easy to extend ORM-based applications.
The program model can be used to connect the application with the SQL code without rewriting the code, saving a lot of time for developers. Because the ORM will manage the application's data needs, developers don't have to write any additional low-level code, which can help to increase productivity and speed up development time.
ORM also makes it easier and more cost-effective to maintain the application over time because it automates object-to-table and table-to-object conversion and requires less code compared to embedded SQL and handwritten stored procedures. In many cases, it can improve the application's overall design. Finally, ORM improves system performance by enabling transparent object caching in the application tier.
Disadvantages of object-relational mapping
Critics have said that ORM can lead to an erosion in application speed and performance due to the extra code that is generated for abstraction. They believe that using stored procedures is a better way to avoid this problem. In some cases, depending on ORM might result in poorly designed databases and make it harder to maintain applications.
Incorrect mapping between data tables and objects can also create application problems that may be difficult to recognize and can affect overall performance. Finally, if the ORM layer is poorly written, it can become difficult to improve data schemas and to manage database migrations.
ORM vs. SQL
Native querying with SQL has both pros and cons. SQL queries are more flexible and detailed than ORM abstractions. SQL also enables programmers to better control their data interface. On the other hand, they are also responsible for maintaining the security of the database code, which can be difficult to do. If there are vulnerabilities in the code, the application may be at risk of SQL injection attacks. The likelihood of such attacks can be lower with ORM, because ORM tools help to "sanitize" and secure the code.
SQL query builders add a layer of abstraction over the raw SQL without masking the underlying details and simplify application integration. They include template builders so developers can understand the database structure. However, they still need to know SQL to understand the database structure.
Popular object-relational mapping tools
An ORM tool is software designed to help OOP developers interact with relational databases in a simpler manner. These tools use one of two strategies:
- Active record pattern. The tool maps data within the structure of objects in the programming code and manages the data using classes and structures within the code.
- Data mapper pattern. This type of tool separates the business logic (in the objects) from the underlying database, allowing reuse of the same programming logic while simplifying database changes.
The best ORM tools create high-quality code without affecting application performance, support and database migrations, and provide options to improve data schemas. They also speed up development, decrease development costs and improve application security to minimize the chances of SQL injections and other types of attacks.
A variety of ORM tools are available for different OOP languages, including PHP, Java and Python.
Tool name | OOP language | Key features/USPs |
Laravel |
PHP |
|
CakePHP |
PHP |
|
RedBeanPHP |
PHP |
|
Django |
Python |
|
Web2py |
Python |
|
SQLObject |
Python |
|
SQLAlchemy |
Python |
|
Hibernate |
Java |
|
Apache OpenJPA |
Java |
|
EclipseLink |
Java |
|
JOOQ (object oriented querying) |
Java |
|
Entity Framework |
|
|
NHibernate |
.NET |
|
Dapper |
.NET |
|
Base One Foundation Component Library (BFC) |
.NET |
|