15.15. The Complete Document
Prev | Chapter 15. Mapping Metadata | Next

## 15.15. The Complete Document

We began this chapter with the goal of mapping the following object model:

That goal has now been met. In the course of explaining JDOR's object-relational mapping metadata, we slowly built the requisite schema and mappings for the complete model. First, the database schema:

And finally, the complete JDOR mapping document:

Example 15.36. Full Mapping Document

    <?xml version="1.0"?>
    <orm>
        <package name="org.mag">
            <sequence name="ArticleSeq" datastore-sequence="ART_SEQ"/>
            <class name="Magazine" table="MAG">
                <version column="VERS" strategy="version-number"/>
                <field name="isbn">
                    <column name="ISBN" jdbc-type="char" length="15"/>
                </field>
                <field name="title" column="TITLE"/>
                <field name="price" column="PRICE"/>
                <field name="copiesSold" column="COPIES"/>
                <field name="coverArticle" column="COVER_ID" delete-action="restrict"/>
                <field name="articles" table="MAG_ARGS">
                    <join delete-action="cascade">
                        <column name="MAG_ISBN" target="ISBN"/>
                        <column name="MAG_TITLE" target="TITLE"/>
                    </join>
                    <element column="ART_ID" delete-action="restrict"/>
                </field>
                <field name="publisher" column="PUB_ID" delete-action="restrict"/>
            </class>
            <class name="Article" table="ART">
                <version column="UPD" strategy="date-time"/>
                <field name="id" column="ID" sequence="ArticleSeq"/>
                <field name="title" column="TITLE" unique="true"/>
                <field name="content" column="CONTENT"/>
                <field name="authors" table="ART_AUTHS">
                    <join column="ART_ID" delete-action="cascade"/>
                    <key column="LNAME"/>
                    <value column="AUTH_ID" delete-action="restrict"/>
                </field>
                <field name="subtitles" table="ART_SUBS">
                    <join column="ART_ID" delete-action="cascade"/>
                    <element column="SUBTITLE"/>
                    <order column="ORD"/>
                </field>
            </class>
        </package>
        <package name="org.mag.pub">
            <sequence name="AuthorSeq" factory-class="Author$SequenceFactory"/>
            <class name="Company" table="COMP">
                <datastore-identity column="CID" strategy="autoassign"/>
                <version strategy="none"/>
                <field name="name" column="NAME" unique="true"/>
                <field name="revenue" column="REV"/>
                <field name="address">
                    <embedded null-indicator-column="STREET">
                        <field name="street" column="STREET"/>
                        <field name="city" column="CITY"/>
                        <field name="state">
                            <column name="STATE" jdbc-type="char" length="2"/>
                        </field>
                        <field name="zip" column="ZIP" indexed="true"/>
                    </embedded>
                </field>
                <field name="subscriptions" table="COMP_SUBS">
                    <join column="COMP_ID" delete-action="cascade"/>
                    <element column="SUB_ID" delete-action="restrict"/>
                </field>
                <field name="mags" mapped-by="publisher"/>
            </class>
            <class name="Author" table="AUTH">
                <datastore-identity sequence="AuthorSeq">
                    <column name="AID" sql-type="INTEGER64"/>
                </datastore-identity>
                <inheritance>
                    <discriminator column="CLS" strategy="class-name"/>
                </inheritance>
                <version strategy="state-comparison"/>
                <field name="firstName" column="FNAME"/>
                <field name="lastName" column="LNAME" indexed="true"/>
                <field name="address">
                    <embedded null-indicator-column="STREET">
                        <field name="street" column="STREET"/>
                        <field name="city" column="CITY"/>
                        <field name="state">
                            <column name="STATE" jdbc-type="char" length="2"/>
                        </field>
                        <field name="zip" column="ZIP" indexed="true"/>
                    </embedded>
                </field>
            </class>
        </package>
        <package name="org.mag.subscribe">
            <sequence name="ContractSeq" strategy="transactional"/>
            <class name="Contract">
                <inheritance strategy="subclass-table"/>
            </class>
            <class name="Subscription" table="CNTRCT.SUB">
                <inheritance>
                    <discriminator value="1">
                        <column name="TYPE" jdbc-type="tinyint"/>
                    </discriminator>
                </inheritance>
                <version column="VERS" strategy="version-number"/>
                <field name="Contract.id" column="ID"/>
                <field name="startDate" column="START"/>
                <field name="payment" column="PAY"/>
                <field name="items">
                    <element column="SUB_ID"/>
                </field>
            </class>
            <class name="LifetimeSubscription">
                <inheritance>
                    <discriminator value="2"/>
                </inheritance>
                <field name="eliteClub" column="ELITE"/>
            </class>
            <class name="TrialSubscription" table="CNTRCT.TRIAL_SUB">
                <inheritance strategy="new-table">
                    <join column="ID" delete-action="restrict"/>
                    <discriminator value="3"/>
                </inheritance>
                <field name="endDate" column="END"/>
            </class>
            <class name="Subscription$LineItem" table="CNTRCT.LINE_ITEM">
                <version column="VERS" strategy="version-number"/>
                <field name="Contract.id" column="ID"/>
                <field name="price" column="PRICE"/>
                <field name="num" column="NUM" value-strategy="autoassign"/>
                <field name="magazine" delete-action="restrict">
                    <column name="MAG_ISBN" target="ISBN"/>
                    <column name="MAG_TITLE" target="TITLE"/>
                </field>
            </class>
        </package>
    </orm>

