Java Reference
In-Depth Information
This technique offers the best performance—for simple queries on simple classes even
in the deepest of inheritance hierarchies, a single select may suffice to gather all the fields to
populate the entity.
Conversely, this is not a satisfying representation of the attribute. Changes to members of
the hierarchy will usually require a column to be altered, added, or deleted from the table. This
will often be a very slow operation. As the hierarchy grows (horizontally as well as vertically),
so too will the number of columns required by this table.
Each mapped subclass must specify the class that it extends and a value that can be used
to discriminate this subclass from the other classes held in the same table. Thus, this is known
as the discriminator value, and is mapped with a discriminator-value attribute in the
<subclass> element (see Listing 7-10).
Listing 7-10. Mapping a Property Advert with the One-Table-per-Class-Hierarchy Approach
<hibernate-mapping>
<subclass
name="com.hibernatebook.xmlmapping.Property"
extends="com.hibernatebook.xmlmapping.Advert"
discriminator-value="property">
<property name="state" type="string"/>
<property name="zipCode" type="string"/>
<property name="description" type="string"/>
</subclass>
</hibernate-mapping>
Note that this also requires the specification of a discriminator column for the root of the
class hierarchy, from which the discriminator values identifying the types of the child classes
can be obtained (see Listing 7-11).
Listing 7-11. The Addition to Advert.hbm.xml Required to Support a One-Table-per-Class-
Hierarchy Approach
<discriminator column="advertType" type="string"/>
A subclass mapping cannot contain <joined-subclass> elements and vice versa—the two
strategies are not compatible.
More Exotic Mappings
The Hibernate mapping DTD is large. We have discussed the core set of mappings that you
will use on a day-to-day basis; but before we move on, we will take a very quick tour around
four of the more interesting remaining mapping types.
Search WWH ::




Custom Search