Create many-to-one mappings

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

This is the "back mapping" of the one-to-many mapping you previously defined.

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

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

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

    Many to One Mapping for owner

    The JPA Details view for the owner field.
  4. Leave the other fields with their default values. Dali correctly completes the information based on the database structure and previously defined mappings.

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

  6. Select the Override Default option.

  7. Select the owner_EMP_ID -> EMP_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: EMP_ID (from the PHONE table)

    • Referenced Column Name: EMP_ID (from the EMPLOYEE table)

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

JPA Structure for PhoneNumber Entity

The owner attribute of the PhoneNumber entity in the JPA Structure view.

Next, you will Create version mappings