JPOX
JPOX
 JPOX Version 1.0
Configuration | Tutorials | Worked Examples | Developer
Examples
Examples : Inverse Collection : MetaData for New Schema

Now that we've decided on our classes and how we want to define their identities we can decide on the precise persistence definition. In this section we'll describe how to persist these objects to a new database schema where we can create new tables and don't need to write to some existing table.

Some JDO tools provide an IDE to generate Meta-Data files, but JPOX doesn't currently. Either way it is a good idea to become familiar with the structure of these files since they define how your classes are persisted. Lets start with the header area. You add a block like this to define that the file is JDO Meta-Data

<?xml version="1.0"?>
<!DOCTYPE jdo PUBLIC "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN" 
    "http://java.sun.com/dtd/jdo_1_0.dtd">
<jdo>
            

Now lets define the persistence for our Pack class. We are going to use datastore identity here, meaning that JPOX will assign id's to each Pack object persisted. We define it as follows

    <package name="org.jpox.examples">
        <class name="Pack" identity-type="datastore">
            <field name="name" persistence-modifier="persistent">
                <extension vendor-name="jpox" key="length" value="max 100"/>
            </field>
            <field name="description" persistence-modifier="persistent">
                <extension vendor-name="jpox" key="length" value="max 255"/>
            </field>
            <field name="cards" persistence-modifier="persistent">
                <collection element-type="org.jpox.examples.Card">
                    <extension vendor-name="jpox" key="owner-field" value="pack"/>
                </collection>
            </field>
        </class>
            

Here we've defined that our name field will be persisted to a VARCHAR(100) column, our description field will be persisted to a VARCHAR(255) column, and that our cards field is a Collection containing org.jpox.examples.Card objects. In addition, it specifies that there is a pack field in the Card class that gives the related pack (with the Pack being the owner of the relationship). This final information is to inform JPOX to link the table for this class (via a foreign key) to the table for Card class. This is what is termed an Inverse relationship. Please refer to the 1-N Relationships Guide for more details on this. We'll discuss Normal relationships in a different example.

Now lets define the persistence for our Card class. We are going to use datastore identity here, meaining that JPOX will assign the id's for any object of type Card. We define it as follows

        <class name="Card" identity-type="datastore">
            <field name="suit">
                <extension vendor-name="jpox" key="length" value="max 10"/>
            </field>
            <field name="number">
                <extension vendor-name="jpox" key="length" value="max 20"/>
            </field>
            <field name="pack">
                <extension vendor-name="jpox" key="collection-field" value="cards"/>
            </field>
        </class>
    </package>
            

Here we've defined that our suit field will be persisted to a VARCHAR(10) column, our number field will be persisted to a VARCHAR(20) column. In addition we specify that this class/table is the collection-field end of a 1-N Inverse relationship, linked to the cards field on the Pack class.

We finally terminate the Meta-Data file with the closing tag

</jdo>
            
Continue on to the next part of this JPOX Example