1. Implementing Multi-Valued Reference Properties in Java

A multi-valued reference property, such as Book::authors, allows storing a collection of internal references to objects of some type, such as references to Author objects. When creating a new object of type Book, the constructor function needs to have a parameter for providing a suitable value for this property. In general, this value can be a set of internal object references or of ID references. With JPA, we 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;}
  ...
}