Examining Container-Managed Relationships

As with a table in a relational database, an entity bean may be related to other entity beans. With BMP entity beans, you need to write code to manage the relationships. With CMP, you declare the relationships in the deployment descriptor and the container generates the code to manage the relationships. An entity bean relationship field represents a relationship to another entity bean. The EJB container provides automatic management of both the persistent state of an entity bean and its relationships to other entity beans.

Local interfaces provide the foundation for container-managed relationships. An entity bean uses its local interface to maintain its relationships to other entity beans. Also, the bean provider creates a single ejb-jar file that contains a deployment descriptor describing the entity beans and their relationships.

Caution

Container-managed relationships are applicable only to container-managed persistence beans and not to bean-managed persistence entity beans. An entity bean that is the target of a container-managed relationship must provide local interfaces.


Cardinality

Cardinality indicates how many instances of one class may be associated with a single instance of another class. Cardinality is also known as multiplicity. Container-managed relationships may be one-to-one, one-to-many, or many-to-many as described in the following list:

  • One-to-one— Each entity bean is associated with a single instance of another entity bean. For example, each student has a corresponding mailing address.

  • One-to-many— Each entity bean is associated with many instances of another entity bean. For example, each order is composed of many line items.

  • Many-to-many— The entity bean instances may be related to multiple instances of each other. For example, a student can enroll in many courses and each course can have many students enrolled in it.

Directionality

Relationships may be either unidirectional or bidirectional as follows:

  • Bidirectional— If a relationship is bidirectional, it can be navigated in both directions. For example, the one-to-many relationship between order and line items is bidirectional because given an order, you need to find the associated line items; given a line item, you need to find the corresponding order.

  • Unidirectional— A unidirectional relationship can be navigated in only one direction. For example, a line item bean would have a relationship field that identifies the associated course bean, but a course bean would not have a relationship field for the line item bean. In other words, the line item bean knows about the course bean, but the course bean doesn't know which line item bean instances refer to it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.124