📄 bmp2.html
字号:
The ejbRemove Method</h4><a name="wp79784"> </a><p class="pBody">A client deletes an entity bean by invoking the <code class="cCode">remove</code> method. This invocation causes the EJB container to call the <code class="cCode">ejbRemove</code> method, which deletes the entity state from the database. In the <code class="cCode">SavingsAccountBean</code> class, the <code class="cCode">ejbRemove</code> method invokes a private method named <code class="cCode">deleteRow</code>, which issues a SQL <code class="cCode">DELETE</code> statement. The <code class="cCode">ejbRemove</code> method is short:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void ejbRemove() { try { deleteRow(id); catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } }}<a name="wp79789"> </a></pre></div><a name="wp79791"> </a><p class="pBody">If the <code class="cCode">ejbRemove</code> method encounters a system problem, it should throw the <code class="cCode">javax.ejb.EJBException</code>. If it encounters an application error, it should throw a <code class="cCode">javax.ejb.RemoveException</code>. For a comparison of system and application exceptions, see the section <a href="BMP5.html#wp81421">deploytool Tips for Entity Beans with Bean-Managed Persistence</a>.</p><a name="wp79795"> </a><p class="pBody">An entity bean may also be removed directly by a database deletion. For example, if a SQL script deletes a row that contains an entity bean state, then that entity bean is removed.</p><a name="wp79798"> </a><h4 class="pHeading3">The ejbLoad and ejbStore Methods</h4><a name="wp79799"> </a><p class="pBody">If the EJB container needs to synchronize the instance variables of an entity bean with the corresponding values stored in a database, it invokes the <code class="cCode">ejbLoad</code> and <code class="cCode">ejbStore</code> methods. The <code class="cCode">ejbLoad</code> method refreshes the instance variables from the database, and the <code class="cCode">ejbStore</code> method writes the variables to the database. The client may not call <code class="cCode">ejbLoad</code> and <code class="cCode">ejbStore</code>.</p><a name="wp79801"> </a><p class="pBody">If a business method is associated with a transaction, the container invokes <code class="cCode">ejbLoad</code> before the business method executes. Immediately after the business method executes, the container calls <code class="cCode">ejbStore</code>. Because the container invokes <code class="cCode">ejbLoad</code> and <code class="cCode">ejbStore</code>, you do not have to refresh and store the instance variables in your business methods. The <code class="cCode">SavingsAccountBean</code> class relies on the container to synchronize the instance variables with the database. Therefore, the business methods of <code class="cCode">SavingsAccountBean</code> should be associated with transactions.</p><a name="wp79802"> </a><p class="pBody">If the <code class="cCode">ejbLoad</code> and <code class="cCode">ejbStore</code> methods cannot locate an entity in the underlying database, they should throw the <code class="cCode">javax.ejb.NoSuchEntityException</code>. This exception is a subclass of <code class="cCode">EJBException</code>. Because <code class="cCode">EJBException</code> is a subclass of <code class="cCode">RuntimeException</code>, you do not have to include it in the <code class="cCode">throws</code> clause. When <code class="cCode">NoSuchEntityException</code> is thrown, the EJB container wraps it in a <code class="cCode">RemoteException</code> before returning it to the client.</p><a name="wp79803"> </a><p class="pBody">In the <code class="cCode">SavingsAccountBean</code> class, <code class="cCode">ejbLoad</code> invokes the <code class="cCode">loadRow</code> method, which issues a SQL <code class="cCode">SELECT</code> statement and assigns the retrieved data to the instance variables. The <code class="cCode">ejbStore</code> method calls the <code class="cCode">storeRow</code> method, which stores the instance variables in the database with a SQL <code class="cCode">UPDATE</code> statement. Here is the code for the <code class="cCode">ejbLoad</code> and <code class="cCode">ejbStore</code> methods:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void ejbLoad() { try { loadRow(); } catch (Exception ex) { throw new EJBException("ejbLoad: " + ex.getMessage()); }}public void ejbStore() { try { storeRow(); } catch (Exception ex) { throw new EJBException("ejbStore: " + ex.getMessage()); }}<a name="wp79805"> </a></pre></div><a name="wp79806"> </a><h4 class="pHeading3">The Finder Methods</h4><a name="wp79808"> </a><p class="pBody">The finder methods allow clients to locate entity beans. The <code class="cCode">SavingsAccountClient</code> program locates entity beans with three finder methods:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SavingsAccount jones = home.findByPrimaryKey("836");...Collection c = home.findByLastName("Smith");...Collection c = home.findInRange(20.00, 99.00);<a name="wp79809"> </a></pre></div><a name="wp79810"> </a><p class="pBody">For every finder method available to a client, the entity bean class must implement a corresponding method that begins with the prefix <code class="cCode">ejbFind</code>. The <code class="cCode">SavingsAccountBean</code> class, for example, implements the <code class="cCode">ejbFindByLastName</code> method as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public Collection ejbFindByLastName(String lastName) throws FinderException { Collection result; try { result = selectByLastName(lastName); } catch (Exception ex) { throw new EJBException("ejbFindByLastName " + ex.getMessage()); } return result;}<a name="wp79812"> </a></pre></div><a name="wp79813"> </a><p class="pBody">The finder methods that are specific to your application, such as <code class="cCode">ejbFindByLastName</code> and <code class="cCode">ejbFindInRange</code>, are optional--but the <code class="cCode">ejbFindByPrimaryKey</code> method is required. As its name implies, the <code class="cCode">ejbFindByPrimaryKey</code> method accepts as an argument the primary key, which it uses to locate an entity bean. In the <code class="cCode">SavingsAccountBean</code> class, the primary key is the <code class="cCode">id</code> variable. Here is the code for the <code class="cCode">ejbFindByPrimaryKey</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public String ejbFindByPrimaryKey(String primaryKey) throws FinderException { boolean result; try { result = selectByPrimaryKey(primaryKey); } catch (Exception ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); } if (result) { return primaryKey; } else { throw new ObjectNotFoundException ("Row for id " + primaryKey + " not found."); }}<a name="wp79816"> </a></pre></div><a name="wp79817"> </a><p class="pBody">The <code class="cCode">ejbFindByPrimaryKey</code> method may look strange to you, because it uses a primary key for both the method argument and return value. However, remember that the client does not call <code class="cCode">ejbFindByPrimaryKey</code> directly. It is the EJB container that calls the <code class="cCode">ejbFindByPrimaryKey</code> method. The client invokes the <code class="cCode">findByPrimaryKey</code> method, which is defined in the home interface.</p><a name="wp79818"> </a><p class="pBody">The following list summarizes the rules for the finder methods that you implement in an entity bean class with bean-managed persistence:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79819"> </a><div class="pSmartList1"><li>The <code class="cCode">ejbFindByPrimaryKey</code> method must be implemented.</li></div><a name="wp79820"> </a><div class="pSmartList1"><li>A finder method name must start with the prefix <code class="cCode">ejbFind</code>.</li></div><a name="wp79821"> </a><div class="pSmartList1"><li>The access control modifier must be <code class="cCode">public</code>.</li></div><a name="wp79822"> </a><div class="pSmartList1"><li>The method modifier cannot be <code class="cCode">final</code> or <code class="cCode">static</code>.</li></div><a name="wp79823"> </a><div class="pSmartList1"><li>The arguments and return type must be legal types for the Java RMI API. (This requirement applies only to methods defined in a remote--not local--home interface.)</li></div><a name="wp79824"> </a><div class="pSmartList1"><li>The return type must be the primary key or a collection of primary keys.</li></div></ul></div><a name="wp79826"> </a><p class="pBody">The <code class="cCode">throws</code> clause may include the <code class="cCode">javax.ejb.FinderException</code> and exceptions that are specific to your application. If a finder method returns a single primary key and the requested entity does not exist, the method should throw the <code class="cCode">javax.ejb.ObjectNotFoundException</code> (a subclass of <code class="cCode">FinderException</code>). If a finder method returns a collection of primary keys and it does not find any objects, it should return an empty collection.</p><a name="wp79828"> </a><h4 class="pHeading3">The Business Methods</h4><a name="wp79829"> </a><p class="pBody">The business methods contain the business logic that you want to encapsulate within the entity bean. Usually, the business methods do not access the database, allowing you to separate the business logic from the database access code. The <code class="cCode">SavingsAccountBean</code> class contains the following business methods:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void debit(BigDecimal amount) throws InsufficientBalanceException { if (balance.compareTo(amount) == -1) { throw new InsufficientBalanceException(); } balance = balance.subtract(amount);}public void credit(BigDecimal amount) { balance = balance.add(amount);} public String getFirstName() { return firstName;} public String getLastName() { return lastName;} public BigDecimal getBalance() { return balance;}<a name="wp79832"> </a></pre></div><a name="wp79833"> </a><p class="pBody">The <code class="cCode">SavingsAccountClient</code> program invokes the business methods as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">BigDecimal zeroAmount = new BigDecimal("0.00");SavingsAccount duke = home.create("123", "Duke", "Earl", zeroAmount);...duke.credit(new BigDecimal("88.50"));duke.debit(new BigDecimal("20.25"));BigDecimal balance = duke.getBalance();<a name="wp79834"> </a></pre></div><a name="wp79835"> </a><p class="pBody">The requirements for the signature of a business method are the same for both session and entity beans:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79836"> </a><div class="pSmartList1"><li>The method name must not conflict with a method name defined by the EJB architecture. For example, you cannot call a business method <code class="cCode">ejbCreate</code> or <code class="cCode">ejbActivate</code>. </li></div><a name="wp79837"> </a><div class="pSmartList1"><li>The access control modifier must be <code class="cCode">public</code>.</li></div><a name="wp79838"> </a><div class="pSmartList1"><li>The method modifier cannot be <code class="cCode">final</code> or <code class="cCode">static</code>.</li></div><a name="wp79839"> </a><div class="pSmartList1"><li>The arguments and return types must be legal types for the Java RMI API. This requirement applies only to methods defined in a remote--not local--home interface.</li></div></ul></div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -