Our data model will be simple: a quote will be linked to a customer. This means that a customer can see a set of quotes, and quotes can be seen by a set of customers. In terms of use cases, we want to be able to monetize our API and make the customers pay to access some quote prices. To do so, we will need a sort of whitelist of quotes per customer.
JPA uses a descriptor calledpersistence.xml, placed in the META-INF repository of resources (or WEB-INF), which defines how EntityManager, which is a class that allows the manipulation of our model, will be instantiated. Here is what it looks like for our application:
The link between the database and the Java code is done through entities. An entity is a plain old java object (POJO) that is decorated with the javax.persistence annotations. They mainly define the mapping between the database and the Java model. For instance, @Id marks a Java field that must match the database identifier.
Here is an example of our Quote entity:
@Entity public class Quote { @Id @GeneratedValue private long id;
private String name;
private double value;
@ManyToMany private Set<Customer> customers;
// getters/setters }
This simple model implicitly defines a QUOTEtable with three columns, ID, NAME, and VALUE (the casing can depend on the database), and a table to manage the relationship with the CUSTOMERtable, which is named QUOTE_CUSTOMERby default.
In the same spirit, our Customer entity just defines an identifier and name as columns and also the reverse relationship to the Quote entity:
@Entity public class Customer { @Id @GeneratedValue private long id;