Object-Relational
Mapping
inheritance, or interfaces are generally more difficult to
store and retrieve with reasonable performance.
MAPPING
Inheritance. In the domain object model, inheritance
is a relationship between two classes in which one class
is a specialization of another. Figure 1 shows a human
relations object model in which Full TimeEmployee and
Part TimeEmployee are specializations of Employee, which
in turn is a specialization of Person.
There are several ways to map this domain object
model to relational schema. A single-table inheritance
strategy maps all classes in the inheritance hierarchy to a
single table that contains a column for each of the fields
in any of the classes. In this mapping, slots of the table
Single Ta ble Inheritance Strategy
<<table>>
PERSON
<<PK>>+PERSON_ID : int4
+FIRST_NAME : char varying
+LAST_NAME : char varying
+MIDDLE_NAME : char varying
-HIRE_DATE : date
-WEEKLY_SALARY:currency
+HOURLY_WAGE:currency
-DISCRIMINATOR:char
FIG 2
contain null or default values when the row represents
an instance of a class that did not contain a field for that
column.
For the single-table strategy to work, the database must
contain information that allows the mapping to determine to which class any particular row is mapped. The
mapping typically contains the name of a discriminator
column and the values of that column that map to each
of the possible classes.
For example, the PERSON table (shown in figure 2)
might contain a DISCRIMINATOR column whose values are
S, E, F, and P, depending on whether the row maps to an
instance of Person, Employee, Full TimeEmployee, or
PartTimeEmployee, respectively. When retrieving rows from
the PERSON table, the provider would always retrieve the
value of the DISCRIMINATOR column in order to instantiate
the correct class.
A table-per-class inheritance strategy (figure 3) maps
each class to its own table and maps each field to a
column in the mapped table. All of the data for a single
instance would have the same primary-key column value
in each of the tables, and the schema would declare
foreign-key relationships among the tables. When querying these tables, the provider constructs join queries to
determine to which class a row maps. For example, to
find all employees hired after a specific date, the query
would join the PERSON, EMPLOYEE, PART_TIME_EMPLOYEE,
and FULL_TIME_EMPLOYEE tables to determine whether
the rows were mapped to Person, Part TimeEmployee, or
FullTimeEmployee.
Tab le Per Class Inheritance Strategy
<<table>>
PERSON
<<PK>>+PERSON_ID : int4
+FIRST_NAME : char varying
+LAST_NAME : char varying
+MIDDLE_NAME : char varying
<<FK>>
<<table>>
EMPLOYEE
-PERSON_ID : int4
+EMPLOYEE_ID : int4
-HIRE_DATE : date
<<FK>>
<<FK>>
<<table>>
FULL_TIME_EMPLOYEE
-PERSON_ID : int4
-WEEKLY_SALARY:currency
-PERSON_ID : int4
+HOURLY_WAGE : currency