blob: ee2359de04c117091b3ccf281d545f0f2f740ef4 [file] [log] [blame]
Title: Modeling Inheritance
<H3><A name="ModelingInheritance-ModelingInheritance"></A>Modeling Inheritance</H3>
<P>&quot;Inheritance&quot; is an Object Oriented concept absent from traditional RDBMS world. Still Cayenne allows to map a class hierarchy to the database schema. All classes in the hierarchy map to the same base table (this type of mapping is therefore called &quot;single table inheritance&quot;). One or more columns in the base table are usually assumed to be &quot;class designator columns&quot;; their values determine what object class to use when a fetched row is converted to a persistent object. </P>
<H4><A name="ModelingInheritance-CreatingObjEntityHierarchy"></A>Creating ObjEntity Hierarchy</H4>
<P>Consider the following class hierarchy that we want to map to a PERSON table:</P>
<P><SPAN class="image-wrap" style=""><IMG src="modeling-inheritance.data/inheritance-diagram.jpg" style="border: 0px solid black"></SPAN></P>
<P>For each of the four Java classes one may create individual ObjEntities, however only AbstractPerson entity would map directly to the underlying &quot;PERSON&quot; DbEntity. For the rest instead of selecting a value from the &quot;Table/View&quot; dropdown, a corresponding &quot;super entity&quot; from the &quot;Inheritance&quot; dropdown should be selected. Note that when an entity inherits from another entity, a list of inherited attributes and relationships shows up as read only information under the corresponding tabs.</P>
<H4><A name="ModelingInheritance-DefiningClassQualifier"></A>Defining Class Qualifier</H4>
<P>Afer creating entity inheritance tree, it is important to configure how the entities differ from each other, so that later when the data is fetched, Cayenne would know which class to instantiate. This is achieved by using entity qualifiers described earlier. Usually all entities in the hierarchy, except for the root, require such qualifier. It should be created in such a way that it completely defines a given entity without considering any subclasses or superclasses. In the example above, if the possible values of the class designator column are &quot;EMPLOYEE&quot;, &quot;MANAGER&quot;, &quot;CUSTOMER&quot;, the following qualifiers might be used:</P>
<DIV class="table-wrap">
<TABLE class="confluenceTable"><TBODY>
<TR>
<TH class="confluenceTh">ObjEntity</TH>
<TH class="confluenceTh">Qualifier</TH>
<TH class="confluenceTh">Final Qualifier Generated by Cayenne for SelectQuery</TH>
</TR>
<TR>
<TD class="confluenceTd">AbstractPerson</TD>
<TD class="confluenceTd">none</TD>
<TD class="confluenceTd">none</TD>
</TR>
<TR>
<TD class="confluenceTd">CustomerContact</TD>
<TD class="confluenceTd">personType = 'CUSTOMER'</TD>
<TD class="confluenceTd">personType = 'CUSTOMER'</TD>
</TR>
<TR>
<TD class="confluenceTd">Employee</TD>
<TD class="confluenceTd">personType = 'EMPLOYEE'</TD>
<TD class="confluenceTd">personType = 'EMPLOYEE' or personType = 'MANAGER'</TD>
</TR>
<TR>
<TD class="confluenceTd">Manager</TD>
<TD class="confluenceTd">personType = 'MANAGER'</TD>
<TD class="confluenceTd">personType = 'MANAGER'</TD>
</TR>
</TBODY></TABLE>
</DIV>
<DIV class="panelMacro"><TABLE class="noteMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cayenne.apache.org/docs/1.2/images/icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B>Qualifiers Note</B><BR>Qualifiers are not inherited! When defining qualifiers for inheritance purposes keep in mind that the actual qualifier built by Cayenne will include the qualifier of a root entity and qualifiers of all its known subentities joined using &quot;or&quot; operator, as shown in the above example.</TD></TR></TABLE></DIV>