Lecture
Object-relational impedance mismatch is a set of conceptual and technical difficulties that are often encountered when a relational database management system (RDBMS) is used by a program written in an object-oriented style, especially when objects or class definitions are mapped to a database table or relational schema.
Object-relational impedance mismatch is a set of conceptual and technical difficulties that are often encountered when a relational database management system (RDBMS) is currently being served by (several application programs or) application programs written in an object-oriented programming language or style, particularly because objects or class definitions must be mapped to database tables defined by a relational schema.
The term object-relational impedance mismatch comes from the electrical engineering term impedance matching.
(object-relational impedance mismatch).
Object-oriented programs are designed with representation-hiding techniques in mind. Within the object-oriented style, the relevant properties of a given object should not be exposed through any interface. Nevertheless, object-relational mapping necessarily exposes the object's core content to interaction with an interface that the object's implementation cannot specify. Thus, object-relational mapping violates the object's encapsulation.
In relational thinking, "private" versus "public" access is a relative rather than absolute characteristic of a data state, unlike in the object-oriented model. Relational and OO models often conflict over the relativity versus absolutism of classifications and characteristics.
In the object-oriented paradigm, objects have interfaces that together provide the only access to the internal parts of the object. The relational model, on the other hand, makes use of derived relation variables to provide different perspectives and constraints to ensure integrity. Likewise, important OOP concepts for object classes, such as inheritance and polymorphism, are not supported by relational databases.
A correct mapping between relational and object-oriented concepts can be achieved if the relational database tables are linked to the associations found in object-oriented analysis.
An important mismatch between existing relational and OO languages is the difference in type systems. The relational model strictly forbids by-reference attributes (or pointers), while OO languages expect pass-by-reference behavior. Scalar types and their operator semantics can differ significantly between models, causing problems in mapping. For example, most SQL systems support string types with different collation types and maximum-length constraints (open-ended text types tend to reduce performance), while most OO languages treat collation only as an argument to a sorting procedure, with string size inherently tied to available memory. A subtler but related example: SQL systems often ignore trailing spaces in strings for comparison purposes, whereas OO string libraries generally do not.
Objects (instances) reference one another and therefore form a graph in the mathematical sense (a network including loops and cycles). Relational schemas, by contrast, are tabular and based on relational algebra, which defines related heterogeneous tuples (grouping data fields into a «row» with different types for each field).
Object-oriented programs are well designed using techniques that allow the creation of encapsulated objects whose internal representation can be hidden. Within an object-oriented structure, the underlying properties of a given object are expected not to be exposed to any interface other than the one implemented together with the object. However, most object-relational mappings expose the object's underlying content to interaction with an interface that the object's implementation cannot specify. Consequently, this object-relational mapping violates the object's encapsulation, since many object-relational mappings automatically generate public fields corresponding to database columns. Some frameworks instead use metaprogramming techniques, which avoid violating encapsulation.
In relational thinking, «private» access versus «public» access depends on need. In the object-oriented (OO) model, this is an absolute characteristic of a data state. Relational and object-oriented models often conflict over the relativity versus absolutism of classifications and characteristics.
In the object-oriented paradigm, objects have interfaces that together provide the sole access to the internals of that object. The relational model, on the other hand, uses derived relation variables (views) to provide different points of view and constraints to ensure integrity. Likewise, the core OOP concepts of object classes, inheritance, and polymorphism are not supported by relational database systems.
A correct mapping between relational and object-oriented concepts can be achieved if relational database tables are linked together to reflect the associations found in object-oriented analysis.
The main mismatch between existing relational and object-oriented languages is the difference in type systems. The relational model strictly forbids the use of by-reference attributes (or pointers), whereas object-oriented languages accept and expect by-reference behavior. Scalar types and their operator semantics can differ greatly across models, causing problems when mapping.
For example, most SQL systems support string types with various collations and bounded maximum lengths (open-ended text types tend to reduce performance), while most object-oriented programming languages treat collation only as an argument to sorting procedures, with string size inherently tied to available memory. A subtler but related example: SQL systems often ignore trailing spaces in a string for comparison purposes, whereas OO string libraries do not. As a rule, it is not possible to create new data types by restricting the possible values of other primitive types in an object-oriented language.
Another mismatch relates to differences in the structural and integrity aspects of the contrasting models. In object-oriented languages, objects can consist of other objects - often to a high degree - or depend on a more general definition. This can make mapping to relational schemas less straightforward. This is because relational data is typically represented in a named set of global, non-nested relation variables. The relations themselves, being sets of tuples all conforming to a single header (see relational tuple calculus), have no ideal counterpart in object-oriented languages. Constraints in object-oriented languages are usually not declared as such but appear as exception-guarding logic surrounding code that operates on encapsulated internal data. The relational model, on the other hand, requires declarative constraints for scalar types, attributes, relation variables, and the database as a whole.
However, semantic differences are especially evident in the manipulative aspects of the contrasting models. The relational model has an internal, relatively small, and well-defined set of primitive operators for use in queries and data manipulation, whereas object-oriented programming languages usually handle queries and manipulation through custom or low-level, register- and pointer-based, path-dependent imperative operations. Some object-oriented languages support declarative query sublanguages, but since object-oriented languages typically deal with lists and possibly hash tables, the manipulative primitives are necessarily different from the set-based operations of the relational model.
Concurrency and transaction aspects also differ substantially. In particular, transactions - the smallest unit of work performed by databases - in relational databases are much larger than any operations performed by classes in object-oriented languages. Transactions in relational databases represent dynamically bounded sets of arbitrary data manipulations, whereas the granularity of transactions in an object-oriented language is usually at the level of individual assignments to fields of primitive types. In general, object-oriented languages have no counterpart to isolation or durability, so atomicity and consistency are guaranteed only when writing to fields of these primitive types.

