C M P 2 . 0
Container Managed Relationships
Container Managed Relationships (CMRs) are a powerful new feature of CMP 2.0.
Programmers have been creating relationships between entity objects since EJB 1.0 was
introduced (not to mention since the introduction of databases), but before CMP 2.0 the
programmer had to write a lot of code for each relationship in order to extract the primary
key of the related entity and store it in a pseudo foreign key field. The simplest relationships
were tedious to code, and complex relationships with referential integrity required many
hours to code. With CMP 2.0 there is no need to code relationships by hand. The container
can manage one to one, one to many and many to many relationships, with referential
integrity. One restriction with CMRs is that they are only defined between local interfaces.
This means that a relationship cannot be created between two entities in different virtual
machines.
3
There are two basic steps to create a container managed relationship: create the cmr field
abstract accessors and declare the relationship in the ejb jar.xml file. The following two
sections describe these steps.
CMR Field Abstract Accessors
A cmr field abstract accessor has the same signatures as cmp field, except a single valued
cmr field must return the local interface of the related entity, and a multi valued cmr field
can only return a java.util.Collection (or java.util.Set) object. For example, to declare a
one to many relationship between Organization and Gangster, first add the following to the
OrganizationBean class:
public abstract class OrganizationBean implements EntityBean {
public
abstract
Set
getMemberGangsters
();
public
abstract
void
setMemberGangsters
(
Set
gangsters);
}
Listing 9 6, Collection Valued cmr field Abstract Accessor Declaration
Second, add the following to the GangsterBean class:
public abstract class GangsterBean implements EntityBean {
public
abstract
Organization
getOrganization
();
public
abstract
void
setOrganization
(
Organization
org);
}
Listing 9 7, Single Valued cmr field Abstract Accessor Declaration
Although in Listing 9 6 and Listing 9 7 each bean declared a cmr field, only one of the two
beans in a relationship must have a set of accessors. As with cmp fields, a cmr field is
required to have both a getter and a setter method.
3
The EJB specification does not even allow for relationships between entities in different applications within the same VM.
88
footer
Our partners:
PHP: Hypertext Preprocessor Cheap Web Hosting
JSP Web Hosting
Ontario Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Java Hosting
Cheapest Hosting
Visionwebhosting.net Business web hosting division of Vision Web Hosting Inc.. All rights reserved