JPOX
JPOX
 JPOX Version 1.0
Configuration | Tutorials | Worked Examples | Developer
Obtaining JPOX
JPOX Preparation
JPOX Runtime
JDO Relationships : 1-to-1

You have a 1-to-1 relationship when an object of a class has an associated object of another class (only one associated object). You can create the relationship in 2 ways depending on whether the 2 classes know about each other (double-ended), or whether only one of the classes knows about the other class (single-ended). These are described below.

Single-Ended

For this case you could have 2 classes, User and Account, as below.



so the Account class knows about the User class, but not vice-versa. If you define the Meta-Data for these classes as follows

<package name="mydomain">
    <class name="User" identity-type="datastore">
        <field name="login" persistence-modifier="persistent">
            <extension vendor-name="jpox" key="length" value="max 20"/>
        </field>
    </class>

    <class name="Account" identity-type="datastore">
        <field name="firstName" persistence-modifier="persistent">
            <extension vendor-name="jpox" key="length" value="max 50"/>
        </field>
        <field name="secondName" persistence-modifier="persistent">
            <extension vendor-name="jpox" key="length" value="max 50"/>
        </field>
        <field name="user" persistence-modifier="persistent">
        </field>
    </class>
</package>
                

This will create 2 tables in the database, one for User (with name USER), and one for Account (with name ACCOUNT and a column USER_ID), as shown below.



[If you wish to specify the names of the database tables and columns for these classes, you can use the JPOX extension MetaData tags table-name and column-name].

Double-Ended

For this case you could have 2 classes, User and Account again, but this time as below.



so the Account class knows about the User class, and also vice-versa. If you define the Meta-Data for these classes as follows

<package name="mydomain">
    <class name="User" identity-type="datastore">
        <field name="login" persistence-modifier="persistent">
            <extension vendor-name="jpox" key="length" value="max 20"/>
        </field>
        <field name="account" persistence-modifier="persistent">
        </field>
    </class>

    <class name="Account" identity-type="datastore">
        <field name="firstName" persistence-modifier="persistent">
            <extension vendor-name="jpox" key="length" value="max 50"/>
        </field>
        <field name="secondName" persistence-modifier="persistent">
            <extension vendor-name="jpox" key="length" value="max 50"/>
        </field>
        <field name="user" persistence-modifier="persistent">
        </field>
    </class>
</package>
                

This will create 2 tables in the database, one for User (with name USER including a ACCOUNT_ID to link to the ACCOUNT table), and one for Account (with name ACCOUNT including a USER_ID).



[If you wish to specify the names of the database tables and columns for these classes, you can use the JPOX extension MetaData tags table-name and column-name].

Please note that you should use this relationship with care. This is because when you insert the first object in the relationship, your other object will not exist, and so you should insert the first object without the link to the other object. Once this object is inserted, you can insert the other end and establish the link (since both objects exist in the datastore and the foreign key relationships will be valid at that point).