Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Enterprise JavaBeans™ Specification, Version 2.0 - Sun Microsystems.pdf
Скачиваний:
14
Добавлен:
24.05.2014
Размер:
2.71 Mб
Скачать

Sun Microsystems Inc

The entity bean provider’s view of persistence Enterprise JavaBeans 2.0, Public Draft Entity Bean Component Contract for Container

The accessor methods must be public or protected, must be abstract, and must bear the name of the container managed persistent field ( cmp-field) or container managed relationship field

(cmr-field) that is specified in the deployment descriptor, prefixed by “ get” or “ set”.

The accessor methods for container managed relationship fields that reference other enterprise beans must be defined in terms of the remote interfaces of those beans, as described in Section 9.4.2.

The accessor methods for container managed relationship fields for one-to-many or

many-to-many relationships must utilize one of the following Collection interfaces: java.util.Collection or java.util.Set[8]. The Collection interfaces used by a

dependent object class are specified in the deployment descriptor.

The container managed persistent fields and container managed relationship fields of the dependent object class must be specified in the deployment descriptor using the cmp-field and cmr-field elements respectively.

Since the container-managed dependent object classes are abstract, the Bean Provider must use a special API to obtain a new instance of a dependent object class at runtime: the create<type>() method, where <type> is the name of the dependent object type of the cmr-field in the case of a one-to-one relationship, and <type> is the name of the collection element type in the case of a one-to-many or many-to-many relationship. A create method must be provided on the entity bean class as an abstract method for each dependent object cmr-field type defined by the entity bean. Likewise, a create method must be provided on a dependent object class as an abstract method for each dependent object class that is the type of a cmr-field defined by the dependent object class or the element type of a collection-valued cmr-field. Because the create methods are on the entity bean and its dependent object classes, they are only available to instances of the entity bean class (or dependent object class) and not to clients of the bean.

Since the implementation of the dependent object classes is maintained by the Persistence Manager, a special API is provided to allow the Bean Provider to obtain a copy of an instance of a dependent object class at runtime, if needed: the deepCopy() method. Every dependent object class must define an abstract deepCopy() method. The implementation of this method is provided by the Persistence Manager. The use of the deepCopy() method is discussed further in Section 9.4.5.1.

9.4.5 Semantics of dependent object classes

An instance of a dependent object class is a dependent object in the sense of Section 9.3.1. That is, its existence is dependent upon the existence of some other object[9].

Dependent object classes can be shared across multiple entity beans (of the same or different type) within the same ejb-jar file, and across multiple relationships of the same entity bean. For example, the dependent object class Address can be used in the two cmr-fields shipping_address and billing_address of an Order entity bean.

[8]We expect to include java.util.List and java.util.Map in a later version of this specification.

[9]The semantics of the removal of dependent object class instances will be specified in a later draft of this specification.

115

5/31/00

Sun Microsystems Inc.

Entity Bean Component Contract for Container Managed PersistenceEnterprise JavaBeans 2.0, Public Draft The entity bean provider’s

9.4.5.1 Semantics of assignment for instances of dependent object classes

A dependent object class instance can only be assigned to a relationship of an appropriate relationship type (i.e., as specified by the ejb-relation and ejb-relationship-role elements of the deployment descriptor). It is the responsibility of the Persistence Manager to detect an illegal assignment operation and to raise the java.lang.IllegalArgumentException.

In the case of a one-to-one or one-to-many relationship, if a dependent object class instance (or collection of dependent object class instances) is assigned from a cmr-field of a given relationship type in one instance to a cmr field of the same relationship type in another instance, the dependent object instance (or collection of dependent object class instances) is effectively moved, and the value of the source cmr-field is set to null (in the case of a one-to-one relationship) or the empty collection (in the case of a one-to-many relationship). The set accessor method thus has special semantics within a relationship that is determined by the relationship multiplicity as specified in the deployment descriptor. The Bean Provider uses the set method to move a dependent object instance between cmr fields of the same relationship type in different instances.

In the following example, the telephone numbers that were associated with the billing address of an Order bean instance are transferred to the shipping address. Both the billing address and shipping address are of the same dependent object class, Address. The dependent object class Address is related to the dependent object class TelephoneNumber in a one-to-many relationship. The example illustrates how a Bean Provider uses the set method to move a set of dependent object class instances.

public void changeTelephoneNumber() { Address a = getShippingAddress(); Address b = getBillingAddress();

a.setTelephoneNumbers(b.getTelephoneNumbers()); Collection c = b.getTelephoneNumbers();

if (c.isEmpty()) {//must be true...

..

}

If the Bean Provider needs to use the value of a dependent object instance in another instance of the same one-to-one or one-to-many relationship type, the deepCopy method must be used to create a copy of the dependent object instance. The use of the deepCopy method is illustrated below:

public void useSameAddress(Boolean useSame) { if (useSame) {

Address a = getShippingAddress(); setShippingAddress(a.deepCopy()); a.setStreet(“Maiden Lane”);

} else {...

}

}

5/31/00

116