10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

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.

Session Beans<br />

<strong>The</strong> Duke’s Bank application has three session beans: AccountControllerBean,<br />

CustomerControllerBean, and TxControllerBean.(Tx stands for a business transaction, such<br />

as transferring funds.) <strong>The</strong>se session beans provide a client’s view of the application’s business<br />

logic. Hidden from the clients are the server-side routines that implement the business logic,<br />

access databases, manage relationships, and perform error checking.<br />

<strong>The</strong> AccountControllerBean Session Bean<br />

<strong>The</strong> business methods of the AccountControllerBean session bean perform tasks that fall into<br />

the following categories: creating and removing entities, managing the account-customer<br />

relationship, and getting the account information.<br />

<strong>The</strong> following methods create and remove entities:<br />

■ createAccount<br />

■ removeAccount<br />

<strong>The</strong>se methods of the AccountControllerBean session bean call the create and remove<br />

methods of the Account entity. <strong>The</strong> createAccount and removeAccount methods throw<br />

application exceptions to indicate invalid method arguments. <strong>The</strong> createAccount method<br />

throws an IllegalAccountTypeException if the type argument is neither Checking, Savings,<br />

Credit, nor Money Market. <strong>The</strong> createAccount method also looks up the specified customer<br />

exists by invoking the EntityManager.find method. If the result of this verification is null, the<br />

createAccount method throws a CustomerNotFoundException.<br />

<strong>The</strong> following methods manage the account-customer relationship:<br />

■ addCustomerToAccount<br />

■ removeCustomerFromAccount<br />

<strong>The</strong> Account and Customer entities have a many-to-many relationship. A bank account can be<br />

jointly held by more than one customer, and a customer can have multiple accounts.<br />

In the Duke’s Bank application, the addCustomerToAccount and removeCustomerFromAccount<br />

methods of the AccountControllerBean session bean manage the account-customer<br />

relationship. <strong>The</strong> addCustomerToAccount method, for example, starts by verifying that the<br />

customer exists. To create the relationship, the addCustomerToAccount method first looks up<br />

the Customer and Account entities using the EntityManager.find method, then it calls the<br />

Account.addCustomer method to associate the customer with the account.<br />

<strong>The</strong> following methods get the account information:<br />

■ getAccountsOfCustomer<br />

■ getDetails<br />

Enterprise Beans<br />

Chapter 37 • <strong>The</strong> Duke’s Bank Application 1051

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

Saved successfully!

Ooh no, something went wrong!