DekGenius.com
[ Team LiB ] Previous Section Next Section

12.6 Inheritance

In object-relational systems, inheritance can be a tricky beast. The problem with inheritance is the lack of support for inheritance in relational databases. In other words, you need to persist a concept from your application that a relational system has no way of modeling. Fortunately, JDO handles this beast for you. Figure 12-2 shows a Person class with many Role instances. Role, however, is an abstract class. Its subclasses include Admin, Employee, Manager, and Contractor.

Figure 12-2. An abstract Role with many implementation classes
figs/jdbp_1202.gif

We can build this class model using the normal Java approach. The next step is to build a metadata descriptor for the Person class as shown in Example 12-4.

Example 12-4. A metadata descriptor with abstract persistent fields
<?xml version="1.0" ?>
<!DOCTYPE jdo SYSTEM "jdo.dtd">
   
<jdo>
  <package name="com.imaginary.ora">
    <class name="Person">
      <field name="personID" primary-key="true"/>
      <field name="lastName"/>
      <field name="firstName"/>
      <field name="title"/>
      <field name="roles">
        <collection element-type="com.imaginary.ora.Role"/>
      </field>
    </class>
   
    <class name="Role">
      <field name="code"/>
      <field name="name"/>
    </class>
  
    <class name="Employee" 
    persistence-capable-superclass="com.imaginary.ora.Role">
    </class>
    ...
  </package>
</jdo>

This code tells the JDO implementation that Employee is a persistent object and that it derives some or all of its persistence from the Role class.

Earlier in the chapter, I mentioned how the second argument to getExtent( ) in PersistenceManager is a boolean indicating whether subclasses may be counted in the extent. If you tell it not to count subclasses, it will return only exact instances of the class in question. This feature enables you to limit queries to a specific class or to the class and its subclasses.

    [ Team LiB ] Previous Section Next Section