In addition to the inheritance relationship between objects and objects, there is also an association: including a pair of more than one or one pairs of many pairs of one and many pairs, because these relationships in the Kodo EJB implementation of the principle is basically similar, so this article mainly on a pair of related relations in depth, At the same time, through simple examples of analysis and practice in detail how to use the annotations provided in the Kodo EJB to define the association between classes and classes, the remaining One-to-many, Many-to-many and Many-to-many three relationships will only be explained at the end of the article, please refer to the implementation process of one-to-one relationship.
In an object-oriented world, a one-to-one relationship between Class A and Class B must meet the following criteria:
Object A1 references an object B1
Class A other object an cannot apply 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.
Let's begin by introducing the knowledge associated with Kodo EJB and one-to-one relationship implementation, and to illustrate the need, we first define a virtual scene.
Simulation scene
We assume that to complete a library management system, the system needs to manage a lot of books, we need to record the basic information such as the number of books, titles, publication dates and other basic information, but also need to record the preface, order and other information.
Suppose we design the book as a Class (book) according to the above requirements, including the number and name of the books, two attributes, and design the preface information of the book into another Class (Bookextend), which includes the number of the book and two attributes of the preface information. Since a book has a preface and it is impossible to have other books the preface will be the same as him, 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.
Note
1, in order to illustrate the simple, example design, each object only select the necessary attributes.
2, the above design only for the demonstration of the requirements and deliberately adopted, does not represent a reasonable design.