Until now, we have focused on mapping metadata in the `.orm` file format. As we mentioned at the beginning of this chapter, however, you can integrate mapping metadata right into your `.jdo` file instead. Here are the above mappings integrated into our persistent class' JDO metadata:

Example 15.37. Integrated JDO Metadata Document

    <?xml version="1.0"?>
    <jdo>
        <package name="org.mag">
            <sequence name="ArticleSeq" datastore-sequence="ART_SEQ"/>
            <class name="Magazine" objectid-class="Magazine$MagazineId" table="MAG">
                <version column="VERS" strategy="version-number"/>
                <field name="isbn" primary-key="true">
                    <column name="ISBN" jdbc-type="char" length="15"/>
                </field>
                <field name="title" column="TITLE" primary-key="true"/>
                <field name="price" column="PRICE"/>
                <field name="copiesSold" column="COPIES"/>
                <field name="coverArticle" column="COVER_ID" delete-action="restrict"/>
                <field name="articles" table="MAG_ARGS">
                    <collection element-type="Article" dependent-element="true"/>
                    <join delete-action="cascade">
                        <column name="MAG_ISBN" target="ISBN"/>
                        <column name="MAG_TITLE" target="TITLE"/>
                    </join>
                    <element column="ART_ID" delete-action="restrict"/>
                </field>
                <field name="publisher" column="PUB_ID" delete-action="restrict"/>
                <fetch-group name="detail">
                    <field name="publisher" fetch-depth="0"/>
                    <field name="articles" fetch-depth="0"/>
                </fetch-group>
            </class>
            <class name="Article" identity-type="application" detachable="true" table="ART">
                <version column="UPD" strategy="date-time"/>
                <field name="id" primary-key="true" column="ID" sequence="ArticleSeq"/>
                <field name="title" column="TITLE" unique="true"/>
                <field name="content" column="CONTENT"/>
                <field name="authors" table="ART_AUTHS">
                    <map key-type="String" value-type="org.mag.pub.Author"/>
                    <join column="ART_ID" delete-action="cascade"/>
                    <key column="LNAME"/>
                    <value column="AUTH_ID" delete-action="restrict"/>
                </field>
                <field name="subtitles" table="ART_SUBS">
                    <collection element-type="String"/>
                    <join column="ART_ID" delete-action="cascade"/>
                    <element column="SUBTITLE"/>
                    <order column="ORD"/>
                </field>
            </class>
        </package>
        <package name="org.mag.pub">
            <sequence name="AuthorSeq" factory-class="Author$SequenceFactory"/>
            <class name="Company" table="COMP">
                <datastore-identity column="CID" strategy="autoassign"/>
                <version strategy="none"/>
                <field name="name" column="NAME" unique="true"/>
                <field name="revenue" column="REV"/>
                <field name="address" embedded="true">
                    <embedded null-indicator-column="STREET">
                        <field name="street" column="STREET"/>
                        <field name="city" column="CITY"/>
                        <field name="state">
                            <column name="STATE" jdbc-type="char" length="2"/>
                        </field>
                        <field name="zip" column="ZIP" indexed="true"/>
                    </embedded>
                </field>
                <field name="subscriptions" table="COMP_SUBS">
                    <collection element-type="org.mag.subscribe.Subscription"/>
                    <join column="COMP_ID" delete-action="cascade"/>
                    <element column="SUB_ID" delete-action="restrict"/>
                </field>
                <field name="mags" mapped-by="publisher">
                    <collection element-type="org.mag.Magazine"/>
                </field>
            </class>
            <class name="Author" table="AUTH">
                <datastore-identity sequence="AuthorSeq">
                    <column name="AID" sql-type="INTEGER64"/>
                </datastore-identity>
                <inheritance>
                    <discriminator column="CLS" strategy="class-name"/>
                </inheritance>
                <version strategy="state-comparison"/>
                <field name="firstName" column="FNAME"/>
                <field name="lastName" column="LNAME" indexed="true"/>
                <field name="address" embedded="true">
                    <embedded null-indicator-column="STREET">
                        <field name="street" column="STREET"/>
                        <field name="city" column="CITY"/>
                        <field name="state">
                            <column name="STATE" jdbc-type="char" length="2"/>
                        </field>
                        <field name="zip" column="ZIP" indexed="true"/>
                    </embedded>
                </field>
            </class>
            <class name="Address" embedded-only="true"/>
        </package>
        <package name="org.mag.subscribe">
            <sequence name="ContractSeq" strategy="transactional"/>
            <class name="Contract" identity-type="application">
                <inheritance strategy="subclass-table"/>
                <field name="id" primary-key="true"/>
            </class>
            <class name="Subscription" detachable="true" table="CNTRCT.SUB">
                <inheritance>
                    <discriminator value="1">
                        <column name="TYPE" jdbc-type="tinyint"/>
                    </discriminator>
                </inheritance>
                <version column="VERS" strategy="version-number"/>
                <field name="Contract.id" column="ID"/>
                <field name="startDate" column="START"/>
                <field name="payment" column="PAY"/>
                <field name="items">
                    <collection element-type="Subscription$LineItem" dependent-element="true"/>
                    <element column="SUB_ID"/>
                    <extension vendor-name="kodo" key="lock-group" value-"none"/>
                </field>
            </class>
            <class name="LifetimeSubscription">
                <inheritance>
                    <discriminator value="2"/>
                </inheritance>
                <field name="eliteClub" column="ELITE"/>
            </class>
            <class name="TrialSubscription" table="CNTRCT.TRIAL_SUB">
                <inheritance strategy="new-table">
                    <join column="ID" delete-action="restrict"/>
                    <discriminator value="3"/>
                </inheritance>
                <field name="endDate" column="END"/>
            </class>
            <class name="Subscription$LineItem" table="CNTRCT.LINE_ITEM">
                <version column="VERS" strategy="version-number"/>
                <field name="Contract.id" column="ID"/>
                <field name="price" column="PRICE"/>
                <field name="num" column="NUM" value-strategy="autoassign"/>
                <field name="magazine" delete-action="restrict">
                    <column name="MAG_ISBN" target="ISBN"/>
                    <column name="MAG_TITLE" target="TITLE"/>
                </field>
            </class>
        </package>
    </jdo>

Prev | Up | Next
15.14. Unique Constraints | Home | Chapter 16. Sequence