Polymorphic Inheritance

Polymorphy is a concept of object oriented programming where objects of different classes act behind a façade of a base class or an interface. This allows performing the same operations in different ways.

In NDO Polymorphy means that you receive objects of all classes in an inheritance tree if you perform a query to the base class. It also means that a class with a relation to a base class is at the same time related to all derived classes.

Type Codes

To distinguish between different types in the database, NDO works with type codes. A type code is an integer value representing a data type. NDO stores the type codes in the mapping file. Every class gets a Type Code during enhancement.

Important: The type codes must not be changed after data has been stored in the database.  NDO never changes any type codes in the NDO mapping file. Since the Type Codes are hash values of the full name of the type, the same type will always get the same Type Code. There is one exception in this behavior: In case of a hash value collision there NDO assigns the next unused integer value.  

Intermediate Tables

NDO uses intermediate tables for all polymorphic relations except unidirectional relations with cardinality 1. These tables include the columns for foreign keys and if needed, type codes.

For the relation between Travel and Expense in the sample travel expense accounting system the intermediate table would have these columns:




If the Travel class, for example had sub classes, another column TCTravel would be included in the table.

Persistent Interfaces

Currently NDO does not support persistent interfaces. You should use abstract base classes instead which might be implementations of your interfaces.

Persistent Abstract Classes

Abstract base classes have entries in the NDOMapping.xml but NDO doesn't construct tables in the Xsd schema or in the database structure, because abstract classes never have instances. Abstract classes can define relations that are inherited by the subclasses. The relation mapping entries for the base class and the subclasses must be identical because the relations are implemented with the same intermediate table for all subclasses. The NDO Enhancer automatically generates the mapping information according to this requirement.

Inheritance of persistent Fields

As told in the chapter Non-polymorphic Inheritance, the subclasses inherit all persistent fields of the base classes. Each subclass is mapped to a specific table.