Object–relational mapping

Short description: Programming technique

Not to be confused with Object–role modeling.

Object–relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is a programming technique for converting data between a relational database and the memory (usually the heap) of an object-oriented programming language. This creates, in effect, a virtual object database that can be used from within the program.

In object-oriented programming, data-management tasks act on objects that combine scalar values into objects. For example, consider an address book entry that represents a single person along with zero or more phone numbers and zero or more addresses. This could be modeled in an object-oriented implementation by a "Person object" with an attribute/field to hold each data item that the entry comprises: the person's name, a list of phone numbers, and a list of addresses. The list of phone numbers would itself contain "PhoneNumber objects" and so on. Each such address-book entry is treated as a single object by the programming language (it can be referenced by a single variable containing a pointer to the object, for instance). Various methods can be associated with the object, such as methods to return the preferred phone number, the home address, and so on.

By contrast, relational databases, such as SQL, group scalars into tuples, which are then enumerated in tables. Tuples and objects have some general similarity, in that they are both ways to collect values into named fields such that the whole collection can be manipulated as a single compound entity. They have many differences, though, in particular: lifecycle management (row insertion and deletion, versus garbage collection or reference counting), references to other entities (object references, versus foreign key references), and inheritance (non-existent in relational databases). As well, objects are managed on-heap and are under full control of a single process, while database tuples are shared and must incorporate locking, merging, and retry. Object–relational mapping provides automated support for mapping tuples to objects and back, while accounting for all of these differences.[1]

The heart of the problem involves translating the logical representation of the objects into an atomized form that is capable of being stored in the database while preserving the properties of the objects and their relationships so that they can be reloaded as objects when needed. If this storage and retrieval functionality is implemented, the objects are said to be persistent.[1]

## Contents

* 1 Overview
* 2 Comparison with traditional data access techniques
* 3 Object-oriented databases
* 4 Challenges
* 5 See also
* 6 References
* 7 External links

## 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.

using System.Collections.Generic;

string sql = "SELECT id, first_name, last_name, phone, birth_date, sex, age FROM persons WHERE id = 10";
List<Person> result = context.Persons.FromSqlRaw(sql).ToList();
string 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.

Person person = repository.GetPerson(10);
string 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.

Person 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.[2]

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.{{Citation needed|date=September 2018}

## 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.[3]

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.[4]

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.[5]

## 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

## References

1. ↑ 1.0 1.1 "What is Object/Relational Mapping?" (in en-US). Hibernate Overview. JBOSS Hibernate. http://www.hibernate.org/about/orm.
2. ↑ "Solving the Java object storage problem". Computer (Institute of Electrical and Electronics Engineers (IEEE)) 31 (11): 33–40. 1998. doi:10.1109/2.730734. https://www.service-architecture.com/articles/object-relational-mapping/transparent-persistence-vs-jdbc-call-level-interface.html. "For this exercise, 496 lines of code were needed using the ODMG Java Binding compared to 1,923 lines of code using JDBC.".
3. ↑ 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)
4. ↑ Feuerstein, Steven; Bill Pribyl (September 1997). "Oracle PL/SQL Programming" (in en-US). 18.5 Modifying Persistent Objects. http://docstore.mik.ua/orelly/oracle/prog2/ch18_05.htm.
5. ↑ "Performing raw SQL queries | Django documentation" (in en). https://docs.djangoproject.com/en/5.1/topics/db/sql/.

## External links

* About ORM by Anders Hejlsberg
* Mapping Objects to Relational Databases: O/R Mapping In Detail by Scott W. Ambler

* v
* t
* e

Database

Main|

* Requirements
* Theory
* Models
* Database management system
* Machine
* Server
* Application
* Connection
* datasource
* DSN
* Administrator
* Lock
* Types
* Tools

Languages|

* Data definition
* Data manipulation
* Query
* information retrieval

Security|

* Activity monitoring
* Audit
* Forensics
* Negative database

Design|

* Entities and relationships (and Enhanced notation)
* Normalization
* Refactoring
* Cardinality

Programming|

* Abstraction layer
* Object-relational mapping

Management|

* Virtualization
* Tuning
* caching
* Migration
* Preservation
* Integrity

See also|

* Database-centric architecture
* Intelligent database
* Two-phase locking
* Locks with ordered sharing
* Load file
* Publishing
* Halloween Problem
* Log shipping

* Book
* Category
* WikiProject

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Object–relational mapping. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Object–relational_mapping&oldid=4155303"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template