📄 ebank2.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Enterprise Beans</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="Ebank.html" /> <link rel="Next" href="Ebank3.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="Ebank.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="Ebank3.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp79750"> </a><h2 class="pHeading1">Enterprise Beans</h2><a name="wp79755"> </a><p class="pBody"><a href="Ebank2.html#wp79762">Figure 32-2</a> takes a closer look at the access paths between the clients, enterprise beans, and database tables. As you can see, the end-user clients (Web and J2EE application clients) access only the session beans. Within the enterprise bean tier, the session beans are clients of the entity beans. On the back end of the application, the entity beans access the database tables that store the entity states. </p><a name="wp79756"> </a><p class="pBody">The source code for these enterprise beans is in the <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/bank/src/com/sun/ebank/ejb/</code> directory.</p><a name="wp79760"> </a><p class="pBody"></p><div align="left"><img src="images/Fig392.gif" height="296" width="444" alt="Duke's Bank Enterprise Beans" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p> <a name="79762"> </a><strong><font >Figure 32-2 Enterprise Beans</font></strong></p><a name="wp79763"> </a><h3 class="pHeading2">Session Beans</h3><a name="wp79765"> </a><p class="pBody">The Duke's Bank application has three session beans: <code class="cCode">AccountControllerBean</code>, <code class="cCode">CustomerControllerBean</code>, and <code class="cCode">TxControllerBean</code>. (<code class="cCode">Tx</code> stands for a business transaction, such as transferring funds.) These session beans provide a client's view of the application's business logic. Hidden from the clients are the server-side routines that implement the business logic, access databases, manage relationships, and perform error checking.</p><a name="wp79766"> </a><h4 class="pHeading3">AccountControllerBean</h4><a name="wp79767"> </a><p class="pBody">The business methods of the <code class="cCode">AccountControllerBean</code> session bean perform tasks that fall into the following categories: creating and removing entity beans, managing the account-customer relationship, and getting the account information.</p><a name="wp79768"> </a><p class="pBody">The following methods create and remove entity beans:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79769"> </a><div class="pSmartList1"><li><code class="cCode">createAccount</code></li></div><a name="wp79770"> </a><div class="pSmartList1"><li><code class="cCode">removeAccount</code></li></div></ul></div><a name="wp79771"> </a><p class="pBody">These methods of the <code class="cCode">AccountControllerBean</code> session bean call the <code class="cCode">create</code> and <code class="cCode">remove</code> methods of the <code class="cCode">AccountBean</code> entity bean. The <code class="cCode">createAccount</code> and <code class="cCode">removeAccount</code> methods throw application exceptions to indicate invalid method arguments. The <code class="cCode">createAccount</code> method throws an <code class="cCode">IllegalAccountTypeException</code> if the type argument is neither <code class="cCode">Checking</code>, <code class="cCode">Savings</code>, <code class="cCode">Credit</code>, nor <code class="cCode">Money Market</code>. The <code class="cCode">createAccount</code> method also verifies that the specified customer exists by invoking the <code class="cCode">findByPrimaryKey</code> method of the <code class="cCode">CustomerBean</code> entity bean. If the result of this verification is <code class="cCode">false</code>, the <code class="cCode">createAccount</code> method throws a <code class="cCode">CustomerNotFoundException</code>.</p><a name="wp79772"> </a><p class="pBody">The following methods manage the account-customer relationship:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79773"> </a><div class="pSmartList1"><li><code class="cCode">addCustomerToAccount</code></li></div><a name="wp79774"> </a><div class="pSmartList1"><li><code class="cCode">removeCustomerFromAccount</code></li></div></ul></div><a name="wp79775"> </a><p class="pBody">The <code class="cCode">AccountBean</code> and <code class="cCode">CustomerBean</code> entity beans have a many-to-many relationship. A bank account may be jointly held by more than one customer, and a customer may have multiple accounts. Because the entity beans use bean-managed persistence, there are several ways to manage this relationship. For more information, see <a href="BMP3.html#wp80001">Mapping Table Relationships for Bean-Managed Persistence</a>.</p><a name="wp79779"> </a><p class="pBody">In the Duke's Bank application, the <code class="cCode">addCustomerToAccount</code> and <code class="cCode">removeCustomerFromAccount</code> methods of the <code class="cCode">AccountControllerBean</code> session bean manage the account-customer relationship. The <code class="cCode">addCustomerToAccount</code> method, for example, starts by verifying that the customer exists. To create the relationship, the <code class="cCode">addCustomerToAccount</code> method inserts a row into the <code class="cCode">customer_account_xref</code> database table. In this cross-reference table, each row contains the <code class="cCode">customerId</code> and <code class="cCode">accountId</code> of the related entities. To remove a relationship, the <code class="cCode">removeCustomerFromAccount</code> method deletes a row from the <code class="cCode">customer_account_xref</code> table. If a client calls the <code class="cCode">removeAccount</code> method, then all rows for the specified <code class="cCode">accountId</code> are removed from the <code class="cCode">customer_account_xref</code> table.</p><a name="wp79780"> </a><p class="pBody">The following methods get the account information:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79781"> </a><div class="pSmartList1"><li><code class="cCode">getAccountsOfCustomer</code></li></div><a name="wp79782"> </a><div class="pSmartList1"><li><code class="cCode">getDetails</code></li></div></ul></div><a name="wp79783"> </a><p class="pBody">The <code class="cCode">AccountControllerBean</code> session bean has two <code class="cCode">get</code> methods. The <code class="cCode">getAccountsOfCustomer</code> method returns all of the accounts of a given customer by invoking the <code class="cCode">findByCustomer</code> method of the <code class="cCode">AccountBean</code> entity bean. Instead of implementing a <code class="cCode">get</code> method for every instance variable, the <code class="cCode">AccountControllerBean</code> has a <code class="cCode">getDetails</code> method that returns an object (<code class="cCode">AccountDetails</code>) that encapsulates the entire state of an <code class="cCode">AccountBean</code> bean. Because it can invoke a single method to retrieve the entire state, the client avoids the overhead associated with multiple remote calls.</p><a name="wp79784"> </a><h4 class="pHeading3">CustomerControllerBean</h4><a name="wp79785"> </a><p class="pBody">Because it is the <code class="cCode">AccountControllerBean</code> enterprise bean that manages the customer-account relationship, <code class="cCode">CustomerControllerBean</code> is the simpler of these two session beans. A client creates a <code class="cCode">CustomerBean</code> entity bean by invoking the <code class="cCode">createCustomer</code> method of the <code class="cCode">CustomerControllerBean</code> session bean. To remove a customer, the client calls the <code class="cCode">removeCustomer</code> method, which not only invokes the <code class="cCode">remove</code> method of <code class="cCode">CustomerBean</code>, but also deletes from the <code class="cCode">customer_account_xref</code> table all rows that identify the customer.</p><a name="wp79786"> </a><p class="pBody">The <code class="cCode">CustomerControllerBean</code> session bean has two methods that return multiple customers: <code class="cCode">getCustomersOfAccount</code> and <code class="cCode">getCustomersOfLastName</code>. These methods call the corresponding finder methods--<code class="cCode">findbyAccountId</code> and <code class="cCode">findByLastName</code>--of <code class="cCode">CustomerBean</code>.</p><a name="wp79787"> </a><h4 class="pHeading3">TxControllerBean</h4><a name="wp79788"> </a><p class="pBody">The <code class="cCode">TxControllerBean</code> session bean handles bank transactions. In addition to its <code class="cCode">get</code> methods, <code class="cCode">getTxsOfAccount</code> and <code class="cCode">getDetails</code>, the <code class="cCode">TxControllerBean</code> bean has several methods that change the balances of the bank accounts:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79789"> </a><div class="pSmartList1"><li><code class="cCode">withdraw</code></li></div><a name="wp79790"> </a><div class="pSmartList1"><li><code class="cCode">deposit</code></li></div><a name="wp79791"> </a><div class="pSmartList1"><li><code class="cCode">makeCharge</code></li></div><a name="wp79792"> </a><div class="pSmartList1"><li><code class="cCode">makePayment</code></li></div><a name="wp79793"> </a><div class="pSmartList1"><li><code class="cCode">transferFunds</code></li></div></ul></div><a name="wp83220"> </a><p class="pBody">These methods access an <code class="cCode">AccountBean</code> entity bean to verify the account type and to set the new balance. The <code class="cCode">withdraw</code> and <code class="cCode">deposit</code> methods are for standard accounts, whereas the <code class="cCode">makeCharge</code> and <code class="cCode">makePayment</code> methods are for accounts that include a line of credit. If the <code class="cCode">type</code> method argument does not match the account, these methods throw an <code class="cCode">IllegalAccountTypeException</code>. If a withdrawal were to result in a negative balance, the <code class="cCode">withdraw</code> method throws an <code class="cCode">InsufficientFundsException</code>. If a credit charge attempts to exceed the account's credit line, the <code class="cCode">makeCharge</code> method throws an <code class="cCode">InsufficientCreditException</code>.</p><a name="wp79795"> </a><p class="pBody">The <code class="cCode">transferFunds</code> method also checks the account type and new balance; if necessary, it throws the same exceptions as the <code class="cCode">withdraw</code> and <code class="cCode">makeCharge</code> methods. The <code class="cCode">transferFunds</code> method subtracts from the balance of one <code class="cCode">AccountBean</code> instance and adds the same amount to another instance. Because both of these steps must complete, the <code class="cCode">transferFunds</code> method has a <code class="cCode">Required</code> transaction attribute. If either step fails, the entire operation is rolled back and the balances remain unchanged. </p><a name="wp79797"> </a><h3 class="pHeading2">Entity Beans</h3><a name="wp79798"> </a><p class="pBody">For each business entity represented in our simple bank, the Duke's Bank application has a matching entity bean: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79800"> </a><div class="pSmartList1"><li><code class="cCode">AccountBean</code></li></div><a name="wp79801"> </a><div class="pSmartList1"><li><code class="cCode">CustomerBean</code></li></div><a name="wp79802"> </a><div class="pSmartList1"><li><code class="cCode">TxBean</code></li></div></ul></div><a name="wp79803"> </a><p class="pBody">The purpose of these beans is to provide an object view of these database tables: <code class="cCode">account</code>, <code class="cCode">customer</code>, and <code class="cCode">tx</code>. For each column in a table, the corresponding entity bean has an instance variable. Because they use bean-managed persistence, the entity beans contain the SQL statements that access the tables. For example, the <code class="cCode">create</code> method of the <code class="cCode">CustomerBean</code> entity bean calls the SQL <code class="cCode">INSERT</code> command.</p><a name="wp79804"> </a><p class="pBody">Unlike the session beans, the entity beans do not validate method parameters (except for the primary key parameter of <code class="cCode">ejbCreate</code>). During the design phase, we decided that the session beans would check the parameters and throw the application exceptions, such as <code class="cCode">CustomerNotInAccountException</code> and <code class="cCode">IllegalAccountTypeException</code>. Consequently, if some other application were to include these entity beans, its session beans would also have to validate the method parameters.</p><a name="wp84209"> </a><p class="pBody">Because the entity beans always run in the same Java VM as their clients the session beans, for improved performance, the entity beans are coded with local interfaces. </p><a name="wp79805"> </a><h3 class="pHeading2">Helper Classes</h3><a name="wp79807"> </a><p class="pBody">The EJB JAR files include several helper classes that are used by the enterprise beans. The source code for these classes is in the <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/bank/src/com/sun/ebank/util/</code> directory. <a href="Ebank2.html#wp79817">Table 32-3</a> briefly describes the helper classes.</p><div align="left"><table border="1" summary="Enterprise Bean Helper Classes" id="wp79817"> <caption><a name="wp79817"> </a><div class="pTableTitle">Table 32-3 Helper Classes for the Application's Enterprise Beans </div></caption> <tr align="center"> <th><a name="wp79821"> </a><div class="pCellHeading">Class Name</div></th>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -