12.11.2014 Views

web server - Borland Technical Publications

web server - Borland Technical Publications

web server - Borland Technical Publications

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Client view of an enterprise bean<br />

Every entity bean instance must have a primary key that uniquely identifies it. An entity<br />

bean instance can also have secondary keys that can be used to locate a particular<br />

entity object.<br />

Find methods and primary key class<br />

The default find method for an entity bean is the findByPrimaryKey() method, which<br />

locates the entity object using its primary key value. Its signature is as follows:<br />

findByPrimaryKey( primaryKey )<br />

Every entity bean must implement a findByPrimaryKey() method. The primaryKey<br />

parameter is a separate primary key class that is defined in the deployment descriptor.<br />

The key type is the type for the primary key, and it must be a legal value type in RMI-<br />

IIOP. The primary key class can be any class--a Java class or a class you've written<br />

yourself.<br />

For example, you have an Account entity bean that defines the primary key class<br />

AccountPK. AccountPK is a String type, and it holds the identifier for the Account bean.<br />

You can obtain a reference to a specific Account entity bean instance by setting the<br />

AccountPK to the account identifier and invoking the findByPrimaryKey() method, as<br />

shown in the following code sample.<br />

AccountPK accountPK = new AccountPK("1234-56-789");<br />

Account source = accountHome.findByPrimaryKey( accountPK );<br />

The bean provider can define additional finder methods that a client can use.<br />

Create and remove methods<br />

A client can also create entity beans using create methods defined in the home<br />

interface. When a client invokes a create() method for an entity bean, the new instance<br />

of the entity object is saved in the data store. The new entity object always has a<br />

primary key value that is its identifier. Its state may be initialized to values passed as<br />

parameters to the create() method.<br />

Keep in mind that an entity bean exists for as long as data is present in the database.<br />

The life of the entity bean is not bound by the client's session. The entity bean can be<br />

removed by invoking one of the bean's remove() methods--these methods remove the<br />

bean and the underlying representation of the entity data from the database. It is also<br />

possible to directly delete an entity object, such as by deleting a database record using<br />

the DBMS or with a legacy application.<br />

Invoking methods<br />

Once the client has obtained a reference to the bean's remote interface, the client can<br />

invoke the methods defined in the remote interface for this enterprise bean. The<br />

methods pertaining to the bean's business logic are of most interest to the client. There<br />

are also methods for getting information about the bean and its interfaces, getting the<br />

bean object's handle, testing if one bean is identical to another bean, and methods for<br />

removing the bean instance.<br />

The next code sample illustrates how a client calls methods of an enterprise bean, in<br />

this case, a cart session bean. We pick up the client code from the point where it has<br />

created a new session bean instance for a card holder and retrieved a Cart reference<br />

to the remote interface. At this point, the client is ready to invoke the bean methods.<br />

First, the client creates a new book object, setting its title and price parameters. Then, it<br />

invokes the enterprise bean business method addItem() to add the book object to a<br />

shopping cart. The addItem() method is defined on the CartBean session bean, and is<br />

made public through the Cart remote interface. The client adds other items (not shown<br />

here), then calls its own summarize() method to list the items in the shopping cart. This<br />

is followed by the remove() method to remove the bean instance. Notice that a client<br />

88 BES Developer’s Guide

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

Saved successfully!

Ooh no, something went wrong!