Create one-to-one mappings

Use a One-to-One Mapping to define a relationship from an attribute to another class, with one-to-one multiplicity to a database column. In the Tutorial Object Model, the address field of the Employee class has a one-to-one relationship to the Address class; each employee may have a single address.

  1. Select the Employee entity in the Package Explorer view.

  2. In the JPA Structure view, select the address field of the Employee entity. The JPA Details view (for attributes) displays the properties for the field.

  3. In the Map As field, select One-to-One.

    One-to-one Mapping for address

    JPA Details view for the address field.
  4. For the Target Entity, click Browse and select the Address persistent entity. Dali adds the @OneToOne(targetEntity=dali.tutorial.model.Address.class) entity to the class.

    Leave the other fields with their default values.

  5. Select the Join Columns tab to specify the relationship between the Employee and Address entities. Because you had to explicitly define the ID field for the Address entity in its ID mapping, you will need to edit the default join relationship.

  6. Select the Override Default option.

  7. Select the address_ADDRESS_ID -> ADDRESS_ID relationship in the Join Columns area and click Edit.

  8. In the Edit Join Column dialog, select the following options and click OK.

    • Name: ADDR_ID (from the EMPLOYEE table)

    • Referenced Column Name: ADDRESS_ID (from the ADDRESS table)

    Editing Join Column for Address Mapping

    Edit Join Column dialog for the address 1:1 mapping.

In the JPA Structure, the address field is identified as a one-to-one mapping, as shown in the following figure:

JPA Structure for Employee Entity

The address attribute of the Employee entity in the JPA Structure view.

Next, you will Create one-to-many mappings.