Java Reference
In-Depth Information
Listing 7-9. Mapping a Property Advert with the One-Table-per-Subclass Approach
<hibernate-mapping>
<joined-subclass
name="com.hibernatebook.xmlmapping.Property"
extends="com.hibernatebook.xmlmapping.Advert">
<key column="advertid"/>
<property name="state" type="string"/>
<property name="zipCode" type="string"/>
<property name="description" type="string"/>
</joined-subclass>
</hibernate-mapping>
Note in the mapping that we replace class with joined-subclass to associate our map-
ping explicitly with the parent. You specify the entity that is being extended and replace the
id and title classes from the subclass with a single key element that maps the foreign key
column to the parent class table's primary key. Otherwise, the <joined-subclass> element is
virtually identical to the <class> element. Note, however, that a <joined-subclass> cannot
contain <subclass> elements and vice versa—the two strategies are not compatible.
One Table per Class Hierarchy
The last of the inheritance mapping strategies is to place each inheritance hierarchy in its own
table. The fields from each of the child classes are added to this table, and a discriminator col-
umn contains a key to identify the base type represented by each row in the table.
Figure 7-8 shows the schema required to represent the hierarchy from Figure 7-5 using
this technique.
Figure 7-8. Mapping one table per hierarchy
Search WWH ::




Custom Search