Inheritance Mapping
- Supported inheritance mapping strategies
- Specifying inheritance mapping per class hierarchy
- Multiple Inheritance Support
- Links to Inheritance-Mapping Articles
Supported inheritance mapping strategies
The Teneo OR Mapper supports two of the three inheritance mapping of JPOX (see here on the jpox website):
- Table-per-class-hierarchy or super-class: in this scenario the classes of one class hierarchy are all mapped to one table. In JDO-2 this is called superclass-table.
- Table-per-subclass or joined-subclass: in this strategy each subclass will have its own table. To retrieve an object from the database the superclass and subclass tables are joined. This also applies to subclasses of subclasses. In JDO-2 this is called new-table.
The OR Mapper can generate a mapping file using either of the two mapping strategies. The two approaches are visible as separate menu options in the Teneo menu (when clicking on a Java package in Eclipse).
There is also another way of mapping inheritance structures: MappedSuperclass. In this method the superclass is not persisted by itself and does not have its own table. All the properties of the superclass are stored in the table(s) of the subclass(es). MappedSuperclass is supported by setting an EJB3-like annotation. For more information see the EJB3 spec or the jpox website (subclass-table).
Specifying inheritance mapping per class hierarchy
There are cases where it is necessary to use different inheritance mapping techniques for different class hierarchy within one application or project. This can be achieved by adding specific EJB3-like annotations to the type definition (uml or XML Schema).
As an example see the following type definition of Address which sets the inheritance mapping strategy to JOINED (joined subclass) for Address and all its subtypes:
<xsd:complexType name="Address" abstract="true"> <xsd:annotation> <xsd:appinfo source="teneo.jpa">@Inheritance(strategy=JOINED)</xsd:appinfo> </xsd:annotation> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> </xsd:sequence> </xsd:complexType>
The inheritance-mapping annotation can have one of the two following values: JOINED or SINGLE_TABLE.
The above annotation can also be added in UML. Please use the same values for the source and the key.
The inheritance annotation has changed, from the 0.7.2 to the 0.7.5 release, to an java annotation syntax format.
Multiple Inheritance Support
In EMF it is possible to define multiple inheritance structures. Although JPOX itself does not support multiple inheritance Teneo does its best to map a multiple inheritance structure.
Choosing the real supertype
Teneo maps the generated implementation classes to JPOX, i.e. it maps the single inheritance structure defined in these implementation classes. The properties of the other super types are mapped together with the subclass. So in a way these other supertypes are treated as a MappedSuperclass (see the jpa/ejb3 spec). The other super types will have their own tables in the database (unless they are interfaces) but there will be no relations between the subclass table and its other superclass tables.
See as an example the figure below, the figure illustrates how a model is translated to a relational database schema using a joined-subclass inheritance mapping strategy. The SchoolBook inherits from both Book and Asset. This is translated into a relational model with SchoolBook refering to the chosen real supertype (Book) and also inheriting the value-efeature from Asset. This means that when the SchoolBook is read from the database then it will also implement the Asset interface/type and its value efeature is set correctly. On the other hand there is no relation to the Asset table (see limitation below).

Limitation
The main limitation is that references to supertypes do not (always) work correctly. Using the example above. According to the model and the generated code the AssetList can also contain SchoolBooks (as the type inherits from Asset). However, the database schema shows that a SchoolBook does not have a foreign key reference to the AssetList table. So when an AssetList with SchoolBooks is persisted the save action will fail.
Links to Inheritance-Mapping Articles
- Inheritance page on the JPOX website
- Mapping Objects To Relational Databases by Scott Ambler