JPOX
JPOX
 Project  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Guides  |  Tools
1.1 | Preparation | O/R Mapping | Runtime | Extensions | Developer
O/R Mapping
Relationships
Interfaces

JDO requires that implementations support the persistence of interfaces as first class objects (FCO's). JPOX provides this capability. It follows the same general process as for java.lang.Object since both interfaces and java.lang.Object are basically references to some persistable object.

FCO

Let's suppose you have an interface with a selection of classes implementing the interface something like this





You then have a class that contains an object of this interface type

public class ShapeHolder
{
    protected Shape shape=null;
    ...
}
                

To allow persistence of this interface field with JPOX you have 2 levels of control. The first level is the JDO 2 way. Here you need to define in the MetaData which classes implement this interface, so we do

<package name="org.jpox.samples.shape">
    <class name="Square">
        <implements name="org.jpox.samples.shape.Shape"/>
        ...
    </class>
    <class name="Circle">
        <implements name="org.jpox.samples.shape.Shape"/>
        ...
    </class>
    <class name="Rectangle">
        <implements name="org.jpox.samples.shape.Shape"/>
        ...
    </class>
</package>

This means that when mapping that field JPOX will look at which classes it knows about that implement the specified interface (defined by the implements element in the MetaData). Using this JDO2 way of doing things the field will accept ANY Persistence-Capable implementation of that interface.

JPOX allows you to extend this to specify the list of classes implementing the interface on a field-by-field basis, defining which of these implementations are accepted for a particular interface field. To do this you define the Meta-Data like this

<package name="org.jpox.samples.shape">
    <class name="ShapeHolder">
        <field name="shape" persistence-modifier="persistent">
            <extension vendor-name="jpox" key="implementation-classes" 
                value="org.jpox.samples.shape.Circle,
                       org.jpox.samples.shape.Rectangle,
                       org.jpox.samples.shape.Square"/>
        </field>
</class>

That is, for any interface object in a class to be persisted, you define the possible implementation classes that can be stored there. JPOX interprets this information and will map the above example classes to the following in the database





So JPOX adds foreign keys from the containers table to all of the possible implementation tables for the shape field.

Collections of Interfaces

You can have a Collection/Map containing elements of an interface type. You specify this in the same way as you would any Collection/Map. In versions before 1.1.0-beta-5 JPOX didn't support having more than a single class implementing the specified interface. With JPOX 1.1.0-beta-5 onwards you can have a Collection of interfaces as long as you use a join table relation and is unidirectional.