Table of Contents
A unidirectional non-functional association is either one-to-many or many-to-many. In both cases such an association is represented, or implemented, with the help of a multi-valued reference property.
In this chapter, we show
how to derive a data model in the form of a JPA entity class model from an information design model with multi-valued reference properties representing unidirectional non-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 model code.
A multi-valued reference property, such as Book::authors
, allows storing a
collection of references to objects of some type, such as references to Author
objects. When creating a new object of type Book
, it must be possible to assign
one or more authors to the book. to assign the constructor needs an extra parameter for the
multi-valued reference property. In general, we can allow this value to be a set (or list) of
internal object references or of ID references. However, using JPA there is no need for ID
references, being very easy to use object references:
@Entity @Table( name="books") @ManagedBean( name="book") @ViewScoped public class Book { ... private Set<Author> authors; public Book() { // required by JPA @Entity annotation!} public Book( String isbn, String title, Integer year, Publisher publisher, Set<Author> authors) {...} public Set<Author> getAuthors() { return this.authors;} public void setAuthors( Set<Author> authors) { this.authors = authors;} ... }