Object–relational mapping: Difference between revisions

Content deleted Content added
Tag: Reverted
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist.
 
(43 intermediate revisions by 34 users not shown)
Line 1:
{{Distinguish|Object-role modeling}}
{{Use dmy dates|date=June 2019}}
{{Refimprove|date=May 2009}}
{{short description|Programming technique}}
{{Distinguish|Object-roleObject–role modeling}}
'''Object–relational mapping''' ('''ORM''', '''O/RM''', and '''O/R mapping tool''') in [[computer science]] is a [[Computer programming|programming]] technique for converting data between incompatible [[type system]]s using [[object-oriented]] programming languages. This creates, in effect, a "virtual [[object database]]" that can be used from within the programming language. There are both free and commercial packages available that perform object–relational mapping, although some programmers opt to construct their own ORM tools.
{{Use dmy dates|date=June 2019}}
{{RefimproveMore 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 incompatiblea [[typerelational systemdatabase]]s usingand the memory (usually the [[Memory management#HEAP|heap]]) of an [[Object-oriented programming|object-oriented]] programming languageslanguage. This creates, in effect, a "virtual [[object database]]" that can be used from within the programming language. There are both free and commercial packages available that perform object–relational mapping, although some programmers opt to construct their own ORM tools.
 
In [[object-oriented programming]], [[data management|data-management]] tasks act on [[object (computer science)|object]]s that arecombine almost always non-[[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">
By contrast, many popular database products such as [[SQL]] [[database management system]]s (DBMS) are not object-oriented and can only store and manipulate [[Scalar (computing)|scalar]] values such as integers and strings organized within [[Table (database)|tables]]. The programmer must either convert the object values into groups of simpler values for storage in the database (and convert them back upon retrieval), or only use simple scalar values within the program. Object–relational mapping implements the first approach.<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}}
{{cite web
|title= What is Object/Relational Mapping?
|url= http://www.hibernate.org/about/orm
|work= Hibernate Overview
|publisher= JBOSS Hibernate
|access-date= 27 January 2022
}}
</ref>
 
Line 30 ⟶ 24:
</syntaxhighlight>
 
In contrast, the following makes use of an ORM-job API, allowingwhich themakes writingit ofpossible to write code whichthat naturally makes use of the features of the language.
 
<syntaxhighlight lang="csharp">
Line 37 ⟶ 31:
</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 tofor the best fit of the ORM best into the surrounding language's design principles.
 
<syntaxhighlight lang="csharp">
var person = Person.Get(10);
</syntaxhighlight>
 
Usually, the framework will expose some filtering and querying functionality, allowing subsets of the storage base to be accessed and modified. The code below queries for people in the database whose ID value is '10'.
 
<syntaxhighlight lang="csharp">
var person = Person.Get(Person.Properties.Id == 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{{cite Barry,journal Torsten| Stanienda, "Solving the Java Object Storage Problem," Computer, vol. 31, no. 11, pp. 33-40, Nov.date= 1998, [http://www2.computer.org/portal/web/csdl/doi/10.1109/2.730734| http://www2.computer.org/portal/web/csdl/doi/=10.1109/2.730734 ],| Excerptissue=11 at| http://www.service-architecture.com/object-relational-mapping/articles/transparent_persistence_vs_jdbc_call-level_interface.html.journal=Computer Lines| ofpages=33–40 code| usingpublisher=Institute O/Rof areElectrical onlyand aElectronics fractionEngineers of(IEEE) those| neededtitle=Solving forthe aJava call-levelobject interfacestorage (1:4).problem ''|quote=For this exercise, 496 lines of code were needed using the ODMG Java Binding compared to 1,923 lines of code using JDBC.'' | url=https://www.service-architecture.com/articles/object-relational-mapping/transparent-persistence-vs-jdbc-call-level-interface.html | vauthors=((Barry, D.)), ((Stanienda, T.)) | volume=31 }}</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. Also, heavy reliance on ORM software has been cited as a major factor in producing poorly designed databases.<ref>Josh Berkus, "Wrecking Your Database", Computer, Aug. 2009, https://www.toolbox.com/tech/data-management/blogs/wrecking-your-database-080509/</ref>
 
==Object-oriented databases==
Line 64 ⟶ 52:
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 |author2language=Bill Pribyl en-US |___location=18.5 Modifying Persistent Objects |date=September 1997}}</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]]
Line 77 ⟶ 67:
*[[Relational model]]
**[[SQL]] (Structured Query Language)
*[[Java Data Objects]] (JDO)
*[[Java Persistence API]] (JPA), now [[Jakarta Persistence]]
*[[Service Data Objects]]
*[[Entity Framework]]
Line 94 ⟶ 85:
 
{{DEFAULTSORT:Object-Relational Mapping}}
[[Category:Object-relationalObject–relational mapping| ]]
[[Category:Data mapping]]
[[Category:Articles with example C Sharp code]] saikumar