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
Arrays

JDO allows implementations to optionally support the persistence of arrays. JPOX provides full support for arrays in similar ways that collections are supported, but with the proviso that any changes in an array cannot be detected by JPOX, and so the whole array field needs updating. JPOX supports persisting arrays as

  • Single Column - the array is byte-streamed into a single column in the table of the containing object.
  • Serialised - the array is serialised into single column in the table of the containing object.
  • Using a Join Table - where the array relation is persisted into the join table, with foreign-key links to an element table where the elements of the array are PersistenceCapable
  • Using a Foreign-Key in the element - only available where the array is of a PersistenceCapable type

Single Column Arrays

Let's suppose you have a class something like this

So we have an Account and it has a number of permissions, each expressed as a byte. We want to persist the permissions in a single-column into the table of the account (but we don't want them serialised). We then define MetaData something like this

<class name="Account" identity-type="datastore">
    <field name="firstName">
        <column name="FIRST_NAME" length="100" jdbc-type="VARCHAR"/>
    </field>
    <field name="lastName">
        <column column="LAST_NAME" length="100" jdbc-type="VARCHAR"/>
    </field>
    <field name="permissions" column="PERMISSIONS">
        <array/>
    </field>
</class>

That is, you define the field as an array. This results in a datastore schema as follows

JPOX supports persistence of the following array types in this way : boolean[], byte[], char[], double[], float[], int[], long[], short[], Boolean[], Byte[], Character[], Double[], Float[], Integer[], Long[], Short[], BigDecimal[], BigInteger[]

See also :-



Serialised Arrays

Let's suppose you have a class something like this

So we have an Account and it has a number of permissions, each expressed as a byte. We want to persist the permissions as serialised into the table of the account. We then define MetaData something like this

<class name="Account" identity-type="datastore">
    <field name="firstName">
        <column name="FIRST_NAME" length="100" jdbc-type="VARCHAR"/>
    </field>
    <field name="lastName">
        <column column="LAST_NAME" length="100" jdbc-type="VARCHAR"/>
    </field>
    <field name="permissions" serialized="true" column="PERMISSIONS">
        <array/>
    </field>
</class>

That is, you define the field as an array, and that the field is serialized. To define arrays of short, long, int, or indeed any other supported array type you would do the same as above. This results in a datastore schema as follows

JPOX supports persistence of many array types in this way, including : boolean[], byte[], char[], double[], float[], int[], long[], short[], Boolean[], Byte[], Character[], Double[], Float[], Integer[], Long[], Short[], BigDecimal[], BigInteger[], String[], java.util.Date[], java.util.Locale[]

See also :-



Arrays persisted into Join Tables

JPOX will support arrays persisted into a join table. Let's take the example above and make the "permission" a class in its own right, so we have

So an Account has an array of Permissions, and both of these objects are PersistenceCapable. We want to persist the relationship using a join table. We define the MetaData as follows

<class name="Account" table="ACCOUNT">
    <field name="firstName">
        <column name="FIRST_NAME" length="100" jdbc-type="VARCHAR"/>
    </field>
    <field name="lastName">
        <column column="LAST_NAME" length="100" jdbc-type="VARCHAR"/>
    </field>
    <field name="permissions" table="ACCOUNT_PERMISSIONS">
        <array/>
        <join column="ACCOUNT_ID"/>
        <element column="PERMISSION_ID"/>
        <order column="PERMISSION_ORDER_IDX"/>
    </field>
</class>
<class name="Permission" table="PERMISSION">
    <field name="name"/>
</class>

This results in a datastore schema as follows



See also :-



Arrays persisted using Foreign-Keys

JPOX will support arrays persisted via a foreign-key in the element table. This is only applicable when the array is of a PersistenceCapable type. Let's take the same example above. So we have

So an Account has an array of Permissions, and both of these objects are PersistenceCapable. We want to persist the relationship using a foreign-key in the table for the Permission class. We define the MetaData as follows

<class name="Account" table="ACCOUNT">
    <field name="firstName">
        <column name="FIRST_NAME" length="100" jdbc-type="VARCHAR"/>
    </field>
    <field name="lastName">
        <column column="LAST_NAME" length="100" jdbc-type="VARCHAR"/>
    </field>
    <field name="permissions">
        <array/>
        <element column="ACCOUNT_ID"/>
        <order column="ACCOUNT_PERMISSION_ORDER_IDX"/>
    </field>
</class>
<class name="Permission" table="PERMISSION">
    <field name="name"/>
</class>

This results in a datastore schema as follows



See also :-