Chapter 7.  EJB 2.0 CMP: Entity Relationships

In Chapter 6, you learned about basic EJB 2.0 container-managed persistence. This material included coverage of container-managed persistence fields and an introduction to a basic container-managed relationship field. In this chapter, we will continue to develop the Customer EJB and discuss in detail each of the seven possible relationships that entity beans can have with each other.

For entity beans to model real-world business concepts, they must be capable of forming complex relationships with each other. This was difficult to accomplish in EJB 1.1 container-managed persistence because of the simplicity of the programming model. In EJB 1.1, entity beans could have persistence fields but not relationship fields.

In EJB 2.0, relationship fields can model complex relationships between entity beans. In Chapter 6, we demonstrated a one-to-one relationship between the Customer and Address EJBs. This relationship was unidirectional; the Customer had a reference to the Address, but the Address did not have a reference back to the Customer. This is a perfectly legitimate relationship, but other relationships are also possible. For example, each Address could also reference its Customer. This is an example of a bidirectional one-to-one relationship, in which both participants maintain references to one another. In addition to one-to-one relationships, entity beans can have one-to-many, many-to-one, and many-to-many relationships. For example, ...

Get Enterprise JavaBeans, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.