Chapter 11. Entity Relationships
Chapter 10 covered basic persistence mappings, including various ways to define primary keys as well as simple and complex property-type mappings. This chapter retools our employee registry a bit further by discussing the relationships between entities.
In order to model real-world business concepts, entity beans must be
capable of forming relationships. For instance, an employee may have an
address; we’d like to form an association between the two in our database
model. The address could be queried and cached like any other entity, yet a
close relationship would be forged with the Employee entity. Entity beans can also have
one-to-many, many-to-one, and many-to-many relationships. For example, the
Employee entity may have many phone
numbers, but each phone number belongs to only one employee (a one-to-many
relationship). Similarly, an employee may belong to many teams within his or
her organization, and teams may have any number of employees (a many-to-many
relationship).
The Seven Relationship Types
Seven types of relationships can exist between entity beans. There are four types of cardinality: one-to-one, one-to-many, many-to-one, and many-to-many. In addition, each relationship can be either unidirectional or bidirectional. These options seem to yield eight possibilities, but if you think about it, you’ll realize that one-to-many and many-to-one bidirectional relationships are actually the same thing. Thus, there are only seven distinct relationship ...