Object-relational mapping

from Wikipedia, the free encyclopedia

Object-relational mapping ( English object-relational mapping , ORM ) is a technique of software development , with the one in a object-oriented programming language written application program its objects in a relational database can store. The database then appears to the program as an object-oriented database , which makes programming easier. This technique is usually implemented with class libraries such as Entity Framework for .NET programming languages, Hibernate for the Java programming language , Doctrine for PHP , SQLAlchemy for Python , Active Record for Ruby or Diesel for Rust . There is also a standardized interface for Java , the Java Persistence API .

principle

Object-oriented programming languages ​​(OOP) encapsulate data and behavior in objects, whereas relational databases store data in tables. The two paradigms are fundamentally different. Objects encapsulate their state and behavior behind an interface and have a unique identity. Relational databases, on the other hand, are based on the mathematical concept of relational algebra . This conceptual contradiction became known as the object-relational impedance mismatch in the 1990s .

In order to resolve the contradiction or at least to mitigate it, various solutions have been proposed, for example object-oriented databases or the extension of programming languages ​​to include relational concepts (e.g. embedded SQL ). The direct object-relational mapping of objects on relations has the advantage that, on the one hand, the programming language itself does not have to be expanded and, on the other hand, relational databases are an established technology that is available in all environments as mature software . The disadvantage of this approach, which complies with the OOP paradigm, is that the strengths and capabilities of relational databases are sometimes not used, which can result in less than optimal performance.

Basic techniques

In the simplest case, classes are mapped to tables , each object corresponds to a table row and a table column is reserved for each attribute . The identity of an object corresponds to the table's primary key . If an object has a reference to another object, this can be represented in the database with a foreign key-primary key relationship.

The term shadow information describes additional data that an object needs in order to be stored persistently . This includes primary keys - especially if they are surrogate keys with no technical significance - as well as auxiliary data for access control, such as time stamps .

Mapping of inheritance hierarchies

Table per inheritance hierarchy
Table per subclass
Table per concrete class

There are essentially three different methods of mapping inheritance hierarchies on database tables. Some frameworks offer further variations and blends of these three basic methods.

Table per inheritance hierarchy
(Also single table , single table ) With this procedure all attributes of the base class and all derived classes are saved in a common table. In addition, a so-called “discriminator” is stored in a further column, which defines which class the object saved in this line belongs to. In most cases, however, attributes of derived classes cannot be provided with a NOT-NULL constraint with this approach . In addition, restrictions on the number of columns allowed per table can thwart this approach in the case of large classes or class hierarchies.
Table per subclass
(also Joined or Class Table ) With this procedure, a table is created for the base class and a further table for each subclass derived from it. A discriminator is not required because the class of an object is determined by a 1-to-1 relationship between the entry in the table of the base class and an entry in one of the tables of the derived classes.
Table per concrete class
(also table per class or concrete table ) Here the attributes of the abstract base class are included in the tables for the concrete subclasses. The table for the base class is omitted. The disadvantage of this approach is that it is not possible to find instances of different classes with one query.

Another method is the mapping of structures (relationships, inheritance) and data in general tables General Tables . The entire database contains exactly 5 tables: One for classes, one for relationships (including inheritance relationships), one for attributes, one for instances (of the classes) and one for values ​​(of the attributes). However, this procedure is of little importance in practice.

literature

Individual evidence

  1. http://blogs.tedneward.com/2006/06/26/The+Vietnam+Of+Computer+Science.aspx ( Memento from January 22, 2018 in the Internet Archive )
  2. ^ Scott W. Ambler: Agile Database Techniques . S. 228-229 .
  3. Chapter 10. Inheritance Mapping. In: Hibernate Reference Documentation. Red Hat Middleware, LLC, 2012, accessed July 31, 2012 .
  4. ^ A b c Martin Fowler: Patterns of Enterprise Application Architecture . Addison-Wesley-Longman, Amsterdam 2002, ISBN 0-321-12742-0 .
  5. Map Classes To A Generic Table Structure