Show TOC Start of Content Area

Procedure documentation Mapping Relationships  Locate the document in its SAP Library structure

Use

You use this procedure to create relationships between entities. JPA supports both bidirectional and unidirectional relationships. In contrast to EJB 2.1, bidirectional relationships are not automatically managed by the container but must be explicitly managed on both sides by the application. One side is the owning side of the relationship and the other side is the inverse side. The owning side determines the behavior of the relationship at runtime. A unidirectional relationship has only an owning side.

Prerequisites

You have:

      Created an EJB 3.0 Project

More information: Creating EJB 3.0 Projects

      Created your entities

More information: Creating Persistent Entities

      Opened the Java Persistence perspective

More information: Java Persistence Perspective

Procedure

Mapping One-to-One Unidirectional Relationships

...

To map a unidirectional one-to-one relationship, the owning entity’s database table must include a foreign key column with the IDs from the other entity’s database table.

Example

In a company, employees have profiles with address data. Each employee has only one profile and profiles do not reference employees. The Employee entity has the following fields:

@Entity

@Table(name="TMP_EDM_EMPLOYEE")

public classEmployee {

@Id

private int employeeId;

private String firstName;

private String lastName;

private Address address;

}

The PersonalData entity has the following fields:

@Entity

public class Address {

@Id

private int Id;

private int Street

private int City;

private int Telephone;

}

       1.      Select the address field of the Employee entity in the Persistence Outline.

       2.      On the General tab of the Persistence Properties, choose Map As One to One.

       3.      Enter the fully qualified name of the Address entity in the Target Entity field.

The SAP NetWeaver Developer Studio creates a default foreign key join column and updates the Employee entity source code with the @OnetoOne annotation. As the Address entity does not reference the Employee entity, no additional code is necessary there.

Mapping One-to-One Bidirectional Relationships

To make the relationship above bidirectional, you must include a field to reference the Employee entity in the Address entity, for example, privateEmployee employee.

...

       1.      Select the address field of the Employee entity in the Persistence Outline.

       2.      On the General tab of the Persistence Properties, choose Map As One to One.

       3.      Select the employee field of the Address entity in the Persistence Outline.

       4.      On the General tab of the Persistence Properties, choose Map As One to One.

       5.      Choose address from the Mapped By dropdown list.

The SAP NetWeaver Developer Studio creates default foreign key join columns for the two entities and updates the Employee entity source code with the @OnetoOne annotation and the Address entity source code with the @OnetoOne annotation with the mappedBy attribute.

Example

@OneToOne(mappedBy="address")

privateEmployee employee;

Mapping Many-to-One Unidirectional Relationships

Example

In a company, employees work in departments, that is, many employees are related to one department. Each employee works for only one department and departments do not reference employees.

...

       1.      The Employee entity is the owning side of the relationship. To map the owning side of the relationship, create a field to hold the foreign keys of the inverse side, for example, employeeDepartment.

       2.      Select the employeeDepartment field in the Persistence Outline.

       3.      On the Persistence Properties, choose Map As Many to One.

The SAP NetWeaver Developer Studio creates a default foreign key join column for the Employee entity and updates the Employee entity source code with the @ManytoOneannotation. As the Department entity does not reference the Employee entity, no additional code is necessary there.

Mapping One-to-Many and Many-to-One Bidirectional Relationships

Example

In a company, employees work in departments, that is, many employees are related to one department. If you create two entities to hold employee and department information respectively, both entities are aware of the relationship, that is, it is a bidirectional relationship. Thus, you manage the relationship on both the owning and the inverse side.

...

       1.      The Employee entity is the owning side of the relationship. To map the owning side of the relationship, create a field to hold the foreign keys of the inverse side, for example, employeeDepartment.

       2.      Select the employeeDepartment field in the Persistence Outline.

       3.      On the Persistence Properties, choose Map As Many to One.

       4.      On the Persistence Properties, choose the General tab. Enter the fully qualified name of the Department entity in the Target Entity field. Leave the rest of the fields with their default values.

This graphic is explained in the accompanying text

Mapping Many to One Relationships on the General Tab

       5.      Choose the Join Columns tab. The SAP NetWeaver Developer Studio provides the default mapping automatically.

       6.      Choose Override Default. Select the default entry and choose Edit. The Edit Join Column dialog opens.

       7.      In the Name field, enter DEPARTMENT_ID. Leave the Referenced Column Name field blank and choose OK.

This graphic is explained in the accompanying text

Mapping Many to One Relationships on the Join Columns Tab

The SAP NetWeaver Developer Studio updates the source code of the entity with the following:

Example

@ManyToOne(targetEntity=com.sap.demo.entities.Department.class)

@JoinColumn(name="DEPARTMENT_ID")

private int employeeDepartment;

       8.      To map the inverse side of the relationship, create a field in the Department entity to hold the employees working for the department, for example, employees.

       9.      Select the field in the Persistence Outline.

   10.      On the Persistence Properties, choose Map As One to Many. The SAP NetWeaver Developer Studio inserts the correct target entity automatically.

   11.      In the Mapped By field, enter the field to map to from the owning side, employeeDepartment.

   12.      From the Cascade Type dropdown list, choose All.

   13.      From the Fetch Type dropdown list, choose Eager.

This graphic is explained in the accompanying text

Mapping One to Many Relationships on the General Tab

The SAP NetWeaver Developer Studio updates the source code of the entity with the following:

Example

@OneToMany(mappedBy="employeeDepartment", fetch=FetchType.EAGER, cascade=CascadeType.ALL)

Mapping Many-to-Many Relationships

Each many-to-many relationship has two sides. On the owning side of the relationship, you designate a table to hold columns with foreign keys from both entities. The name element specifies the name of the table in the database. The column to insert in the join table from the owning side is always listed first with the joinColumns element. The value of the joinColumns element is the @JoinColumnannotation that specifies the name of the column from the owning entity. The inverse side is listed second with the inverseJoinColumns element. Again, the value of the element is specified by the @JoinColumn that lists the name of the column from the inverse side to be inserted in the join table.

Example

In a company, employees work on projects, that is, an employee references many project instances and a project references many employee instances. You create two entities to hold employee and project data respectively and both entities are aware of the relationship, that is, it is a bidirectional relationship. Thus, you manage the relationship on both the owning and the inverse side.

The Project entity:

...

@ManyToMany

@JoinTable(name = "TMP_EDM_EMP_PRJ", joinColumns = @JoinColumn(name = "PROJECT_ID"), inverseJoinColumns = @JoinColumn(name = "EMPLOYEE_ID"))

public List<Employee> getEmployees() {

return employees;

}

 

public voidsetEmployees(List<Employee> employees) {

this.employees = employees;

}

The Employee entity:

@ManyToMany(mappedBy = "employees", fetch=EAGER)

public List<Project> getProjectsByParticipant() {

return projectsByParticipant;

}

End of Content Area