JPOX
JPOX
 JPOX Version 1.0
Configuration | Tutorials | Worked Examples | Developer
Obtaining JPOX
JPOX Preparation
JPOX Runtime
Inheritance

JPOX supports the use of inherited classes. These classes can be used in a straightforward manner, or can be used as the elements in collections.

Lets take an example. We have a class

public class Product
{
    protected String name;
    protected String description;
    protected double price;

    protected Product()
    {
    }

    public Product(String name,String desc,double price)
    {
        ...
    }
}
            

And lets have some subclasses

public class Book extends Product
{
    String isbn;
    String author;
    String title;

    protected Book()
    {
        super();
    }
}

public class TravelGuide extends Book
{
    String country;

    protected TravelGuide()
    {
        super();
    }
}

public class CompactDisc extends Product
{
    String artist;
    String title;

    protected CompactDisc()
    {
        super();
    }
}
            


So we have 1 inheritance tree with 2 branches. These are related in the JDO Meta-Data like this

<package name="org.jpox.sample.store">
    <class name="Product">
        <field name="name">
            <extension vendor-name="jpox" key="length" value="max 100"/>
        </field>
        <field name="description">
            <extension vendor-name="jpox" key="length" value="max 255"/>
        </field>
        <field name="price"/>
    </class>
    <class name="Book" persistence-capable-superclass="org.jpox.samples.store.Product">
        <field name="isbn">
            <extension vendor-name="jpox" key="length" value="max 20"/>
        </field>
        <field name="author">
            <extension vendor-name="jpox" key="length" value="max 40"/>
        </field>
        <field name="title">
            <extension vendor-name="jpox" key="length" value="max 40"/>
        </field>
    </class>
    <class name="TravelGuide" persistence-capable-superclass="org.jpox.samples.store.Book">
        <field name="country">
            <extension vendor-name="jpox" key="length" value="max 40"/>
        </field>
    </class>
    <class name="CompactDisc" persistence-capable-superclass="org.jpox.samples.store.Product">
        <field name="artist">
            <extension vendor-name="jpox" key="length" value="max 40"/>
        </field>
        <field name="title">
            <extension vendor-name="jpox" key="length" value="max 40"/>
        </field>
    </class>
            

So we make use of the Meta-Data tag persistence-capable-superclass to relate the classes.

Datastore Treatment

In the datastore, each class in an inheritance tree is represented in its own datastore table (tables PRODUCT, BOOK, TRAVELGUIDE, and COMPACTDISC), with the subclasses tables' having foreign keys between the primary key and the primary key of the superclass' table.



Some JDO implementations allow the user to "flatten" the subclasses down into a single table for each inheritance tree. JPOX doesn't allow this option since it can cause issues regarding the duplication of fields between 2 classes in different branches of the inheritance tree. Additionally, the way we persist this data offers a better representation of the objects in our opinion. We may offer this flattening facility in a future release.

Retrieval of inherited objects

JDO provides particular mechanisms for retrieving inheritance trees. These are accessed via the Extent/Query interface. Taking our example above, we can then do

tx.begin();

Extent e = pm.getExtent(org.jpox.samples.store.Product.class,true);
Query  q = pm.newQuery(e);
Collection c=(Collection)q.execute();

tx.commit();
                

The second parameter passed to pm.getExtent relates to whether to return subclasses. So if we pass in the root of the inheritance tree (Product in our case) we get all objects in this inheritance tree returned. You can, of course, use far more elaborate queries using JDOQL, and SQL but this is just to highlight the method of retrieval of subclasses.