web server - Borland Technical Publications

web server - Borland Technical Publications web server - Borland Technical Publications

techpubs.borland.com
from techpubs.borland.com More from this publisher
12.11.2014 Views

Container-Managed Persistence in Borland Enterprise Server Container-Managed Persistence in Borland Enterprise Server The Borland Enterprise Server's EJB Container is fully J2EE 1.3 compliant. The bean provider designs persistence schemas for their entity beans, determined the methods for accessing container-managed fields and relationships, and defines these in the beans' deployment descriptor. The deployer maps this persistence schema to the database and creates any other necessary classes for the beans' maintenance. Information on J2EE 1.3 entity beans and CMP 2.0 is found in Chapter 16, “Using BES Properties for CMP 2.x.” BES CMP engine's CMP 1.1 implementation While you don't have to be an expert on all aspects of the Borland CMP engine to use it effectively, it is helpful to have some knowledge of certain areas. This section provides information on the areas that users of the CMP engine should understand. In particular, it focuses on the deployment descriptor file and the XML statements contained within the file. Before continuing, there are some key things to note in the implementation of an entity bean that uses 1.1 container-managed persistence: ■ ■ ■ ■ ■ The entity bean has no implementations for finder methods. The EJB Container provides the finder method implementations for entity beans with containermanaged persistence. Rather than providing the implementation for finder methods in the bean's class, the deployment descriptor contains information that enables the container to implement these finder methods. The entity bean declares all fields public that are managed by the container for the bean. The CheckingAccount bean declares name and balance to be public fields. The entity bean class implements the seven methods declared in the EntityBean interface: ejbActivate(), ebjPassivate(), ejbLoad(), ejbStore(), ejbRemove(), setEntityContext(), and unsetEntityContext(). However, the entity bean is required to provide only skeletal implementations of these methods, though it is free to add application-specific code where appropriate. The CheckingAccount bean saves the context returned by setEntityContext() and releases the reference in unsetEntityContext(). Otherwise, it adds no additional code to the EntityBean interface methods. There is an implementation of the ejbCreate() method (because this entity bean allows callers of the bean to create new checking accounts), and the implementation initializes the instance's two variables, account name and balance amount, to the argument values. The ejbCreate() method returns a null value because, with container-managed persistence, the container creates the appropriate reference to return to the client. The entity bean provides the minimal implementation for the ejbPostCreate() method, though this method could have performed further initialization work if needed. For beans with container-managed persistence, it is sufficient to provide just the minimal implementation for this method because ejbPostCreate() serves as a notification callback. Note that the same rule applies to the methods inherited from the EntityBean interface as well. Providing CMP metadata to the Container According to the EJB Specification, the deployer must provide CMP metadata to the EJB container. The Borland Container captures the CMP-relevant metadata in the XML deployment descriptor. Specifically, the Borland Container uses the vendor-specific portion of the deployment descriptor for the CMP metadata. Chapter 14: Entity Beans and CMP 1.1 in Borland Enterprise Server 115

Container-Managed Persistence in Borland Enterprise Server This section illustrates some of the information that needs to be provided for containermanaged finder methods, particularly if you are constructing container-managed finder methods at the command line level. Because it is not an exhaustive reference, you should refer to the DTD of the deployment descriptor for the detailed syntax. Look for the syntax for the finder methods and Object-Relation (OR) mapping metadata. Constructing finder methods When you construct a finder method, you are actually constructing an SQL select statement with a where clause. The select statement includes a clause that states what records or data are to be found and returned. For example, you might want to find and return checking accounts in a bank. The where clause of the select statement sets limits on the selection process; that is, you might want to find only those checking accounts with a balance greater than some specified amount, or accounts with a certain level of activity per month. When the Container uses container-managed persistence, you must specify the terms of the where clause in the deployment descriptor. For example, suppose you have a finder method called findAccountsLargerThan(int balance) and you are using container-managed persistence. This finder method attempts to find all bank accounts with a balance greater than the specified value. When the Container executes this finder method, it actually executes a select statement whose where clause tests the account balances against the int value passed as a parameter to the method. Because we're using container-managed persistence, the deployment descriptor needs to specify the conditions of the where clause; otherwise, the Container does not know how to construct the complete select statement. The value of the where clause for the findAccountsLargerThan(int balance)method is “balance > :balance“. In English, this translates to: “the value of the balance column is greater than the value of the parameter named balance.” (Note that there is only one argument to the finder method, an int value.) The default container-managed persistence implementation supports this finder method by constructing the complete SQL select statement, as follows: select * from Accounts where ? > balance The CMP engine then substitutes “?” with the int parameter. Lastly, the engine converts the result set into either an Enumeration or Collection of primary keys, as required by the EJB Specification. It is possible to inspect the various SQL statements that the CMP implementation constructs. To do this, enable the EJBDebug flag on the container. When that flag is enabled, it prints the exact statements constructed by the Container. While other EJB Container products use code generation to support CMP, the Borland Container does not use code generation because it has serious limitations. For example, code generation makes it difficult to support a “tuned update” feature, because of the great number of different update statements to container-managed fields that are required. Constructing the where clause The where clause is a necessary part of select statements when you want to delimit the extent of the returned records. Because the where clause syntax can be fairly complex, you must follow certain rules in the XML deployment descriptor file so that the EJB Container can correctly construct this clause. To begin with, you are not obligated to use the literal “where” in your . You can construct a where clause without this literal and rely on the Container to supply it. However, the Container only does this if the is not an empty string; it leaves empty strings empty. For example, you could define a where clause as either: 116 BES Developer’s Guide

