Understanding and Implementing Information Management Concepts and Techniques

Building Back-End Web Apps with Java, JPA and JSF

An incremental in-depth tutorial about building Java back-end web applications with Java Server Faces (JSF) as the user interface technology and the Java Persistence API (JPA) for object-to-storage mapping and storage management

Mircea Diaconescu

Gerd Wagner

Warning: This tutorial book is a draft version, so it may still contain errors and may still be incomplete in certain respects. Please report any issue to [email protected].

This tutorial book is available as an open access online book (HTML) and as an e-book (PDF) via its book webpage.

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 author's consent.

2016-01-29

Revision History
20151130gw
Revised part 1, added practice projects.
20150702gw
First version.

Table of Contents

Foreword
1. Run the Apps and Get Their Code
I. Getting Started
1. A Quick Tour of the Foundations of Web Apps
1. The World Wide Web (WWW)
2. HTML and XML
2.1. XML documents
2.2. Unicode and UTF-8
2.3. XML namespaces
2.4. Correct XML documents
2.5. The evolution of HTML
2.6. HTML forms
3. Styling Web Documents and User Interfaces with CSS
4. JavaScript - "the assembly language of the Web"
4.1. JavaScript as an object-oriented language
4.2. Further reading about JavaScript
5. Accessibility for Web Apps
6. Quiz Questions
6.1. Question 1: Well-Formed XML
6.2. Question 2: HTML Forms
2. Java Summary
1. Compared to JavaScript, what is different in Java?
2. JavaBean Classes and Entity Classes
3. Building a Minimal Java Web App in Seven Steps
1. Step 1 - Set up the Folder Structure
2. Step 2 - Write the Model Code
2.1. Storing Book objects in a database table books
2.2. Creating a new Book instance and storing it
2.3. Retrieving all Book instances
2.4. Updating a Book instance
2.5. Deleting a Book instance
2.6. Creating test data
2.7. Clearing all data
3. Step 3 - Configure the App
3.1. Create the EntityManager and UserTransaction objects
3.2. Configure the JPA database connection
3.3. Create the main template
3.4. Define the managed beans needed in facelets
3.5. Build the WAR file and deploy it to TomEE
4. Step 4 - Implement the Create Use Case
5. Step 5 - Implement the Retrieve/List All Use Case
6. Step 6 - Implement the Update Use Case
7. Step 7 - Implement the Delete Use Case
8. Run the App and Get the Code
9. Possible Variations and Extensions
9.1. Accessibility for Web Apps
9.2. Using resource URLs
9.3. Dealing with date/time information using Date and <time>
9.4. Using an alternative DBMS
10. Points of Attention
10.1. Code clarity
10.2. Boilerplate code
10.3. Offline availability
10.4. Architectural separation of concerns
4. Practice Projects
1. Project 1 - Managing Information about Movies
2. Project 2 - Managing Information about Countries
II. Integrity Constraints
5. Integrity Constraints and Data Validation
1. Introduction
2. Integrity Constraints
2.1. String Length Constraints
2.2. Mandatory Value Constraints
2.3. Range Constraints
2.4. Interval Constraints
2.5. Pattern Constraints
2.6. Cardinality Constraints
2.7. Uniqueness Constraints
2.8. Standard Identifiers (Primary Keys)
2.9. Referential Integrity Constraints
2.10. Frozen Value Constraints
2.11. Beyond property constraints
3. Responsive Validation
4. Constraint Validation in MVC Applications
5. Criteria for Evaluating the Validation Support of Frameworks
6. Quiz Questions
6.1. Question 1: Where to Check Constraints
6.2. Question 2: Where to Define Constraints
6.3. Question 3: Counting Constraints
6.4. Question 4: Constraint Names
6.5. Question 5: Compliant Objects
6. Implementing Constraint Validation in a Java Web App
1. Java Annotations for Persistent Data Management and Constraint Validation
1.1. JPA database constraint annotations
1.2. Bean validation annotations
2. New Issues
3. Make a JPA Entity Class Model
4. Style the User Interface with CSS
5. Write the Model Code
5.1. Type mapping
5.2. Encode the constraints as annotations
5.3. Checking uniqueness constraints
5.4. Dealing with model related exceptions
5.5. Requiring non-empty strings
6. Validation in the View Layer
6.1. Form validation for the Create Object use case
6.2. Form validation for the Update Object use case
7. Defining a Custom Validation Annotation
8. Run the App and Get the Code
9. Possible Variations and Extensions
9.1. Object-level constraint validation
9.2. Alternative class level custom constraint validation
7. Practice Projects
1. Project 1 - Develop a Java Back-End App with Constraint Validation
III. Enumerations
8. Enumerations and Enumeration Attributes
1. Enumerations
1.1. Simple Enumerations
1.2. Code Lists
1.3. Record Enumerations
2. Enumeration Attributes
3. Enumerations in Computational Languages
3.1. Enumerations in SQL
3.2. Enumerations in XML Schema
3.3. Enumerations in JavaScript
4. Quiz Questions
4.1. Question 1: Languages Supporting Enumerations
4.2. Question 2: UI Widget for Single-Valued Enum Attribute
4.3. Question 3: UI Widget for Multi-Valued Enum Attribute
9. Dealing with Enumerations in Java, JPA and JSF
1. New Issues
2. Make a JPA Entity Class Model
3. Write the Model Code
3.1. Encode the enumerations
3.2. Encode the entity class
3.3. Define a converter for serializing enumeration attribute values
3.4. Encode the enumeration attribute setters
3.5. Write a serialization function
3.6. Database schema
3.7. Creating test data
4. Write the View Code
4.1. Selection lists
4.2. Choice widgets
4.3. Responsive validation for selection lists and choice widgets
5. Show multi-valued properties for the List Objects Use Case
6. Run the App and Get the Code
7. Possible Variations and Extensions
7.1. Ordered-Set-Valued Attributes
10. Practice Projects
1. Project 1 - Develop a Java Back-End App with Enumeration Attributes
IV. Associations
11. 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 restricted to unidirectional associations
7.2. Eliminating associations from the Publisher-Book-Author 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
12. Implementing Unidirectional Functional Associations with Java, JPA and JSF
1. Implementing Single-Valued Reference Properties in Java
2. Make a JPA Entity Class Model
3. New Issues
4. Write the Model Code
4.1. Summary
4.2. Encode each class of the data model
4.3. Encode the constraints
4.4. Encode 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 the List Objects use case
5.3. Allow selecting associated objects in the create and update use cases
13. Implementing Unidirectional Non-Functional Associations with Java, JPA and JSF
1. Implementing Multi-Valued Reference Properties in Java
2. Make a JPA Entity Class Model
3. New issues
4. Write the Model Code
4.1. Summary
4.2. Encode the entity classes
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 the List Objects use case
5.2. Allow selecting associated objects in the create use case
6. Run the App and Get the Code
7. Possible Variations and Extensions
7.1. Set-valued versus ordered-set-valued reference properties
14. Bidirectional Associations
1. Inverse Reference Properties
2. Making an Association-Free Information Design Model
2.1. The basic procedure
2.2. How to eliminate unidirectional associations
2.3. How to eliminate bidirectional associations
2.4. The resulting association-free design model
3. Quiz Questions
3.1. Question 1: Table populations of bidirectional associations
3.2. Question 2: Implications of bi-directionality
3.3. Question 3: Elimination of bi-directional associations
3.4. Question 4: Meaning of bi-directional association
15. Implementing Bidirectional Associations with Java, JPA and JSF
1. Make a JPA Entity Class Model
2. Write the Model Code
2.1. New issues
2.2. Summary
2.3. Encode each class of the data model as an entity class
2.4. Encode the setter operations
2.5. Encode the add and remove operations
2.6. Take care of deletion dependencies
2.7. EntityManagers and Cached Entities
3. Exploiting Derived Inverse Reference Properties in the User Interface
3.1. Show information about published books in the List Publishers use case
4. Run the App and Get the Code
16. Part-Whole Associations
1. Aggregation
2. Composition
V. Inheritance in Class Hierarchies
17. Subtyping and Inheritance
1. Introducing Subtypes by Specialization
2. Introducing Supertypes by Generalization
3. Intension versus Extension
4. Type Hierarchies
5. The Class Hierarchy Merge Design Pattern
6. Subtyping and Inheritance in Computational Languages
6.1. Subtyping and Inheritance with OOP Classes
6.2. Subtyping and Inheritance with Database Tables
7. Quiz Questions
7.1. Question 1: Statements about a Class Hierarchy
7.2. Question 2: Class Hierarchy Merge
18. Subtyping in a Java Back-End App
1. Subtyping in Java
2. Case Study 1: Eliminating a Class Hierarchy with Single Table Inheritance
2.1. Make the Java data model
2.2. New issues
2.3. Encode the model classes of the Java data model
2.4. Database schema for single table class hierarchy
2.5. Write the View and Controller Code
3. Case Study 2: Implementing a Class Hierarchy with Joined Table Inheritance
3.1. Make the Java data model
3.2. New issues
3.3. Encode the model classes of the Java data model
3.4. Write the View and Controller Code
4. Run the App and Get the Code
Glossary

