Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Object–relational mapping
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Programming technique}} {{Distinguish|Object–role modeling}} {{Use dmy dates|date=June 2019}} {{More citations needed|date=May 2009}} '''Object–relational mapping''' ('''ORM''', '''O/RM''', and '''O/R mapping tool''') in [[computer science]] is a [[Computer programming|programming]] technique for converting data between a [[relational database]] and the memory (usually the [[Memory management#HEAP|heap]]) of an [[object-oriented]] programming language. This creates, in effect, a virtual [[object database]] that can be used from within the programming language. In [[object-oriented programming]], [[data management|data-management]] tasks act on [[object (computer science)|object]]s that combine [[scalar (computing)|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 (computer science)|object]]" with an [[attribute (computing)|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 [[Method (computer programming)|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 [[Table (database)|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 (computer science)|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.<ref name="hibernate-orm-overview"> {{cite web |title=What is Object/Relational Mapping? |url=http://www.hibernate.org/about/orm |access-date=27 January 2022 |work=Hibernate Overview |publisher=JBOSS Hibernate |language=en-US}} </ref> 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 [[persistence (computer science)|persistent]].<ref name=hibernate-orm-overview /> ==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 Sharp (programming language)|C#]] code, to execute a query written in [[SQL]] using a database engine. <syntaxhighlight lang="csharp"> 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"]; </syntaxhighlight> 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. <syntaxhighlight lang="csharp"> var person = repository.GetPerson(10); var firstName = person.GetFirstName(); </syntaxhighlight> 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. <syntaxhighlight lang="csharp"> var person = Person.Get(10); </syntaxhighlight> ==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.<ref>Douglas Barry, Torsten Stanienda, "Solving the Java Object Storage Problem," Computer, vol. 31, no. 11, pp. 33-40, Nov. 1998, [https://www.computer.org/csdl/magazine/co/1998/11/ry033/13rRUxC0SRY. 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.'']</ref> Disadvantages of ORM tools generally stem from the high level of [[Database abstraction layer|abstraction]] obscuring what is actually happening in the implementation code. ==Object-oriented databases== Another approach is to use an [[object database|object-oriented database management system]] (OODBMS) or [[document-oriented database]]s such as native [[XML database]]s 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 [[junction table|join tables]]/operations. The equivalent of ORMs for [[document-oriented database]]s 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.{{Citation needed|date=September 2013}} 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]].<ref>[https://www.semanticscholar.org/paper/Object-Relational-Mapping-Revisited-A-Quantitative-Lorenz-Rudolph/708ac5e798b7e45b949d42e2f872549a3612e1e2 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)</ref> 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|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.<ref>{{cite web |last=Feuerstein |first=Steven |author2=Bill Pribyl |date=September 1997 |title=Oracle PL/SQL Programming |url=http://docstore.mik.ua/orelly/oracle/prog2/ch18_05.htm |access-date=23 August 2011 |language=en-US |location=18.5 Modifying Persistent Objects}}</ref> 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.<ref>{{Cite web |title=Performing raw SQL queries {{!}} Django documentation |url=https://docs.djangoproject.com/en/5.1/topics/db/sql/ |access-date=2024-09-08 |website=Django Project |language=en}}</ref> == 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== {{Reflist}} ==External links== *[http://www.artima.com/intv/abstract3.html About ORM] by [[Anders Hejlsberg]] *[http://www.agiledata.org/essays/mappingObjects.html Mapping Objects to Relational Databases: O/R Mapping In Detail] by [[Scott W. Ambler]] {{Database}} {{DEFAULTSORT:Object-Relational Mapping}} [[Category:Object–relational mapping| ]] [[Category:Data mapping]] [[Category:Articles with example C Sharp code]]
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Citation needed
(
edit
)
Template:Cite web
(
edit
)
Template:Database
(
edit
)
Template:Distinguish
(
edit
)
Template:More citations needed
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Use dmy dates
(
edit
)
Search
Search
Editing
Object–relational mapping
Add topic