The SQL repository supports a simplified form of inheritance that uses an optional one-to-one relationship between the primary table and an auxiliary table. The same repository can define one item descriptor that inherits properties from another.
For example, a clothing store catalog might offer shirts and shorts. Because the two items are likely to have common properties, you might use inheritance as follows:
Define a base item descriptor class
clothing
which defines the properties common to shirts and shorts.Define item descriptors
shirt
andshorts
as sub-types ofclothing
, so they inherit the properties defined inclothing
.
The data model for the clothing catalog can be represented as follows:
This approach has several advantages
Avoids duplicated database columns and code.
Facilitates queries across multiple sub-types, such as
shirt
andshorts
. For example:find all clothing items where description contains "shorts"
The XML repository definition (with inheritance-related tags in bold face) looks like this:
<!-- The "clothing" item type, a base type --> <item-descriptor name="clothing" sub-type-property="type"> <!-- This is the primary table that holds clothing data --> <table name="clothing" type="primary" id-column-names="id"> <property name="type" data-type="enumerated"> <option value="shirt"/> <option value="shorts"/> </property> <property name="name"/> <property name="description"/> <property name="color"/> <property name="size"/> <property name="shippingWeight"/> </table> </item-descriptor> <!-- The "shirt" item type is a subclass of "clothing" --> <item-descriptor name="shirt" super-type="clothing" sub-type-value="shirt"> <table name="shirt" type="auxiliary" id-column-names="id"> <property name="season"/> </table> </item-descriptor> <!-- The "shorts" item type, now a subclass of "clothing" --> <item-descriptor name="shorts" super-type="clothing" sub-type-value="shorts"> <table name="shorts" type="auxiliary" id-column-names="id"> <property name="pleated" data-type="boolean"/> </table> </item-descriptor>
These definitions utilize inheritance as follows:
The parent item descriptor
clothing
defines properties that are common to its sub-typesshirt
andshorts
—for example,color
andsize
. This item descriptor’ssub-type-property
attribute points to the enumerated propertytype
, which specifies this item descriptor’s sub-types.The item descriptors
shirt
andshorts
define themselves as sub-types of theclothing
item descriptor through the attributessuper-type
(set toclothing
) andsub-type-value
(set toshirt
andshorts
, respectively).
Note: Instances of objects are associated with their superclasses by ID. So, in this example, a shirt ID always has a matching clothing ID.
A sub-type item descriptor must never set sub-type-value
to NULL
. Given the previous example: some clothing items might be neither shirts nor shorts. In this case, the clothing
item descriptor should set its sub-type-value
attribute to clothing
and add clothing
as an option to its sub-type-property
:
<!-- The "clothing" item type, a base type --> <item-descriptor name="clothing" sub-type-property="type" sub-type-value="clothing"> <!-- This is the primary table that holds clothing data --> <table name="clothing" type="primary" id-column-names="id"> <property name="type" data-type="enumerated"> <option value="clothing"/> <option value="shirt"/> <option value="shorts"/> <property/> ...
From the Repository API point of view, each ItemDescriptor maps to a single RepositoryView
. When an SQL repository uses item type inheritance, each parent item type results in a RepositoryViewContainer
that contains its subtype views as children.