Create one-to-many mappings

Use a One-to-Many Mapping to define a relationship from an attribute to another class, with one-to-many multiplicity to a database column. In the Tutorial Object Model, the phoneNumbers field of the Employee class has a one-to-many relationship to the Phone class; each employee may have many phone numbers.

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

  2. In the JPA Structure view, select the phoneNumber 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-Many.

    One-to-many Mapping for phoneNumbers

    This figure shows the Persistence Properties page for the phoneNumbers field.
  4. Select PhoneNumber as the Target Entity. Leave the other fields with their default values.

  5. In the Join Table area, notice that Dali has selected the correct joins, based on the foreign key associations in the database tables.

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

JPA Structure for Employee Entity

This figure shows the phoneNumbers attribute of the Employee entity in the Persistence Outline view.

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