Using inheritance and polymorphism with entities
Consider as an example that your data model could contain the following entities: Company, Individual, Party and Product. With Talend Studio, you can define inheritance types where Individual and Company both extend Party. Party becomes a polymorphic element that holds individuals and companies.
You can also define in the Product entity a foreign key in the supplier attribute that points at the Party entity. So, this foreign key can also point at Individual or Company since both extend Party, so it acts as a polymorphic foreign key.
Before you begin
You have already created your data model. For further information, see Creating a data model.
About this task
To create inheritance and polymorphism for entities, proceed as follows:
Procedure
What to do next
Your data model should look like the one shown below:
![](/en-US/studio-user-guide/8.0-R2024-07/Content/Resources/images/Inheritance_Poly2.png)
The Company and Individual entities extend the Party entity using the PartyType group. All complex types are also listed in the Data Model Types panel.
Now, you can create a polymorphic foreign key which, by pointing to the Party entity, can also point to the Individual or Company entities, since both extend Party.
-
Create a supplier attribute in the Product entity.
Right-click supplier and select Set the Foreign Key from the list.
A dialog box pops up.
-
Click the three-dot button and browse to set an Xpath to the entity to which you want the foreign key to point. In this example, the Xpath is set to Party/code.
-
Click Add and then OK.
The foreign key path is listed below the supplier attribute.
You have achieved inheritance here at the entities level by adding the Party group type to the Company and Individual entities.
Polymorphism is also achieved when, at runtime, the actual Party element can be expanded into Individual or Company when a business user tries to create a data record through Talend MDM Web UI. Business users will also have access to a foreign key list that displays Individual and Company when they click the Select the relationship icon. From this list they can choose any of the extended types you defined in Talend Studio.
![](/en-US/studio-user-guide/8.0-R2024-07/Content/Resources/images/Inheritance_Poly5.png)
By default, the complex types list displays the code of the types, but Talend Studio also lets you define labels for the complex types so that they can have labels in different languages and become more understandable as displayed in the Select a type list. To do so, proceed as follows:
-
Open the data model where you have defined the complex types.
-
Click Schema Source at the bottom of the editor to open the source XML schema of the data model.
-
Browse to the schema corresponding to the extended complex type to which you want to add a label and which you want to display in a certain order for the business user.
-
Inside the root element of the complex type, use the X_Label_<ISO> annotation to add a label for the extended type as follows:
... <xsd:complexType name="IndividualType"> <xsd:annotation> <xsd:appinfo source="X_Label_EN">individual supplier</xsd:appinfo> <xsd:appinfo source="X_Label_FR">fournisseur individuel</xsd:appinfo> </xsd:annotation> <xsd:complexContent> ...
In the above schema, you want to name the IndividualType as individual supplier and you want it to be the third in the list of extended types.
-
Save your modifications in the model editor.
When authorized business users browse master data records in the updated data model through Talend MDM Web UI, the Select the relationship icon gives them access to the extended type list that shows the labels you defined in the annotation elements.
![](/en-US/studio-user-guide/8.0-R2024-07/Content/Resources/images/Inheritance_Poly8.png)