Chapter 9. Dealing with Enumerations in Java, JPA and JSF

Table of Contents

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

In this chapter, we show how to build a back-end web application with enumeration attributes, using Java with JPA and JSF. In addition to the topic of enumeration attributes, we also show how to deal with multi-valued attributes.

We again consider the simple data management problem that was considered in Part 2 of this tutorial, but now we have four additional enumeration attributes, as shown in the UML class diagram in Figure 9.1 below:

  1. the single-valued mandatory attribute originalLanguage with the enumeration datatype LanguageEL as its range,

  2. the multi-valued optional attribute otherAvailableLanguages with range LanguageEL,

  3. the single-valued mandatory attribute category with range BookCategoryEL

  4. the multi-valued mandatory attribute publicationForms with range PublicationFormEL

Figure 9.1. An information design model for the object type Book

An information design model for the object type Book

Notice that the attributes otherAvailableLanguages and publicationForms are multivalued, as indicated by their multiplicity expressions [*] and [1..*]. This means that the possible values of these attributes are sets of enumeration literals, such as the set {ePub, PDF}, which can be represented in Java as a corresponding set, or array list, of enumeration literals.

The meaning of the design model and its enumeration properties can be illustrated by a sample data population for the model class Book:

Table 9.1. Sample data for Book

ISBN Title Original language Other languages Category Publication forms
0553345842 The Mind's I English (en) de, es, fr novel paperback, ePub, PDF
1463794762 The Critique of Pure Reason German (de) de, es, fr, pt, ru other paperback, PDF
1928565379 The Critique of Practical Reason German (de) de, es, fr, pt, ru other paperback
0465030793 I Am A Strange Loop English (en) es textbook hardcover, ePub

1. New Issues

Compared to the Validation App discussed in Part 2 we have to deal with the following new issues:

  1. Enumeration datatypes have to be defined in a suitable way as part of the model code.

  2. Enumeration attributes have to be defined in model classes and handled in the user interface.

In terms of coding, the new issues are:

  1. In the model code we have to take care of

    1. enumeration datatypes to be defined in the form of Java enum classes;

    2. single-valued enumeration attributes (like Book::originalLanguage) requiring specific JPA annotations;

    3. multi-valued enumeration attributes (like Book::publicationForms) requiring specific JPA annotations and mapping methods for serializing their values in the form of suitable strings;

    4. extending the methods Book.add, and Book.update such that they take care of the added enumeration attributes.

  2. In the user interface code we have to take care of

    1. adding new table columns in listAll.xhtml;

    2. adding suitable form controls (such as selection lists, radio button groups or checkbox groups) in create.xhtml and upate.xhtml;

    3. rendering multi-valued enumeration attributes in the table view of listAll.xhtml and in the choice widgets of create.xhtml and upate.xhtml (with the help of an array of JSF SelectItems each consisting of an enumeration value and its label).