Container-Managed Persistence in <strong>Borland</strong> Enterprise Server<br />

Container-Managed Persistence in <strong>Borland</strong> Enterprise Server<br />

The <strong>Borland</strong> Enterprise Server's EJB Container is fully J2EE 1.3 compliant. The bean<br />

provider designs persistence schemas for their entity beans, determined the methods<br />

for accessing container-managed fields and relationships, and defines these in the<br />

beans' deployment descriptor. The deployer maps this persistence schema to the<br />

database and creates any other necessary classes for the beans' maintenance.<br />

Information on J2EE 1.3 entity beans and CMP 2.0 is found in Chapter 16, “Using BES<br />

Properties for CMP 2.x.”<br />

BES CMP engine's CMP 1.1 implementation<br />

While you don't have to be an expert on all aspects of the <strong>Borland</strong> CMP engine to use<br />

it effectively, it is helpful to have some knowledge of certain areas. This section<br />

provides information on the areas that users of the CMP engine should understand. In<br />

particular, it focuses on the deployment descriptor file and the XML statements<br />

contained within the file.<br />

Before continuing, there are some key things to note in the implementation of an entity<br />

bean that uses 1.1 container-managed persistence:<br />

■<br />

■<br />

■<br />

■<br />

■<br />

The entity bean has no implementations for finder methods. The EJB Container<br />

provides the finder method implementations for entity beans with containermanaged<br />

persistence. Rather than providing the implementation for finder methods<br />

in the bean's class, the deployment descriptor contains information that enables the<br />

container to implement these finder methods.<br />

The entity bean declares all fields public that are managed by the container for the<br />

bean. The CheckingAccount bean declares name and balance to be public fields.<br />

The entity bean class implements the seven methods declared in the EntityBean<br />

interface: ejbActivate(), ebjPassivate(), ejbLoad(), ejbStore(), ejbRemove(),<br />

setEntityContext(), and unsetEntityContext(). However, the entity bean is required<br />

to provide only skeletal implementations of these methods, though it is free to add<br />

application-specific code where appropriate. The CheckingAccount bean saves the<br />

context returned by setEntityContext() and releases the reference in<br />

unsetEntityContext(). Otherwise, it adds no additional code to the EntityBean<br />

interface methods.<br />

There is an implementation of the ejbCreate() method (because this entity bean<br />

allows callers of the bean to create new checking accounts), and the implementation<br />

initializes the instance's two variables, account name and balance amount, to the<br />

argument values. The ejbCreate() method returns a null value because, with<br />

container-managed persistence, the container creates the appropriate reference to<br />

return to the client.<br />

The entity bean provides the minimal implementation for the ejbPostCreate()<br />

method, though this method could have performed further initialization work if<br />

needed. For beans with container-managed persistence, it is sufficient to provide<br />

just the minimal implementation for this method because ejbPostCreate() serves as<br />

a notification callback. Note that the same rule applies to the methods inherited from<br />

the EntityBean interface as well.<br />

Providing CMP metadata to the Container<br />

According to the EJB Specification, the deployer must provide CMP metadata to the<br />

EJB container. The <strong>Borland</strong> Container captures the CMP-relevant metadata in the XML<br />

deployment descriptor. Specifically, the <strong>Borland</strong> Container uses the vendor-specific<br />

portion of the deployment descriptor for the CMP metadata.<br />

Chapter 14: Entity Beans and CMP 1.1 in <strong>Borland</strong> Enterprise Server 115

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!