📄 bmp2.html
字号:
<a name="wp79840"> </a><p class="pBody">The <code class="cCode">throws</code> clause may include the exceptions that you define for your application. The <code class="cCode">debit</code> method, for example, throws the <code class="cCode">InsufficientBalanceException</code>. To indicate a system-level problem, a business method should throw the <code class="cCode">javax.ejb.EJBException</code>.</p><a name="wp79843"> </a><h4 class="pHeading3">The Home Methods</h4><a name="wp79845"> </a><p class="pBody">A home method contains the business logic that applies to all entity beans of a particular class. In contrast, the logic in a business method applies to a single entity bean, an instance with a unique identity. During a home method invocation, the instance has neither a unique identity nor a state that represents a business object. Consequently, a home method must not access the bean's persistence state (instance variables). (For container-managed persistence, a home method also must not access relationships.) </p><a name="wp79846"> </a><p class="pBody">Typically, a home method locates a collection of bean instances and invokes business methods as it iterates through the collection. This approach is taken by the <code class="cCode">ejbHomeChargeForLowBalance</code> method of the <code class="cCode">SavingsAccountBean</code> class. The <code class="cCode">ejbHomeChargeForLowBalance</code> method applies a service charge to all savings accounts with balances less than a specified amount. The method locates these accounts by invoking the <code class="cCode">findInRange</code> method. As it iterates through the collection of <code class="cCode">SavingsAccount</code> instances, the <code class="cCode">ejbHomeChargeForLowBalance</code> method checks the balance and invokes the <code class="cCode">debit</code> business method. Here is the source code of the <code class="cCode">ejbHomeChargeForLowBalance</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void ejbHomeChargeForLowBalance( BigDecimal minimumBalance, BigDecimal charge) throws InsufficientBalanceException { try { SavingsAccountHome home = (SavingsAccountHome)context.getEJBHome(); Collection c = home.findInRange(new BigDecimal("0.00"), minimumBalance.subtract(new BigDecimal("0.01"))); Iterator i = c.iterator(); while (i.hasNext()) { SavingsAccount account = (SavingsAccount)i.next(); if (account.getBalance().compareTo(charge) == 1) { account.debit(charge); } } } catch (Exception ex) { throw new EJBException("ejbHomeChargeForLowBalance: " + ex.getMessage()); } } <a name="wp79847"> </a></pre></div><a name="wp79849"> </a><p class="pBody">The home interface defines a corresponding method named <code class="cCode">chargeForLowBalance</code> (see <a href="BMP2.html#wp79937">Home Method Definitions</a>). Since the interface provides the client view, the <code class="cCode">SavingsAccountClient</code> program invokes the home method as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SavingsAccountHome home;...home.chargeForLowBalance(new BigDecimal("10.00"), new BigDecimal("1.00"));<a name="wp79853"> </a></pre></div><a name="wp79854"> </a><p class="pBody">In the entity bean class, the implementation of a home method must adhere to these rules:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79855"> </a><div class="pSmartList1"><li>A home method name must start with the prefix <code class="cCode">ejbHome</code>.</li></div><a name="wp79856"> </a><div class="pSmartList1"><li>The access control modifier must be <code class="cCode">public</code>.</li></div><a name="wp79857"> </a><div class="pSmartList1"><li>The method modifier cannot be <code class="cCode">static</code>.</li></div></ul></div><a name="wp79858"> </a><p class="pBody">The <code class="cCode">throws</code> clause may include exceptions that are specific to your application; it must not throw the <code class="cCode">java.rmi.RemoteException</code>.</p><a name="wp79859"> </a><h4 class="pHeading3">Database Calls</h4><a name="wp79864"> </a><p class="pBody"><a href="BMP2.html#wp79879">Table 21-1</a> summarizes the database access calls in the <code class="cCode">SavingsAccountBean</code> class. The business methods of the <code class="cCode">SavingsAccountBean</code> class are absent from the preceding table because they do not access the database. Instead, these business methods update the instance variables, which are written to the database when the EJB container calls <code class="cCode">ejbStore</code>. Another developer might have chosen to access the database in the business methods of the <code class="cCode">SavingsAccountBean</code> class. This choice is one of those design decisions that depend on the specific needs of your application.</p><a name="wp79865"> </a><p class="pBody">Before accessing a database, you must connect to it. For more information, see Chapter <a href="Resources.html#wp79660">26</a><a href="Resources.html#wp79663"></a>.</p><div align="left"><table border="1" summary="SQL Statements in SavingsAccountBean" id="wp79879"> <caption><a name="wp79879"> </a><div class="pTableTitle">Table 21-1 SQL Statements in SavingsAccountBean </div></caption> <tr align="center"> <th><a name="wp79883"> </a><div class="pCellHeading">Method</div></th> <th><a name="wp79885"> </a><div class="pCellHeading">SQL Statement</div></th></tr> <tr align="left"> <td><a name="wp79887"> </a><div class="pCellBody"><code class="cCode">ejbCreate</code></div></td> <td><a name="wp79889"> </a><div class="pCellBody"><code class="cCode">INSERT</code></div></td></tr> <tr align="left"> <td><a name="wp79891"> </a><div class="pCellBody"><code class="cCode">ejbFindByPrimaryKey</code></div></td> <td><a name="wp79893"> </a><div class="pCellBody"><code class="cCode">SELECT</code></div></td></tr> <tr align="left"> <td><a name="wp79895"> </a><div class="pCellBody"><code class="cCode">ejbFindByLastName</code></div></td> <td><a name="wp79897"> </a><div class="pCellBody"><code class="cCode">SELECT</code></div></td></tr> <tr align="left"> <td><a name="wp79899"> </a><div class="pCellBody"><code class="cCode">ejbFindInRange</code></div></td> <td><a name="wp79901"> </a><div class="pCellBody"><code class="cCode">SELECT</code></div></td></tr> <tr align="left"> <td><a name="wp79903"> </a><div class="pCellBody"><code class="cCode">ejbLoad</code></div></td> <td><a name="wp79905"> </a><div class="pCellBody"><code class="cCode">SELECT</code></div></td></tr> <tr align="left"> <td><a name="wp79907"> </a><div class="pCellBody"><code class="cCode">ejbRemove</code></div></td> <td><a name="wp79909"> </a><div class="pCellBody"><code class="cCode">DELETE</code></div></td></tr> <tr align="left"> <td><a name="wp79911"> </a><div class="pCellBody"><code class="cCode">ejbStore</code></div></td> <td><a name="wp79913"> </a><div class="pCellBody"><code class="cCode">UPDATE</code></div></td></tr></table></div><p class="pBody"></p><a name="wp79915"> </a><h3 class="pHeading2">Home Interface</h3><a name="wp79916"> </a><p class="pBody">The home interface defines the <code class="cCode">create</code>, finder, and home methods. The <code class="cCode">SavingsAccountHome</code> interface follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import java.util.Collection;import java.math.BigDecimal;import java.rmi.RemoteException;import javax.ejb.*;public interface SavingsAccountHome extends EJBHome { public SavingsAccount create(String id, String firstName, String lastName, BigDecimal balance) throws RemoteException, CreateException; public SavingsAccount findByPrimaryKey(String id) throws FinderException, RemoteException; public Collection findByLastName(String lastName) throws FinderException, RemoteException; public Collection findInRange(BigDecimal low, BigDecimal high) throws FinderException, RemoteException; public void chargeForLowBalance(BigDecimal minimumBalance, BigDecimal charge) throws InsufficientBalanceException, RemoteException;}<a name="wp79918"> </a></pre></div><a name="wp79919"> </a><h4 class="pHeading3">create Method Definitions</h4><a name="wp79921"> </a><p class="pBody">Each <code class="cCode">create</code> method in the home interface must conform to the following requirements:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79922"> </a><div class="pSmartList1"><li>It has the same number and types of arguments as its matching <code class="cCode">ejbCreate</code> method in the enterprise bean class.</li></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -