JPOX
JPOX
 JPOX Version 1.0
Configuration | Tutorials | Worked Examples | Developer
Examples
Examples : Normal Collection - MetaData for Existing 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 an existing database schema where we already have some database tables from a previous persistence mechanism and we want to use those tables (because they have data in them). Our existing tables are shown below.



We will take the Meta-Data that was described in the previous section (New Schema) and continue from there. To recap, here is what we arrived at

<?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>
    <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"/>
            </field>
        </class>
        <class name="Card" identity-type="application"
           objectid-class="org.jpox.examples.Card$CardKey">
            <field name="suit" primary-key="true">
                <extension vendor-name="jpox" key="length" value="max 10"/>
            </field>
            <field name="number" primary-key="true">
                <extension vendor-name="jpox" key="length" value="max 20"/>
            </field>
        </class>
    </package>
</jdo>
            

The first thing we need to do is map the Pack class to the table that we have in our database. It needs to be mapped to a table called "DECK", with columns "IDENTIFIERNAME" and "DETAILS", and the identity column that JPOX uses needs to be called IDENTIFIER_ID. We do this by changing the Meta-Data to be

        <class name="Pack" identity-type="datastore">
            <extension vendor-name="jpox" key="table-name" value="DECK"/>
            <extension vendor-name="jpox" key="key-column-name" value="IDENTIFIER_ID"/>
            <field name="name" persistence-modifier="persistent">
                <extension vendor-name="jpox" key="column-name" value="IDENTIFIERNAME"/>
                <extension vendor-name="jpox" key="length" value="max 100"/>
            </field>
            <field name="description" persistence-modifier="persistent">
                <extension vendor-name="jpox" key="column-name" value="DETAILS"/>
                <extension vendor-name="jpox" key="length" value="max 255"/>
            </field>
            <field name="cards" persistence-modifier="persistent">
                <collection element-type="org.jpox.examples.Card"/>
            </field>
        </class>
            

So we made use of the JPOX extension tags table-name and column-name to align to the table that is there. In addition we made use of the JPOX extension tag key-column-name to map the identity column name. Lets now do the same for the class Card. In our database we want this to map to a table called "PLAYINGCARD", with columns "SET" and "VALUE". So we do the same thing to its Meta-Data

        <class name="Card" identity-type="application"
           objectid-class="org.jpox.examples.Card$CardKey">
           <extension vendor-name="jpox" key="table-name" value="PLAYINGCARD"/>
            <field name="suit" primary-key="true">
                <extension vendor-name="jpox" key="column-name" value="SET"/>
                <extension vendor-name="jpox" key="length" value="max 10"/>
            </field>
            <field name="number" primary-key="true">
                <extension vendor-name="jpox" key="column-name" value="VALUE"/>
                <extension vendor-name="jpox" key="length" value="max 20"/>
            </field>
        </class>
            

OK, so we've now mapped our 2 classes to their tables. The only remaining thing is that to form our relationship with the "Normal Relationship" we have a join table and in our database this is called "DECKOFCARDS" with columns "SET_ID", "VALUE_ID" and "DECK_ID". To map to these we need to further modify the Meta-Data for Pack as follows

        <class name="Pack" identity-type="datastore">
            <extension vendor-name="jpox" key="table-name" value="DECKOFCARDS"/>
            <field name="name" persistence-modifier="persistent">
                <extension vendor-name="jpox" key="column-name" value="IDENTIFIERNAME"/>
                <extension vendor-name="jpox" key="length" value="max 100"/>
            </field>
            <field name="description" persistence-modifier="persistent">
                <extension vendor-name="jpox" key="column-name" value="DETAILS"/>
                <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="table-name" value="DECKOFCARDS"/>
                    <extension vendor-name="jpox" key="owner-column-name" value="DECK_ID"/>
                    <extension vendor-name="jpox" key="element-column-name" value="SET_ID,VALUE_ID"/>
                </collection>
            </field>
        </class>
            

So we've now updated the cards field to use the JPOX extension tags table-name, owner-column-name, element-column-name and have a mapping to that table as well now. Please note the use of the comma-separated list of column-names where we have a composite primary key on Card. This completes our job. The only other aspect that is likely to be met is where a column in the database is of a particular type, but we'll cover that in a different example.