Java Reference
In-Depth Information
Hibernate can represent inheritance relationships in a relational schema in three ways,
each mapped in a slightly different way. These are as follows:
One table for each concrete class implementation
One table for each subclass (including interfaces and abstract classes)
One table for each class hierarchy
Each of these techniques has different costs and benefits, so we will show you an example
mapping from each and discuss some of these issues.
One Table per Concrete Class
This approach is the easiest to implement. You map each of the concrete classes as normal,
writing mapping elements for each of its persistent properties (including those that are inher-
ited). No mapping files are required for interfaces and abstract classes.
Figure 7-6 shows the schema required to represent the hierarchy from Figure 7-5 using
this technique.
Figure 7-6. Mapping one table per concrete class
While this is easy to create, there are several disadvantages; the data belonging to a parent
class is scattered across a number of different tables, so a query couched in terms of the par-
ent class is likely to cause a large number of select operations. It also means that changes to
a parent class can touch an awful lot of tables. We suggest that you file this approach under
“quick-and-dirty solutions.”
Listing 7-8 demonstrates how a derived class ( Property ) can be mapped to a single table
independently of its superclass ( Advert ).
Listing 7-8. Mapping a Property Advert with the One-Table-per-Concrete-Class Approach
<hibernate-mapping>
<class name="com.hibernatebook.xmlmapping.Property">
<id name="id" type="int">
<generator class="native"/>
</id>
<property name="title" type="string" length="255"/>
Search WWH ::




Custom Search