![]() | ![]() |
![]() |
| Project | Ver 1.1 | Ver 1.2 | JDO | JPA | Guides | Tools |
| 1.2 | Persistence | JDO ORM | JPA ORM | Runtime | JDO Runtime | JPA Runtime | Extensions | Developer |
![]() You have a 1-to-1 relationship when an object of a class has an associated object of another class (only one associated object). It could also be between an object of a class and another object of the same class (obviously). You can create the relationship in 2 ways depending on whether the 2 classes know about each other (bidirectional), or whether only one of the classes knows about the other class (unidirectional). These are described below. The various possible relationships are described below.
For this case you could have 2 classes, User and Account, as below.
<package name="mydomain">
<class name="User" table="USER">
<field name="id" primary-key="true">
<column name="USER_ID"/>
</field>
<field name="login">
<column name="LOGIN" length="20" jdbc-type="VARCHAR"/>
</field>
</class>
<class name="Account" table="ACCOUNT">
<field name="id" primary-key="true">
<column name="ACCOUNT_ID"/>
</field>
<field name="firstName">
<column name="FIRSTNAME" length="50" jdbc-type="VARCHAR"/>
</field>
<field name="secondName">
<column name="SECONDNAME" length="50" jdbc-type="VARCHAR"/>
</field>
<field name="user" persistence-modifier="persistent">
<column name="USER_ID"/>
</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.
Things to note :-
For this case you could have 2 classes, User and Account again, but this time as below. Here the Account class knows about the User class, and also vice-versa. ![]() Here we create the 1-1 relationship with a single foreign-key. To do this you define the MetaData as
<package name="mydomain">
<class name="User" table="USER">
<field name="id" primary-key="true">
<column name="USER_ID"/>
</field>
<field name="login">
<column name="LOGIN" length="20" jdbc-type="VARCHAR"/>
</field>
<field name="account" persistence-modifier="persistent" mapped-by="user">
</field>
</class>
<class name="Account" table="ACCOUNT">
<field name="id" primary-key="true">
<column name="ACCOUNT_ID"/>
</field>
<field name="firstName">
<column name="FIRSTNAME" length="50" jdbc-type="VARCHAR"/>
</field>
<field name="secondName">
<column name="SECONDNAME" length="50" jdbc-type="VARCHAR"/>
</field>
<field name="user" persistence-modifier="persistent">
<column name="USER_ID"/>
</field>
</class>
</package>The difference is that we added mapped-by to the field of User. This will create 2 tables in the database, one for User (with name USER), and one for Account (with name ACCOUNT including a USER_ID). The fact that we specified the mapped-by on the User class means that the foreign-key is created in the ACCOUNT table. ![]() Things to note :-
The above 2 relationship types assume that both classes in the 1-1 relation will have their own table. You can, of course, embed the elements of one class into the table of the other. This is described in Embedded PC Objects. |