Skip to content

1.Object mapping

1. Introduction. Mapping technics.

In previous units we have studied the access from our programs to files and relational databases. As seen, the big problem is that our programs handle objects, while databases handle tables, which causes the use of different models, and ends in the aforementioned object-relational gap.

The object-relational mapping come to fill the gap between both models, preventing us from spending time in our code converting between the results of the operations and our objects. From the use of an ORM (Object Relational Mapping), once the correspondence between models has been established, in our programs we will only work with one model, the object-oriented model. In this way we manage to avoid our programs from that gap, focusing on the business logic.

ORM techniques (Object-Relational Mapping Tools) are responsible, through a set of descriptions and metadata (data who describes data) to make a correspondence between the primitive data of both models and their structures: between tables and objects, fields and attributes, their identifiers and their primary keys. This correspondence will not always be simple, and metadata that can express greater complexity will have to be available.

For example:

  • We may find that sometimes it may be interesting to store a property in more than one column, or several properties in a single column.
  • At other times, there may be properties that are not stored, or database fields that do not appear in the objects
  • You use attributes with non-primitive data types that need to be cast in other tables, and decide which fields will be foreign keys that point to the new tables.

In the same way as the definition of the data, we will need an object persistence mechanism, so that the objects can be "tracked" in memory, and when changes in them are reflected directly in the database.

2. Mapping

![ORM](./img/ORM_Eng.png){width=90%}

With this mapping tools we will:

  • Reduce or development time
  • Forget our underlying DBMS
  • Work with objects, attributes and methods. You do not need nothing about tables and relations.

Al mapping tools are based in these three solid bases

2.1. Mapping technics

We highlight two object-relational mapping techniques:

  • Those that embed the definitions within the code of the classes, and are linked to the language, such as C++ macros or PHP and Java annotations.
  • Those that save the definitions in files independent of the code, generally in XML or JSON

These are not mutually exclusive techniques, as both are available in most environments, and can even coexist in the same application.

2.2. Query language

As DBMS includes SQL as query language, most ORM's includes its own language, based on OQL Object Query Language. Like SQL we can ask for information but based on objects instead of tables.

2.3. Synchronization

ORM's includes several processes focused on:

  • Track and discover the changes that objects undergo during their life cycle in order to store them.
  • Create and start new instances of objects from the data stored in the database.
  • From the objects, extract their information to reflect it in the database tables.

Note

We will study lifecycle of objects when are loaded and saved from/to database.