Figure 3 - Our conceptual framework for impedance mismatch, including ORM strategies
| Level | impedance mismatch |
| Paradigm | Issues related to the incompatibility between two different views of a concept from the universe of discourse: one as a network of interacting objects; the other as a set of relations. |
| Language | Issues related to the incompatibility of data structures between object and relational languages. Lammel calls this canonical mapping. In this article we will use the term pattern in Keller's context, as an outline of the solution description. |
| Schema | Issues related to maintaining two representations of a defined concept, described in different languages. |
| Instance | Issues related to storing and retrieving an object in the context of an object-relational application. |
Work on the impedance mismatch problem for object-oriented programs begins with recognizing the differences in the specific logical systems used. The mismatch is then either minimized or compensated for.
The object-relational impedance mismatch problem is not a universal problem between object-oriented objects and databases. As the name suggests, this impedance problem arises only with relational databases. The most common solution to this problem is to use an alternative database, such as a NoSQL or XML database.
Several attempts have been made to create object-oriented database management systems (OODBMS) that would avoid the impedance mismatch problem. However, in practice they have proven less successful than relational databases, partly because of the limitations of object-oriented principles as a foundation for a data model. Research has been carried out on extending the database-like capabilities of object-oriented languages using concepts such as transactional memory.
One common solution to the impedance mismatch problem is to overlay domain logic and structure. In this scheme, an object-oriented language is used to model certain relational aspects at runtime, rather than attempting a more static mapping. Frameworks that use this method typically have a tuple analog, usually as a «row» in a «dataset» component or as a generic «entity instance» class, as well as an analog for a relation. The advantages of this approach can include:
The disadvantages can include:
Mixing levels of discourse in object-oriented application code creates problems, but some common mechanisms are used to compensate for them. The biggest challenge is providing framework support, data-manipulation automation, and presentation patterns at the level of discourse in which the domain data is modeled. To address this, reflection and/or code generation are used. Reflection allows code (classes) to be treated as data, thereby enabling automation of data transport, presentation, integrity, and so on. Generation solves the problem by treating entity structures as input to code-generation tools or metaprogramming languages that mass-produce classes and supporting infrastructure. Both of these schemes can still be subject to certain anomalies when these levels of discourse merge. For example, generated entity classes will typically have properties that correspond to the domain (e.g., name, address) as well as properties that provide state management and other infrastructure plumbing (e.g., IsModified).
Christopher J. Date and others have argued that a truly relational DBMS would not create such a problem, , since domains and classes are essentially the same thing. Native mapping between classes and relational schemas is a fundamental design mistake; and individual tuples in a database table (relation) should be viewed as establishing relationships between objects, not as representations of complex objects themselves. However, this view tends to diminish the influence and role of object-oriented programming, treating it as little more than a field-type management system.
Impedance mismatch arises when programming between domain objects and the user interface. Complex user interfaces that allow operators, managers, and other non-programmers to access and manage database records often require deep knowledge of the nature of various database attributes (beyond name and type). In particular, it is considered good practice (from the standpoint of end-user performance) to design user interfaces so that the interface prevents the entry of illegal transactions (those that would cause database constraint violations); this requires that much of the logic present in relational schemas be duplicated in the code.
Certain code-development platforms can make use of certain forms of logic represented in the database schema (such as referential integrity constraints), so that such problems are solved in a standard and generic way through library procedures, rather than by ad hoc code written for the case at hand.
It has been argued that SQL, due to its very limited set of domain types (and other perceived shortcomings), makes it difficult to properly model objects and domains; and that SQL is a very inefficient and ineffective interface between a DBMS and an application program (whether written in an object-oriented style or not). However, SQL is currently the only widely adopted database language on the market; using vendor-specific query languages is considered bad practice whenever it can be avoided. Other database languages, such as Business System 12 and Tutorial D, have been proposed, but neither has achieved widespread adoption among DBMS vendors.
In current versions of major «object-relational» DBMSs, such as Oracle and Microsoft SQL Server, the above point may no longer matter. With these mechanisms, the functionality of a given database can be arbitrarily extended with stored code (functions and procedures) written in a modern object-oriented language (Java for Oracle and Microsoft's .NET language for SQL Server), and these functions can in turn be invoked in SQL statements transparently: that is, the user neither knows nor cares that these functions/procedures were not originally part of the database engine. Modern software development paradigms are fully supported: thus, a set of library procedures can be created that can be reused across multiple database schemas.
These vendors chose to support the integration of an object-oriented language into the DBMS back end because they realized that, despite the ISO SQL-99 committee's attempts to add procedural constructs to SQL, SQL will never have the rich set of libraries and data structures that modern application programmers take for granted, and it makes sense to use them as directly as possible rather than trying to extend the base SQL language. Consequently, the line between "application programming" and "database administration" is now blurred: reliably implementing features such as constraints and triggers may often require a person with both DBA and OO programming skills, or a partnership between people who combine these skills. This fact is also relevant to the "separation of responsibilities" issue discussed below.
Some, however, would point out that this claim is debatable because: (1) DBMSs were never intended to facilitate object modeling, and (2) SQL should generally be regarded merely as a lossy or inefficient interface when one tries to find a solution for which DBMSs were not designed. SQL performs very efficiently the task it was designed for, namely querying, sorting, filtering, and storing large data sets. Some would further note that embedding object-oriented programming language functionality in the back end simply encourages poor architectural practice, since it allows high-level application logic to leak into the data layer, which runs counter to the RDBMS approach.
This is where the «canonical» copy of the state resides. The database model generally assumes that the database management system is the single authoritative store of state concerning the enterprise; any copies of such state held in the application program are merely temporary copies (which may become stale if the underlying database record is subsequently changed by a transaction). Many object-oriented programmers prefer to regard the in-memory representations of the objects themselves as the canonical data and to treat the database as a backing store and persistence mechanism.
Another contentious issue is the proper division of responsibility between application programmers and database administrators (DBAs). It is often the case that necessary changes to application code (to implement a requested new feature or functionality) require corresponding changes to the database definition; in most organizations, the DBA is responsible for the database definition. Because of the need to keep a production database system running 24 hours a day, many DBAs are reluctant to make changes to database schemas that they consider unnecessary or excessive, and in some cases refuse to do so at all. Using development databases (in addition to production systems) can help to some extent; but once the newly developed application "goes live," the DBA will need to approve any changes. Some programmers consider this irreconcilable;
However, in organizations with a dysfunctional relationship between DBAs and developers, the problem described above should not arise, since the decision to change the database schema or not will depend solely on business needs: for example, a new requirement to store additional data, or an effort to improve the performance of a business-critical application, will trigger a schema change.
The key philosophical differences between the object-oriented and relational models can be summarized as follows:
As a result of the object-relational impedance mismatch, proponents of both sides of the debate often argue that the other technology should be abandoned or scaled back. Some database proponents consider traditional «procedural» languages more compatible with DBMSs than many object-oriented languages, or suggest using a less object-oriented style. (Specifically, it is argued that long-lived domain objects should not exist in application code; any such objects that do exist should be created when a query is executed and destroyed once the transaction or task is complete.) Conversely, some object-oriented proponents argue that more OO-friendly persistence mechanisms, such as OODBMS, should be developed and used, and that relational technology should be discontinued. Many (if not most) programmers and database administrators hold neither of these views, and regard the object-relational impedance mismatch as simply a fact of life that information technology has to deal with.
It is also argued that O/R mapping pays off in some situations, but is probably oversold: alongside its drawbacks it does have advantages. Skeptics note that it is worth thinking carefully before using it, since in some cases it will provide no benefit at all.
Many users are interested in a combined approach that would let them take advantage of object databases without giving up their relational databases entirely. Such solutions do exist. If migrating from a relational database to an object database is too costly, then using the latter as an extension and complement to relational DBMSs is often a more economical alternative. Compromise solutions make it possible to strike a balance between objects and relational tables (Figure 3).
.
Object-relational adapters. This method involves using a so-called object-relational adapter, which automatically extracts program objects and stores them in relational databases. The object-oriented application runs as an ordinary user of the DBMS. Despite some reduction in performance, this option allows programmers to focus entirely on object-oriented development. In addition, all applications already existing within the enterprise can still access the data stored in relational form.
Some object DBMSs, such as GemStone from GemStone Systems, can themselves act as a powerful object-relational adapter, allowing object-oriented applications to access relational databases.
Object-relational adapters, such as Hewlett-Packard's Odapter for the Oracle DBMS, can be used successfully in many areas, for example as middleware linking object-oriented applications with relational DBMSs.
Object-relational gateways. With this method, the user interacts with the database using the OODBMS language, and the gateway replaces all the object-oriented elements of that language with their relational counterparts. Here again, one pays a price in performance. For example, the gateway must convert objects into a set of links, generate original identifiers (OID) for the objects, and pass this to the relational database. The gateway must then, each time the relational DBMS interface is used, convert the OID found in the database into the corresponding object stored in the RDBMS.
Performance in the two approaches discussed depends on the method of accessing the relational database. Every RDBMS consists of two layers: the data manager layer and the storage manager layer. The former processes SQL statements, while the latter maps data to storage. A gateway or adapter can interact either with the data layer (i.e., access the RDBMS via SQL) or with the storage layer (via low-level procedure calls). Performance in the first case is much lower (for example, Hewlett-Packard's OpenODB system, which can act as a gateway, only supports the high-level approach).
Hybrid DBMSs. Another solution could be to create hybrid object-relational DBMSs capable of storing both traditional tabular data and objects. Many analysts believe that the future belongs to such hybrid databases. Leading relational DBMS vendors are beginning (or planning) to add object-oriented facilities to their products. In particular, Sybase and Informix are planning to introduce object support in upcoming versions of their DBMSs. Independent firms intend to pursue similar developments. For example, Shores is preparing to equip Oracle8 with object-oriented facilities, with a release planned for late 1996.
On the other hand, object DBMS vendors, such as Object Design, recognize that object-oriented databases are unlikely to replace relational DBMSs in the foreseeable future. This is forcing them to build gateways to support relational and hierarchical databases, or various kinds of interfaces, a characteristic example being Ontos's object-relational interface, the Ontos Integration Server, used together with its Ontos/DB OODBMS. [Source 3]
Comments