Developing a persistent application begins with defining a domain model. A domain model consists of a number of entities (classes) and relationships between them.

Figure 18.4, “A Domain Model” illustrates a simple domain model as a UML class diagram. It has two entities: Country and Person. They have a "country has persons" relationship. This is a one-to-many relationship with one country having many persons, each of which belongs to just one country.


Realized in Java, the classes are as follows:

public class Country {
    private Long   id;
    private String name;
    private Set<Person> persons;

    ... setters and getters ...
}

public class Person {
    private Long    id;
    private String  name;
    private Integer age;
    private Country country;

    ... setters and getters ...
}

You should make the classes proper beans by defining a default constructor and implementing the Serializable interface. A default constructor is required by the JPA entity manager for instantiating entities. Having the classes serializable is not required but often useful for other reasons.

After you have a basic domain model, you need to define the entity relationship metadata by annotating the classes.

The entity relationships are defined with metadata. The metadata can be defined in an XML metadata file or with Java annotations defined in the javax.persistence package. With Vaadin JPAContainer, you need to provide the metadata as annotations.

For example, if we look at the Person class in the JPAContainer AddressBook Demo, we define various database-related metadata for the member variables of a class:

@Entity
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long    id;

    private String  name;
    private Integer age;

    @ManyToOne
    private Country country;

The JPA implementation uses reflection to read the annotations and defines a database model automatically from the class definitions.

Let us look at some of the basic JPA metadata annotations. The annotations are defined in the javax.persistence package. Please refer to JPA reference documentation for the complete list of possible annotations.