Cayenne User Documentation
Modeling Inheritance

### Modeling Inheritance

"Inheritance" 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 "single table inheritance"). One or more columns in the base table are usually assumed to be "class designator columns"; their values determine what object class to use when a fetched row is converted to a persistent object.

#### Creating ObjEntity Hierarchy

Consider the following class hierarchy that we want to map to a PERSON table:

For each of the four Java classes one may create individual ObjEntities, however only AbstractPerson entity would map directly to the underlying "PERSON" DbEntity. For the rest instead of selecting a value from the "Table/View" dropdown, a corresponding "super entity" from the "Inheritance" 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.

#### Defining Class Qualifier

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 "EMPLOYEE", "MANAGER", "CUSTOMER", the following qualifiers might be used:

ObjEntity | Qualifier | Final Qualifier Generated by Cayenne for SelectQuery
--- | --- | ---
AbstractPerson | none | none
CustomerContact | personType = 'CUSTOMER' | personType = 'CUSTOMER'
Employee | personType = 'EMPLOYEE' | personType = 'EMPLOYEE' or personType = 'MANAGER'
Manager | personType = 'MANAGER' | personType = 'MANAGER'

| Qualifiers Note 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 "or" operator, as shown in the above example.

.
Copyright ©2001-2008 Apache Software Foundation