3. Defining relationships

PROBLEM

One of the basic principles of object-oriented development is that objects can be associated with other objects. There needs to be a mechanism for defining relationships between domain objects.

SOLUTION

The domain object editor allows you to define relationships to a domain object.

Relationship Type

The Relationship Type defines the cardinality of the relationship from the perspective of the current Domain Object. The options are:

Table 4.2. Relationship Types

TypeDescription
One-to-Onea domain object is related to a single occurrence of the other domain object
One-to-Manya domain object is related to many occurrences of the other domain object; the opposite side of a Many-to-One relationship
Many-to-Onea domain object is related to a single occurrence of the other domain object; the opposite side of a One-to-Many relationship
Many-to-Manya domain object is related to many occurrences of the other domain object, and vice versa

Bidirectional Relationships

When a relationship is added to another domain object, a unidirectional relationship is implied. Two independent unidirectional relationships can be linked together to create a bidirectional relationship. If you decide to define a bidirectional relationship, the cardinality and relationship names need to match.

Relationships Names

When defining a relationship between two data objects, the Domain Object editor will prompt you for a relationship name and a reverse name. These names will be used for accessing each other's related data. The relationship name is used to identify the relationship from the perspective of the current domain object. The reverse name is used to identify the relationship from the target data object type back to the current domain object.

Regarding relationship names, the Domain Object editor will default the domain object name as the relationship names, but you can very easily changed the name. It's a good idea to change these relationship names to something more semantically meaningful. While they have no impact on the functionality, these names will be the what you will see as your referencing these relationships in the services you create. These names should be meaningful, and it's recommended that you name them with the appropriate plurality for the relationship type.

HOW IT WORKS

The Relationship section of the domain object editor is used for defining relationships between domain objects. The following Book and Author example shows a many-to-many relationship between Book and Author domain objects.

From the perspective of the Book domain object, it has a many relationship with the Author domain object. The relationship name is authors.

Figure 4.2. Book domain object with relationship to Authors

Book domain object with relationship to Authors

From the perspective of the Author domain object, it has a many relationship with the Book domain object. The relationship name is books.

Figure 4.3. Author domain object with reverse relationship to Book

Author domain object with reverse relationship to Book

A fragment of the code generated for the Author domain object is shown below. It shows how the relationship to the Books domain object manifested in code.

Example 4.3. Code generated for domain object relationships

package org.bookapp.domain;

public class Author implements Serializable{

   /**
   * 
   * @generated
   */
   private String ID;
   private String firstName;
   private String lastName;
   private String title;
   private Set<Book> books;1

   // Constructors, Getters and Setters
   ...
}

1

The books relationship defined in the domain object is generated into a Set.