1. Implementing Single-Valued Reference Properties in Java

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) {...}
  ...
}