List of Figures

3.1. The object type Book.
4.1. The object type Movie.
4.2. The object type Movie
5.1. The object type Person with an interval constraint
5.2. The object type Book with a pattern constraint
5.3. Two object types with cardinality constraints
5.4. The object type Book with a uniqueness constraint
5.5. The object type Book with a standard identifier declaration
5.6. Class with constraints
5.7. Class with constraints
5.8. Class with constraints
6.1. A platform-independent design model with the class Book and two invariants
6.2. Deriving a JavaBean data model from an information design model
7.1. The object type Movie defined with several constraints.
8.1. The simple enumeration GenderEL represented as a UML enumeration data type
8.2. The enumeration GenderEL defined as a code list
8.3. A single and a multiple select element with no selected option
8.4. A radio button group
8.5. A checkbox group
9.1. An information design model for the object type Book
9.2. A JPA data model for the object type Book
9.3. The user interface for creating a new book record with ISBN, title and four enumeration attributes
9.4. The user interface for listing all the book records with the corresponding attribute values
10.1. The object type Movie defined together with two enumerations.
11.1. A committee has a club member as chair expressed by the reference property chair
11.2. A committee has a club member as chair expressed by an association end with a "dot"
11.3. Representing the unidirectional association ClubMember has Committee as chairedCommittee as a reference property
11.4. A model of the Committee-has-ClubMember-as-chair association without ownership dots
11.5. Modeling a bidirectional association between Committee and ClubMember
11.6. The Publisher-Book information design model with a unidirectional association
11.7. The Publisher-Book-Author information design model with two unidirectional associations
11.8. Turn a non-functional target association end into a corresponding reference property
11.9. The association-free Publisher-Book design model
11.10. The association-free Publisher-Book-Author design model
13.1. An example with a set-valued reference property authoredBooks and an ordered-set-valued reference property authors
14.1. The Publisher-Book-Author information design model with two bidirectional associations
14.2. Turn a bidirectional one-to-one association into a pair of mutually inverse single-valued reference properties
14.3. Turn a bidirectional many-to-many association into a master-slave pair of mutually inverse multi-valued reference properties
14.4. The association-free design model
15.1. The Java data model
17.1. The object type Book is specialized by two subtypes: TextBook and Biography
17.2. The object types Employee and Author share several attributes
17.3. The object types Employee and Author have been generalized by adding the common supertype Person
17.4. The complete class model containing two inheritance hierarchies
17.5. A class hierarchy having the root class Vehicle
17.6. A multiple inheritance hierarchy
17.7. The design model resulting from applying the Class Hierarchy Merge design pattern
17.8. A class model with a Person roles hierarchy
17.9. An SQL table model with a single table representing the Book class hierarchy
17.10. An SQL table model with a single table representing the Person roles hierarchy
17.11. An SQL table model with the table Person as the root of a table hierarchy
18.1. The object type Book as the root of a disjoint segmentation
18.2. The Person roles hierarchy
18.3. Student is a subclass of Person
18.4. The Java data model
18.5. The Java data model of the Person class hierarchy

List of Tables

2.1. Java Visibility Level
3.1. Sample data for Book
3.2. Book objects represented as a table
4.1. Sample data
4.2. Sample country data
6.1. Sample data for Book
6.2. JavaBean validation annotations for properties
6.3. Datatype mapping to Java
6.4. Datatype mapping to MySQL
8.1. Representing an enumeration of records as a table
8.2. Representing a record enumeration as a table
9.1. Sample data for Book
11.1. An example of an association table
11.2. Different terminologies
11.3. Functionality types
12.1. Sample data for Publisher
12.2. Sample data for Book
13.1. Sample data for Publisher
13.2. Sample data for Book
13.3. Sample data for Author