Table per Hierarchy

Table per Hierarchy (TPH) inheritance is the commonest and easiest type of inheritance.

In TPH the inheritance tree is created through one table only. The TPH inheritance depends on a conditional mapping which is defined by a condition such as a discriminator database field. This condition is used to define records as different types.

For example, the database contains the following table:

Images_TPHtable

In this table, ClassType is the discriminator (that can be either a string or integer), which determines the specific type of the animal.

For the purpose of this example, we shall perform the following sequence of actions within the Database-First approach.

Firstly, in the Database Explorer window locate the TPH_Animal table and move it onto the diagram surface to create the TPHAnimal class. At this moment, this class includes properties that are common for all types of animals as well as animal-specific properties.

The common properties are: FoodClassification, BirthDate, Family, and Genus. These properties should remain in the TPHAnimal class, while all the other properties should be moved to the corresponding classes.

Now, we shall start by selecting and moving the snake-related properties (Length and IsAdder) from the TPHAnimal class onto the design surface. This displays the following dialog:

Images_ChooseActionTPH

In the Model Refactoring dialog, we select New derived class, enter the name of the class (Snake) into the corresponding box and click OK.

Note that the TPH inheritance is created automatically between the Snake class and the TPHAnimal class. To edit this inheritance, we double-click it and, in the Inheritance Editor window, enter the values, as shown below:

Images_INheritanceEditorTPH

Note that the discriminator value specified for the base class is preserved for all other inheritances created with this class. On INSERT, this value is used as the value of the discriminator property column in the database table, while on FETCH allows identifying the corresponding class as the base one.

note Note

In the Discriminator area, select either the Column option button and specify the column, whose values are to be used as the discriminator, or select the Formula option button to enter a SQL expression that is to be used to calculate the discriminator value.

Clear the Insert check box, if the discriminator column is also part of a mapped composite identifier.

Select the Force check box, so that a condition is added to the query specifying discriminator values contained in hierarchy objects, even when the entire hierarchy is queried.

Similarly, we create the Dog class (with the Breed property), the Horse class (with the MaximumSpeed property), and the Crocodile class (with the Weight and Length properties), and edit inheritances that are automatically created between each of the classes and the TPHAnimal class to obtain the final model (see below).

Images_TPH_model

 

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.