There is an association between an object and an object in addition to an inheritance relationship: a pair of one or one pairs, multiple pairs, and many-to-many relationships, and in the OpenJPA framework, developers only need to use annotations such as javax.persistence.OneToOne and provide the appropriate configuration content. You can easily implement association relationships between entities, and you can implement cascading creation, updating, and deletion of entities.
In this paper, we will take a one-to-one relationship between the entities as an example, in-depth talk about how to use the annotations provided by the OpenJPA framework to achieve the association between entities in enterprise applications. This article will provide a simple example that details how to define the steps for one-to-one relationships between classes and classes, while highlighting the attributes supported by these annotations. One-to-many, multiple pairs of one and many to many of these three relationships in the OpenJPA process and the implementation process of one-to-one association relationship is consistent, just need to choose to use different annotations, at the end of this article, the implementation of these three relationships are simply explained, the reader can The relationship between One-to-many, Many-to-many and Many-to-many is realized by referring to the realization process of one-to-one relationship.
One-to-one relationships
In an object-oriented world, a one-to-one relationship between class A and class B must meet the following criteria:
Object A1 refers to the object B1;
The other object of class A cannot refer to the same object B1.
In a relational database, we typically use a unique foreign key to achieve one-to-one relationships, as illustrated in the following illustration.
Figure 1. One-to-one relationships in a relational database
The following is an introduction to the knowledge about one-to-one relationships among implementing entities in OpenJPA, and we first define a simple application scenario to illustrate the need.
Simulation scene
Suppose the developer wants to complete a library management system, we need to record the basic information such as number, book title, date of publication and other basic information, also need to record the preface, preface and other information.
To illustrate the one-to-one relationship between entities, we designed the book as a Class (book), including the number of books and the names of two attributes, while the preface of the book was designed as another class (Bookextend), which included the number of the book and two attributes of the preface. Since a book has a preface and it is impossible to have a preface to other books it will be the same, so the book and the Bookextend naturally form a one-to-one relationship. The properties of these two classes, as well as the relationships between classes, are shown in the following illustration.
Figure 2. One-to-one relationships between classes
[Note]: In order to illustrate the simplicity of this example, each object selected only the necessary attributes at design time.