JPOX
JPOX
 Project  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Guides  |  Tools
1.1 | Preparation | O/R Mapping | Runtime | Extensions | Developer
Persistence
Object Identity
Meta-Data

MetaData is used to define how a class is to be persisted. It defines which classes are persistable, and which fields of these classes that are to be persisted. It also defines how the classes/fields are persisted. It is an essential part of a JDO system, and is used during the enhancement process as well as during the runtime process.

Java Persistent Objects JDO (JPOX) accepts standard JDO Meta-Data, and also a selection of vendor extensions. These are documented fully in the Meta-Data Reference Guide. It should be noted that the JPOX vendor extensions are optional and you can operate JPOX with no extensions.

MetaData is required for all classes that will be persistable (PersistenceCapable) as well as for any classes that directly update the fields of a persistable class (PersistenceAware). The majority of classes specified will be of the first type. Any classes that do directly update the fields of persistable classes will require a MetaData entry like

<class name="MyClass" persistence-modifier="persistence-aware"/>


File Locations

JDO (1.0.1 and 2.0) expects the Meta-Data to be specified in a file or files in particular positions in the file system. For example, if you have a class com.mycompany.sample.myexample, JDO will look for any of the following files until it finds one (in the order stated) :-

META-INF/package.jdo
WEB-INF/package.jdo
package.jdo
com/package.jdo
com/mycompany/package.jdo
com/mycompany/sample/package.jdo
com/mycompany/sample/myexample.jdo

In addition, for this example, JPOX allows the previous JDO 1.0.0 alternatives of

com.jdo
com/mycompany.jdo
com/mycompany/sample.jdo

In addition to the above, you can split your MetaData definitions between JDO MetaData files. For example if you have the following classes

com/mycompany/A.java
com/mycompany/B.java
com/mycompany/C.java
com/mycompany/app1/D.java
com/mycompany/app1/E.java
                

You could define the MetaData for these 5 classes in many ways -- for example put all class definitions in com/mycompany/package.jdo, or put the definitions for D and E in com/mycompany/app1/package.jdo and the definitions for A, B, C in com/mycompany/package.jdo, or have some in their class named MetaData files e.g com/mycompany/app1/A.jdo, or a mixture of the above. JPOX will always search for the MetaData file containing the class definition for the class that it requires.

File Types

JDO 1.0 requires the specification of all information in a jdo MetaData file specified in the above locations. JPOX supports this specification method.

JDO 2.0 allows the specification of either all information in a jdo MetaData file, or the basic JDO information in a jdo MetaData file AND all Object-Relational Mapping information in a separate ormfile (with suffix "orm"). JPOX supports these specification methods.

To make use of this JDO 2 Object-Relational Mapping file separation, you must specify the PersistenceManagerFactory property javax.jdo.option.Mapping. If you set this to, for example, mysql JPOX would look for files such as package.jdo and package-mysql.orm in the same locations as specified above.

You can read about the contents of the jdo MetaData file here, you can read about the contents of the orm MetaData file here, and you can read about the contents of the jdoquery MetaData file here.

Simple Example

Let us take a sample class and generate MetaData for it. Suppose I have a class as follows

package org.jpox.mysample;

public class Person
{
    /** Title of the Person. */
    String title=null;

    /** Forename of the Person. */
    String forename=null;

    /** Surname of the Person. */
    String surname=null;

    ...
}

and I want to use an existing schema. With this case I need to define the table and column names that it maps to. To do this I need to use JDO 2 ORM tags. So I come up with MetaData as follows in package.jdo

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jdo PUBLIC
    "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN"
    "http://java.sun.com/dtd/jdo_2_0.dtd">
<jdo>
<package name="org.jpox.mysample">
    <class name="Person" identity-type="datastore">
        <field name="title"/>
        <field name="forename"/>
        <field name="surname"/>
    </class>
</package>
</jdo>


and then I add the O/R mapping information in package-mysql.orm as

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE orm PUBLIC
    "-//Sun Microsystems, Inc.//DTD Java Data Objects Mapping Metadata 2.0//EN"
    "http://java.sun.com/dtd/jdo_orm_2_0.dtd">
<orm>
<package name="org.jpox.mysample">
    <class name="Person" table="PERSON">
        <field name="title">
            <column name="TITLE"/>
        </field>
        <field name="forename">
            <column name="FORENAME" length="100" jdbc-type="VARCHAR"/>
        </field>
        <field name="surname">
            <column name="SURNAME" length="100" jdbc-type="VARCHAR"/>
        </field>
    </class>
</package>
</orm>

So you see that our class is being mapped across to a table "PERSON" in the datastore, with columns "TITLE", "FORENAME", "SURNAME". We have also specified that the upper size limit on the forename and surname fields is 100.

Memory utilisation

The XML files are parsed and populated to memory the first time a pesistent operation is executed over a persistent class (e.g. pm.makePersistent(object) ). If the persistent class has relationships to other persistent classes, the metadata for the classes in the relationships are loaded. In addition to the persistent class and classes in the relationships, all other classes / files that were encountered while searching for the persistent classes are loaded, plus their relationships.

In average, for each persistent class a 3kb of memory is used to hold metadata information. This value will vary according the amount of metadata declared. Although this value can be used as reference in earlier stages of development, you should verify if it corresponds to your persistent classes.

A general formula can be used (with caution) to estimate the amount of memory required:

Amount Required = (# of persistent classes) * 3KB