| Oracle® TopLink Developer's Guide 10g (10.1.3.1.0) B28218-01 |
|
![]() Previous |
![]() Next |
CMP is the part of the J2EE component model that provides an object persistence service that an EJB container uses to persist entity beans. CMP provides distributed, transactional, secure access to persistent data, with a guaranteed portable interface.
This architecture is an extension of the three-tier architecture, in which the implementation of persistence methods is handled by the container at runtime. As a bean provider, you only need to specify in a deployment descriptor those persistent fields and relationships for which the container must handle data access and, optionally, an abstract representation of the database schema.
TopLink CMP is an extension of the TopLink persistence framework that provides custom integration to EJB containers of various application servers (see "Application Server Support"). For more information about choosing an application server, see "Understanding Target Platforms". TopLink integrates with the EJB container in this architecture to augment (or, in the case of OC4J, become) the container's persistence manager.
|
Note: When using OC4J and Java 1.5, TopLink supports a subset of the persistence features anticipated in the final EJB 3.0 specification. For more information on EJB 3.0 support, see Oracle Containers for J2EE Enterprise JavaBeans Developer's Guide.EJB 3.0 feature support is subject to change and dependent upon the contents of the final specification. |
TopLink CMP integration is nonintrusive (see Figure 2-6). Through a combination of run-time integration and code generation, the container uses TopLink internally and the bean user interacts with entity beans with container-managed persistence according to their standard API. This lets you combine the standard interfaces and power of CMP and a container with TopLink flexibility, performance, and productivity.
For more information, see the following:
An example of the entity beans with container-managed persistence implementation is a Model-View-Controller Model 2 architectural design pattern that runs in a J2EE container, with servlets and JSP that access either session beans or entity beans with container-managed persistence enhanced by TopLink.
A three-tier architecture using entity beans with container-managed persistence offers the following advantages:
It allows for entity beans with container-managed persistence supplied with sophisticated TopLink features such as caching and mapping support, storing bean data across more than one table, composite primary keys, and data conversion.
It presents a standard method to access data, which lets you create standardized, reusable business objects.
It is well-suited to create coarse-grained objects, which TopLink relates to dependent, lightweight, regular Java objects (TopLink can also manage container-managed relationships to lightweight dependent Java objects).
TopLink provides for lazy initialization of referenced objects and beans (see "Indirection").
TopLink provides functionality for transactional copies of beans, allowing concurrent access by several clients, rather than relying on individual serialization.
TopLink provides advanced query capabilities, as well as dynamic querying, including the ability to define queries at the bean-level rather than the data source level and to use a rich set of querying and finder options.
TopLink maintains bean and object identity.
The disadvantage of this architecture is that pure entity bean with container-managed persistence architectures can impose a high overhead cost. This is especially true when a data model has a large number of fine-grained classes with complex relationships.
The key technical challenge in this architecture lies in integrating components into a cohesive system. For example, this architecture requires a specific TopLink integration with the application server or J2EE container.
Other issues include the following:
By default, TopLink manages its own connection pools. You can also configure TopLink to use connection pooling offered by the host application server. This feature is useful for shared connection pools and is required for JTA/JTS integration (see "Configuring External Connection Pooling").
JTA and JTS are standard Java components that enable sessions to participate in distributed transactions. You must configure TopLink to use JTA/JTS to use session beans in the architecture (see "Integrating the Unit of Work With an External Transaction Service").
If you choose to use multiple servers to scale your application, you may require TopLink cache coordination (see "Understanding Cache Coordination").
When one-to-one or many-to-many relationship is bidirectional, you must maintain the back pointers as the relationships change.
TopLink automatically maintains the relationship between two entity beans.
To set the back pointer manually, do one of the following:
Code the entity bean to maintain the back pointer when the relationship is established or modified (recommended).
Code the client to explicitly set the back pointer.
If you code the entity bean to set back pointers, the client is freed of this responsibility. This has the advantage of encapsulating this maintenance implementation in the bean.
In a one-to-many relationship, a source bean might have several dependent target objects. For example, an EmployeeBean might own several dependent PhoneNumber instances. When you add a new dependent object (a PhoneNumber, in this example) to an employee, you must set the PhoneNumber instance's back pointer to its owner (the employee). Maintaining a one-to-many relationship in the entity bean involves getting the local object reference from the context of the EmployeeBean, and then updating the back pointer as Example 2-1 shows.
Example 2-1 Setting the Back-Pointer in the Entity Bean
// obtain owner and phoneNumber owner = empHome.findByPrimaryKey(ownerId); phoneNumber = new PhoneNumber("cell", "613", "5551212"); // add phoneNumber to the phoneNumbers of the owner owner.addPhoneNumber(phoneNumber); // Maintain the relationship in the Employee's addPhoneNumber method public void addPhoneNumber(PhoneNumber newPhoneNumber) { // get, then set the back pointer to the owner Employee owner = (Employee)this.getEntityContext().getEJBLocalObject(); newPhoneNumber.setOwner(owner); // add new phone getPhoneNumbers().add(newPhoneNumber); }
For more information, see the following:
Unlike EJB, TopLink dependent persistent objects can be sent back and forth between a client and a server. When objects are serialized, the risk exists the objects can cause the cache to lose the identity of the objects or attempt to cache duplicate identical objects. To avoid potential problems, use the bean setter methods when adding dependent objects to relationship collections as Example 2-2 shows. This enables TopLink to handle merging of objects in the cache.
Collections generally use the equals method to compare objects. This is not a problem in the case of an object that contains a collection of EJBObject objects, because the EJB container collection handles equality appropriately.