Table of Contents
The three example apps that we have discussed in previous chapters, the minimal app, validation app and 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 software development text books and not well supported by application development frameworks. In this part of the tutorial, we show how to deal with unidirectional associations, while bidirectional associations and subtype relationships are covered in parts 5 and 6.
We adopt the approach of model-based development, which provides a general methodology for engineering all kinds of artifacts, including data management apps. For being able to understand this tutorial, you need to understand the underlying concepts and theory. Either you first read the theory chapter on reference properties and associations, before you continue to read this tutorial chapter, or you start reading this tutorial chapter and consult the theory chapter only on demand, e.g., when you stumble upon a term that you don't know.
A unidirectional functional association is either one-to-one or many-to-one. In both cases such an association is represented, or implemented, with the help of a single-valued reference property.
In this chapter of our tutorial, we show
how to derive a data model in the form of a JPA entity class model from an information design model with single-valued reference properties representing unidirectional functional associations,
how to encode the JPA entity class model in the form of entity classes (representing model classes),
how to write the view and controller code based on the entity classes.
A single-valued reference property, such as the property publisher
of the
object type Book
, allows storing internal references to objects of another type,
such as Publisher
. When creating a new object, the constructor function needs to
have a parameter for allowing to assign a suitable value to the reference property. In a typed
programming language, such as Java, we may have to take a decision if this value is expected to
be an internal object reference or an ID reference. Using the JPA object-to-storage mapping
technology, we can work with object references and leave the mapping to corresponding ID
references to JPA. The Book
class is extended as
follows:
@Entity @Table( name="books") @ViewScoped @ManagedBean( name="book") public class Book { ... private Publisher publisher; public Book() {} public Book( String isbn, String title, Integer year, Publisher publisher) {...} public Publisher getPublisher() {...} public void setPublisher( Publisher publisher) {...} ... }