Part I. Associations

The three example apps that we have discussed in Volume 1, the minimal app, the validation app, and the enumeration app, have been limited to managing the data of one object type only. A real app, however, has to manage the data of several object types, which are typically related to each other in various ways. In particular, there may be associations and subtype (inheritance) relationships between object types. Handling associations and subtype relationships are advanced issues in software application engineering. They are often not sufficiently discussed in text books and not well supported by application development frameworks.

Associations are important elements of information models. Software applications have to implement them in a proper way, especially in their model layer within a model-view-controller (MVC) architecture. Unfortunately, application development frameworks do often not provide much support for dealing with associations.

There are two important distinctions, which are independent of each other:

  1. Functional versus non-functional ("many-to-many") associations.

  2. Unidirectional versus bidirectional associations.

A functional association corresponds to what is called a function in mathematics, while a many-to-many association corresponds to what is called a relation in mathematics.

A unidirectional association corresponds to a reference property in OO modeling and programming, while a bidirectional association corresponds to a pair of mutually inverse reference properties.

Table of Contents
1. Reference Properties and Unidirectional Associations
1. References and Reference Properties
2. Referential Integrity
3. Modeling Reference Properties as Unidirectional Associations
4. Representing Unidirectional Associations as Reference Properties
5. Adding Directionality to a Non-Directed Association
6. Our Running Example
7. Eliminating Unidirectional Associations
7.1. The basic elimination procedure
7.2. Eliminating associations from the design model
8. Rendering Reference Properties in the User Interface
9. Collection Types for Multi-Valued Reference Properties
10. Quiz Questions
10.1. Question 1: Meaning of "one-to-one"
10.2. Question 2: When to Check Referential Integrity
10.3. Question 3: Meaning of "Functional"
10.4. Question 4: Object Destruction Dependency
10.5. Question 5: Rendering a Single-Valued Reference Property
10.6. Question 6: Expressing a Functionality Type
2. Unidirectional Functional Associations in Plain JS
1. New JavaScript Elements
1.1. Block-Scope Variable Declarations with let and const
1.2. Arrow Functions
1.3. For-Of Loops over Iterable Objects
1.4. Template Literals
1.5. The Spread Operator
1.6. Destructuring Assignments
2. Implementing Single-Valued Reference Properties
3. Make a JS Class Model
4. New Issues
5. Code the Model
5.1. Summary
5.2. Code each model class as an ES2015 class
5.3. Code the property checks
5.4. Code the property setters
5.5. Choose and implement a deletion policy
5.6. Serialization and Object-to-Storage Mapping
6. Code the View and Controller
6.1. Initialize the app
6.2. Showing associated objects in Retrieve/List All
6.3. Selecting associated objects in the Create and Update use cases
7. Quiz Questions
7.1. Question 1: JS class model
3. Unidirectional Non-Functional Associations in Plain JS
1. Implementing Multi-Valued Reference Properties
2. Make a JS Class Model
3. New issues
4. Code the Model
4.1. Summary
4.2. Code the add and remove operations
4.3. Choose and implement a deletion policy
4.4. Serialization and Object-to-Storage Mapping
5. Code the View
5.1. Showing information about associated objects in Retrieve/List All
5.2. Selecting associated objects in the Create use case
5.3. Selecting associated objects in the Update use case
6. How to Run the App and Get the Code
7. Points of Attention
4. Unidirectional Functional Associations in Java EE
1. Implementing Single-Valued Reference Properties in Java
2. Make a Java Entity Class Model
3. New Issues
4. Write the Model Code
4.1. Summary
4.2. Code each class of the Entity class model
4.3. Code the constraints
4.4. Code getters and setters
4.5. Implement a deletion policy
4.6. Serialization and De-Serialization
5. The View and Controller Layers
5.1. Initialize the app
5.2. Show information about associated objects in Retrieve/List All
5.3. Allow selecting associated objects in Create and Update
5.3.1. Create the Object-to-String converter
5.3.2. Write the view code
5. Unidirectional Non-Functional Associations with Java EE
1. Implementing Multi-Valued Reference Properties in Java
2. Make a Java Entity Class Model
3. New issues
4. Write the Model Code
4.1. Summary
4.2. Code each class of the Java Entity class model
4.3. Implement a deletion policy
4.4. Serialization and De-Serialization
5. Write the User Interface Code
5.1. Show information about associated objects in Retrieve/List All
5.2. Allow selecting associated objects in Create
6. Run the App and Get the Code
7. Sets versus Ordered Sets as the Values of Reference Properties
6. Unidirectional Associations Practice Projects
1. Assign a director and actors to a movie
2. Assign cities and a capital to a country, and members to an organization
7. Bidirectional Associations
1. Inverse Reference Properties
2. Making an OO Class Model
2.1. The basic procedure
2.2. How to eliminate unidirectional associations
2.3. How to eliminate bidirectional associations
2.4. The resulting OO class model
8. Bidirectional Associations in Plain JS
1. Make a JavaScript Class Model
2. Write the Model Code
2.1. New issues
2.2. Coding Summary
2.3. Code each class of the JS class model
2.4. Code the set methods of single-valued properties
2.5. Code the add and remove operations
2.6. Suppress the storage of the values of derived properties
2.7. Take care of deletion dependencies
3. Exploit Inverse Reference Properties in the User Interface
3.1. Show information about published books in Retrieve/List All
9. Implementing Bidirectional Associations with Java EE
1. Make a Java Entity Class Model
2. Write the Model Code
2.1. New issues
2.2. Summary
2.3. Code each class of the Java Entity class model
2.4. Code the setter operations
2.5. Code the add and remove operations
2.6. Take care of deletion dependencies
2.7. Entity Managers and Cached Entities
3. Exploiting Derived Inverse Reference Properties in the User Interface
3.1. Show information about published books in Retrieve/List All
4. Run the App and Get the Code
10. Special Topics on Associations
1. Part-Whole Associations
1.1. Aggregation
1.2. Composition
1.3. Example
2. Association Classes
11. Bidirectional Associations Practice Projects
1. Assign directors/actors to movies and movies to directors/actors
2. Assign cities to a country and members to an organization, and vice versa