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.

Enterprise Beans<br />

1052<br />

<strong>The</strong> AccountControllerBean session bean has two get methods. <strong>The</strong> getAccountsOfCustomer<br />

method returns all of the accounts of a given customer by invoking the getAccounts method of<br />

the Account entity. Instead of implementing a get method for every instance variable, the<br />

AccountControllerBean has a getDetails method that returns an object (AccountDetails)<br />

that encapsulates the entire state of an Account entity. Because it can invoke a single method to<br />

retrieve the entire state, the client avoids the overhead associated with multiple remote calls.<br />

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

A client creates a Customer entity by invoking the createCustomer method of the<br />

CustomerControllerBean session bean. To remove a customer, the client calls the<br />

removeCustomer method, which invokes the EntityManager.remove method on the Customer<br />

instance.<br />

<strong>The</strong> CustomerControllerBean session bean has two methods that return multiple customers:<br />

getCustomersOfAccount and getCustomersOfLastName. getCustomersOfAccount calls the<br />

getCustomers method of the Account entity. getCustomersOfLastName uses the<br />

Customer.FindByLastName named query to search the database for customers with a matching<br />

last name, which is a named parameter to the query.<br />

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

<strong>The</strong> TxControllerBean session bean handles bank transactions. In addition to its get methods,<br />

getTxsOfAccount and getDetails, the TxControllerBean bean has several methods that<br />

change the balances of the bank accounts:<br />

■ withdraw<br />

■ deposit<br />

■ makeCharge<br />

■ makePayment<br />

■ transferFunds<br />

<strong>The</strong>se methods access an Account entity to verify the account type and to set the new balance.<br />

<strong>The</strong> withdraw and deposit methods are for standard accounts, whereas the makeCharge and<br />

makePayment methods are for accounts that include a line of credit. If the type method<br />

argument does not match the account, these methods throw an<br />

IllegalAccountTypeException. If a withdrawal were to result in a negative balance, the<br />

withdraw method throws an InsufficientFundsException. If a credit charge attempts to<br />

exceed the account’s credit line, the makeCharge method throws an<br />

InsufficientCreditException.<br />

<strong>The</strong> transferFunds method also checks the account type and new balance; if necessary, it<br />

throws the same exceptions as the withdraw and makeCharge methods. <strong>The</strong> transferFunds<br />

method subtracts from the balance of one Account instance and adds the same amount to<br />

another instance. Both of these steps must complete to ensure data integrity. If either step fails,<br />

the entire operation is rolled back and the balances remain unchanged. <strong>The</strong> transferFunds<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

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

Saved successfully!

Ooh no, something went wrong!