Web Applications with JavaScript or Java

Volume 2: Associations and Class Hierarchies

Gerd Wagner

Ion Mircea Diaconescu

This book will be published by De Gruyter Oldenbourg in April 2021 with the ISBN: 978-3-11-050024-0. It is also available as a living document in the form of an open access online (HTML) book from http://web-engineering.info/WebAppTextbook.

This book, along with any associated source code, is licensed under The Code Project Open License (CPOL), implying that the associated code is provided "as-is", can be modified to create derivative works, can be redistributed, and can be used in commercial applications, but the book must not be distributed or republished without the publisher's consent.

2020-09-14


Dedication

To Our Families.

Table of Contents
Foreword
I. Associations
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
II. Inheritance in Class Hierarchies
12. Subtyping and Inheritance
1. Introducing Subtypes by Specialization
2. Introducing Supertypes by Generalization
3. Intension versus Extension
4. Type Hierarchies
5. Kinds and Roles
6. The Class Hierarchy Merge Design Pattern
7. Subtyping and Inheritance in Computational Languages
7.1. Subtyping and inheritance in OOP
7.2. Subtyping and inheritance with XML Schema
7.3. Subtyping and inheritance with OWL
7.4. Representing class hierarchies with SQL database tables
7.4.1. Single Table Inheritance
7.4.2. Table per Class Inheritance
7.4.3. Joined Tables Inheritance
8. Quiz Questions
8.1. Question 1: Statements about a Class Hierarchy
8.2. Question 2: Class Hierarchy Merge
13. Subtyping with Plain JS
1. Subtyping with Constructor-Based Classes
2. Case Study 1: Eliminating a Class Hierarchy
2.1. Make the JS class model
2.2. New issues
2.3. Code the model classes of the JS class model
2.3.1. Summary
2.3.2. Code the enumeration type BookCategoryEL
2.3.3. Code the model class Book
2.4. Write the View and Controller Code
2.4.1. Summary
2.4.2. Add a segment information column in Retrieve/List All
2.4.3. Add a category selection field in Create and Update
3. Case Study 2: Implementing a Class Hierarchy
3.1. Make a JS class model
3.2. Make a JS entity table model
3.3. New issues
3.4. Code the model classes of the JS class model
3.4.1. Defining subtype relationships
3.4.2. Loading the instances of the root class Person
3.4.3. Saving the supertable when saving a subtable
4. Quiz Questions
4.1. Question 1: Constructor-Based Inheritance Pattern
4.2. Question 2: Check Method for Segment Property
14. Subtyping with Java EE
1. Subtyping in Java
2. Case Study 1: Implement a Class Hierarchy with Single Table Inheritance
2.1. Make the Java Entity class model
2.2. New issues
2.3. Code the classes of the Java Entity class model
2.3.1. Summary
2.3.2. Code the enumeration type BookCategoryEL
2.4. STI database schema
2.5. Write the view and controller code
2.5.1. Summary
2.5.2. Add a segment information column in Retrieve/List All
2.5.3. Add a "Special type" select control in Create and Update
3. Case Study 2: Implement a Class Hierarchy with Joined Table Inheritance
3.1. Make the Java Entity class model
3.2. New issues
3.3. Code the model classes of the Java Entity class model
3.3.1. Define the category relationships
3.3.2. Database schema for joined table class hierarchy
3.4. Write the View and Controller Code
4. Run the App
15. Subtyping Practice Projects
1. Project 1 - Person Types and Movie Types
Glossary
Index
List of Figures
1.1. A committee has a club member as chair expressed by the reference property chair
1.2. An association end with a "dot"
1.3. Representing unidirectional associations as reference properties
1.4. A model of a non-directed association between Committee and ClubMember
1.5. Modeling a bidirectional association between Committee and ClubMember
1.6. The Publisher-Book information design model with a unidirectional association
1.7. The Publisher-Book-Author information design model with two unidirectional associations
1.8. Turning a functional association end into a reference property
1.9. Turning a non-functional association end into a multi-valued reference property
1.10. An OO class model for Publisher and Book
1.11. An OO class model for the classes Book, Publisher and Author
2.1. A JS class model defining the classes Book and Publisher
5.1. A set-valued reference property authoredBooks and an ordered-set-valued reference property authors
7.1. The Publisher-Book-Author information design model with two bidirectional associations
7.2. Turn a bidirectional one-to-one association into a master-slave pair of mutually inverse single-valued reference properties
7.3. Turn a bidirectional many-to-many association into a master-slave pair of mutually inverse multi-valued reference properties
7.4. The OO class model with two pairs of mutually inverse reference properties
12.1. The object type Book with two subtypes: TextBook and Biography
12.2. The object types Employee and Author share several attributes
12.3. The object types Employee and Author are generalized by the common supertype Person
12.4. An information model with two class hierarchies
12.5. A class hierarchy having the root class Vehicle
12.6. A multiple inheritance hierarchy
12.7. The result of applying the Class Hierarchy Merge design pattern
12.8. An information design model with a Person roles hierarchy
12.9. An SQL table model with a single table representing the Book class hierarchy
12.10. An STI table model representing the Person roles hierarchy
12.11. A TCI table model representing the Person roles hierarchy
12.12. A JTI table model representing the Person roles hierarchy
13.1. The JS class model of the merged Book class hierarchy
13.2. The JS class model of the Person roles class hierarchy
13.3. An STI model of the Person roles class hierarchy
13.4. A TCI model of the Person roles class hierarchy
14.1. The Java Entity class model of the Book class hierarchy
14.2. The Java Entity class model of the Person class hierarchy
List of Tables
1.1. An example of an association table
1.2. Functionality types
List of Examples
2.1. Code Example 1
2.2. Code Example 2
2.3. Use Case 1: Dealing with multiple return values of a function
2.4. Use Case 2: Swapping two variables
2.5. Use Case 3: Cloning arrays
2.6. Use Case 4: Simplifying functions with parameter records