Object–relational mapping (ORM) is a programming technique that converts data between a relational database and the memory of an object-oriented language, creating a virtual object database. In object-oriented programming, data-management acts on objects that encapsulate scalar values and related entities, like a “Person object” with attributes such as phone numbers. Relational databases like SQL organize data into tuples within tables, differing in aspects like lifecycle management and references. ORM automates translating between these paradigms, enabling objects to be stored persistently in databases while preserving their structure and relationships, supporting features like persistence.
Overview
Implementation-specific details of storage drivers are generally wrapped in an API in the programming language in use, exposing methods to interact with the storage medium in a way which is simpler and more in line with the paradigms of surrounding code.
The following is a simple example, written in C# code, to execute a query written in SQL using a database engine.
var sql = "SELECT id, first_name, last_name, phone, birth_date, sex, age FROM persons WHERE id = 10"; var result = context.Persons.FromSqlRaw(sql).ToList(); var name = result[0]["first_name"];In contrast, the following makes use of an ORM-job API which makes it possible to write code that naturally makes use of the features of the language.
var person = repository.GetPerson(10); var firstName = person.GetFirstName();The case above makes use of an object representing the storage repository and methods of that object. Other frameworks might provide code as static methods, as in the example below, and yet other methods may not implement an object-oriented system at all. Often the choice of paradigm is made for the best fit of the ORM into the surrounding language's design principles.
var person = Person.Get(10);Comparison with traditional data access techniques
Compared to traditional techniques of exchange between an object-oriented language and a relational database, ORM often reduces the amount of code that needs to be written.3
Disadvantages of ORM tools generally stem from the high level of abstraction obscuring what is actually happening in the implementation code.
Object-oriented databases
Another approach is to use an object-oriented database management system (OODBMS) or document-oriented databases such as native XML databases that provide more flexibility in data modeling. OODBMSs are databases designed specifically for working with object-oriented values. Using an OODBMS eliminates the need for converting data to and from its SQL form, as the data is stored in its original object representation and relationships are directly represented, rather than requiring join tables/operations. The equivalent of ORMs for document-oriented databases are called object-document mappers (ODMs).
Document-oriented databases also prevent the user from having to "shred" objects into table rows. Many of these systems also support the XQuery query language to retrieve datasets.
Object-oriented databases tend to be used in complex, niche applications. One of the arguments against using an OODBMS is that it may not be able to execute ad-hoc, application-independent queries. For this reason, many programmers find themselves more at home with an object-SQL mapping system, even though most object-oriented databases are able to process SQL queries to a limited extent. Other OODBMS provide replication to SQL databases, as a means of addressing the need for ad-hoc queries, while preserving well-known query patterns.
Challenges
A variety of difficulties arise when considering how to match an object system to a relational database. These difficulties are referred to as the object–relational impedance mismatch.4
An alternative to implementing ORM is use of the native procedural languages provided with every major database. These can be called from the client using SQL statements. The Data Access Object (DAO) design pattern is used to abstract these statements and offer a lightweight object-oriented interface to the rest of the application.5
ORMs are limited to their predefined functionality, which may not cover all edge cases or database features. They usually mitigate this limitation by providing users with an interface to write raw queries, such as Django ORM.6
See also
- List of object–relational mapping software
- Comparison of object–relational mapping software
- AutoFetch – automatic query tuning
- Common Object Request Broker Architecture (CORBA)
- Object database
- Object persistence
- Object–relational database
- Object–relational impedance mismatch
- Relational model
- SQL (Structured Query Language)
- Java Data Objects (JDO)
- Java Persistence API (JPA), now Jakarta Persistence
- Service Data Objects
- Entity Framework
- Active record pattern
- Data mapper pattern
- Single Table Inheritance
External links
- About ORM by Anders Hejlsberg
- Mapping Objects to Relational Databases: O/R Mapping In Detail by Scott W. Ambler
References
"What is Object/Relational Mapping?". Hibernate Overview. JBOSS Hibernate. Retrieved 27 January 2022. http://www.hibernate.org/about/orm ↩
"What is Object/Relational Mapping?". Hibernate Overview. JBOSS Hibernate. Retrieved 27 January 2022. http://www.hibernate.org/about/orm ↩
Douglas Barry, Torsten Stanienda, "Solving the Java Object Storage Problem," Computer, vol. 31, no. 11, pp. 33-40, Nov. 1998, Excerpt at https://www.service-architecture.com/articles/object-relational-mapping/transparent-persistence-vs-jdbc-call-level-interface.html Lines of code using O/R are only a fraction of those needed for a call-level interface (1:4). For this exercise, 496 lines of code were needed using the ODMG Java Binding compared to 1,923 lines of code using JDBC. https://www.computer.org/csdl/magazine/co/1998/11/ry033/13rRUxC0SRY. ↩
Object–Relational Mapping Revisited - A Quantitative Study on the Impact of Database Technology on O/R Mapping Strategies. M Lorenz, JP Rudolph, G Hesse, M Uflacker, H Plattner. Hawaii International Conference on System Sciences (HICSS), 4877-4886 (DOI:10.24251/hicss.2017.592) https://www.semanticscholar.org/paper/Object-Relational-Mapping-Revisited-A-Quantitative-Lorenz-Rudolph/708ac5e798b7e45b949d42e2f872549a3612e1e2 ↩
Feuerstein, Steven; Bill Pribyl (September 1997). "Oracle PL/SQL Programming". 18.5 Modifying Persistent Objects. Retrieved 23 August 2011.{{cite web}}: CS1 maint: location (link) http://docstore.mik.ua/orelly/oracle/prog2/ch18_05.htm ↩
"Performing raw SQL queries | Django documentation". Django Project. Retrieved 8 September 2024. https://docs.djangoproject.com/en/5.1/topics/db/sql/ ↩