Developers tend to use object-oriented programming languages like Java, to develop business objects and components. These objects can be stored in a database. A problem arises when the user tries to store objects in a relational database because object modeling describes a system through objects that have identity, behavior and encapsulated state whereas relational modeling describes a system by its data.
Furthermore, object codes in an OOM for a given object language are often different than codes used in a relational database. This requires that you modify object codes after generating an OOM into a PDM or a PDM into an OOM to be compliant with the object language.
You can use object mapping to bypass this impedance-mismatch.
The following schema illustrates the link between classes and tables to store objects in a relational database:
Object persistence implies to store and extract objects in a relational database.
When a class inherits from a non-generated class via a generalization link, the attributes of the class appear in the Selection dialog box in order to let you create a mapping with these inherited attributes. The attributes of a non-generated derived class also appear in the Selection dialog box.
O/R mapping on an association allows you to define the role navigability of an association in the database. When an association role is not navigable, there is no need to design a mapping for the association since no information is transmitted between the classes. However, when an association role is navigable, you have to set up the structure for data transmission within the database. In relational databases, data transmission is implemented via foreign keys designed to relate a record in one table with a record in another.
The type of mapping for an association depends on the association multiplicity:
One-to-one or one-to-many associations can be mapped to a reference in the source model. This reference is used in the database to transfer data and migrate key columns to the appropriate table
Many-to-many associations have to be mapped to an associative table. This table is created to maintain a relationship between two or more tables in a relational database. The columns contained in the associative table are the combination of the keys in the tables involved in the reference. For example, the following classes have a many-to-many association:
In a relational database, this association is mapped to the following associative table:
When the association roles are navigable, the following queries are automatically computed:
Query |
Action |
---|---|
Select (Role A) |
Retrieves related class A instances for the role A defined on class B |
Insert (Role A) |
Associates an instance of class A with class B. It is computed only when the source association is an associative table |
Delete (Role A) |
Deletes from class B the association with class A. This query is computed only when the source association is an associative table |
Select (Role B) |
Retrieves related class B instances for the role B defined on class A |
Insert (Role B) |
Associates an instance of class B with class A. This query is computed only when the source association is an associative table |
Delete (Role B) |
Deletes from class A the association with class B. This query is computed only when the source association is an associative table |