Designing Container-Managed Relationships

Now that you understand the basics of container-manager relationships, let's examine the design of the order-line item relationship in detail and implement it. Figure 12.5 shows an order entity bean with a relationship to line items. Each order is composed of multiple line items. A composition relationship is shown as a solid line with a filled diamond at one end. The order is composed of many line items, and the name of the composition relationship is Order-LineItems. Order-has-lineitems is the role name of the OrderEJB and Lineitems-belongs-to-order is the role name of the LineItemEJB in this relationship.

Figure 12.5. Order-line item relationship.


An OrderEJB entity bean class consists of the persistent fields orderId, studentId, orderDate, status, and amount, and of the container-managed persistent field, lineItems. It also implements two business methods: addLineItem() and getOrderLineItems(). It implements the methods setEntityContext(), unsetEntityContext(), ejbActivate(), ejbPassivate(), ejbLoad(), ejbStore(), and ejbRemove() as defined in the javax.ejb.EntityBean interface. It also implements the ejbCreate() and ejbPostCreate() methods. An order entity bean is identified by its primary key, orderId.

An OrderLineItemEJB entity bean class consists of the persistent fields orderLineItemId, courseId, and fee, and of the container-managed persistent field, order. It implements the methods setEntityContext(), unsetEntityContext(), ejbActivate(), ejbPassivate(), ejbLoad(), ejbStore(), ejbRemove() as defined in the javax.ejb.EntityBean interface. It also implements the ejbCreate() and ejbPostCreate() methods. An OrderLineItem entity bean is identified by its primary key, orderLineItemId.

In addition (but not shown in Figure 12.5), the order entity bean uses the line item internally, but does not expose it to remote clients. This is achieved as follows: The order entity bean defines both a remote and a local component interface. The local interface is presented only to the line item entity bean and the remote interface is presented to the remote clients. Note that this satisfies the requirement that an entity bean that is the target of a container-managed relationship must provide local interfaces.

Also, the lifetime of the line items is coincident with the lifetime of the order. Because a line item object should exist only when the parent order exists, you want to ensure that when you delete an order, you also delete all the line items that belong to the order. This is known as a cascade delete. We'll discuss the cascade delete facility later today.

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

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