The Java Persistence API allows us to define relationships between entities. The four different types of relationships are as follows:
One-to-one relationships
One-to-many relationships
Many-to-one relationships
Many-to-many relationships
A one-to-one relationship is useful when we have two entities that have a direct relationship, but we do not want to model these entities in the same Java classes. For example, a customer may have a one-to-one relationship with an account—one customer has one account and one account belongs only to one customer.
In this case, we use the @OneToOne
annotation within Java classes to express the relationship between entities.
@Entity @Name("customer") public class Customer { private Account account; @OneToOne(mappedBy="customer") public Account getAccount() { return account; } public void setAccount(Account account) { this.account = account; } // Rest of class omitted for brevity. } @Entity @Name("account")...