📄 cmp3.html
字号:
The <code class="cCode">PlayerBean</code> class defines the access methods for the persistent fields as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public abstract String getPlayerId();public abstract void setPlayerId(String id);public abstract String getName();public abstract void setName(String name);public abstract String getPosition();public abstract void setPosition(String position);public abstract double getSalary();public abstract void setSalary(double salary);<a name="wp79883"> </a></pre></div><a name="wp79884"> </a><p class="pBody">The name of an access method begins with <code class="cCode">get</code> or <code class="cCode">set</code>, followed by the capitalized name of the persistent or relationship field. For example, the accessor methods for the <code class="cCode">salary</code> field are <code class="cCode">getSalary</code> and <code class="cCode">setSalary</code>. This naming convention is similar to that of JavaBeans components.</p><a name="wp79887"> </a><h5 class="pHeading4">Access Methods for Relationship Fields</h5><a name="wp79888"> </a><p class="pBody">In the <code class="cCode">RosterApp</code> application, since a player can belong to multiple teams, a <code class="cCode">PlayerEJB</code> instance may be related to many <code class="cCode">TeamEJB</code> instances. To specify this relationship, the deployment descriptor of <code class="cCode">PlayerEJB</code> defines a relationship field named <code class="cCode">teams</code>. In the <code class="cCode">PlayerBean</code> class, the access methods for the <code class="cCode">teams</code> relationship field are as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public abstract Collection getTeams();public abstract void setTeams(Collection teams);<a name="wp79890"> </a></pre></div><a name="wp79891"> </a><h4 class="pHeading3">Finder and Select Methods</h4><a name="wp83215"> </a><p class="pBody">Finder and select methods use EJB QL queries to return objects and state information of entity beans using container-managed persistence.</p><a name="wp83220"> </a><p class="pBody">A select method is similar to a finder method in the following ways:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79895"> </a><div class="pSmartList1"><li>A select method can return a local or remote interface (or a collection of interfaces).</li></div><a name="wp79896"> </a><div class="pSmartList1"><li>A select method queries a database.</li></div><a name="wp79898"> </a><div class="pSmartList1"><li>The deployment descriptor specifies an EJB QL query for a select method.</li></div><a name="wp79899"> </a><div class="pSmartList1"><li>The entity bean class does not implement the select method.</li></div></ul></div><a name="wp79900"> </a><p class="pBody">However, a select method differs significantly from a finder method:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79901"> </a><div class="pSmartList1"><li>A select method can return a persistent field (or a collection thereof) of a related entity bean. A finder method can return only a local or remote interface (or a collection of interfaces).</li></div><a name="wp79902"> </a><div class="pSmartList1"><li>Since it is not exposed in any of the local or remote interfaces, a select method cannot be invoked by a client. It can be invoked only by the methods implemented within the entity bean class. A select method is usually invoked by either a business or home method.</li></div><a name="wp79903"> </a><div class="pSmartList1"><li>A select method is defined in the entity bean class. For bean-managed persistence, a finder method is defined in the entity bean class, but for container-managed persistence it is not.</li></div></ul></div><a name="wp79904"> </a><p class="pBody">The <code class="cCode">PlayerBean</code> class defines these select methods:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public abstract Collection ejbSelectLeagues(LocalPlayer player) throws FinderException;public abstract Collection ejbSelectSports(LocalPlayer player) throws FinderException;<a name="wp79905"> </a></pre></div><a name="wp79906"> </a><p class="pBody">The signature for a select method must follow these rules:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79907"> </a><div class="pSmartList1"><li>The prefix of the method name must be <code class="cCode">ejbSelect</code>.</li></div><a name="wp79908"> </a><div class="pSmartList1"><li>The access control modifier must be <code class="cCode">public</code>.</li></div><a name="wp79909"> </a><div class="pSmartList1"><li>The method must be declared as <code class="cCode">abstract</code>.</li></div><a name="wp79910"> </a><div class="pSmartList1"><li>The <code class="cCode">throws</code> clause must include the <code class="cCode">javax.ejb.FinderException</code>.</li></div></ul></div><a name="wp79911"> </a><h4 class="pHeading3">Business Methods</h4><a name="wp79912"> </a><p class="pBody">Since clients cannot invoke select methods, the <code class="cCode">PlayerBean</code> class wraps them in the <code class="cCode">getLeagues</code> and <code class="cCode">getSports</code> business methods:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public Collection getLeagues() throws FinderException { LocalPlayer player = (team.LocalPlayer)context.getEJBLocalObject(); return ejbSelectLeagues(player);}public Collection getSports() throws FinderException { LocalPlayer player = (team.LocalPlayer)context.getEJBLocalObject(); return ejbSelectSports(player);}<a name="wp79914"> </a></pre></div><a name="wp79915"> </a><h4 class="pHeading3">Entity Bean Methods</h4><a name="wp79916"> </a><p class="pBody">Because the container handles persistence, the life-cycle methods in the <code class="cCode">PlayerBean</code> class are nearly empty.</p><a name="wp79918"> </a><p class="pBody">The <code class="cCode">ejbCreate</code> method initializes the bean instance by assigning the input arguments to the persistent fields. At the end of the transaction that contains the create call, the container inserts a row into the database. Here is the source code for the <code class="cCode">ejbCreate</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public String ejbCreate (String id, String name, String position, double salary) throws CreateException { setPlayerId(id); setName(name); setPosition(position); setSalary(salary); return null;}<a name="wp79920"> </a></pre></div><a name="wp83227"> </a><p class="pBody">The <code class="cCode">ejbPostCreate</code> method returns <code class="cCode">void</code> and it has the same input parameters as the <code class="cCode">ejbCreate</code> method. If you want to set a relationship field to initialize the bean instance, you should do so in the <code class="cCode">ejbPostCreate</code> method. You may not set a relationship field in the <code class="cCode">ejbCreate</code> method.</p><a name="wp79922"> </a><p class="pBody">Except for a debug statement, the <code class="cCode">ejbRemove</code> method in the <code class="cCode">PlayerBean</code> class is empty. The container invokes <code class="cCode">ejbRemove</code> before removing the entity object.</p><a name="wp79925"> </a><p class="pBody">The container automatically synchronizes the state of the entity bean with the database. After the container loads the bean's state from the database, it invokes the <code class="cCode">ejbLoad</code> method. In like manner, before storing the state in the database, the container invokes the <code class="cCode">ejbStore</code> method.</p><a name="wp79929"> </a><h3 class="pHeading2">Local Home Interface</h3><a name="wp79931"> </a><p class="pBody">The local home interface defines the <code class="cCode">create</code>, finder, and home methods that may be invoked by local clients. </p><a name="wp79933"> </a><p class="pBody">The syntax rules for a <code class="cCode">create</code> method follow:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79934"> </a><div class="pSmartList1"><li>The name begins with <code class="cCode">create</code>.</li></div><a name="wp79935"> </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 entity bean class. </li></div><a name="wp79936"> </a><div class="pSmartList1"><li>It returns the local interface type of the entity bean. </li></div><a name="wp79937"> </a><div class="pSmartList1"><li>The <code class="cCode">throws</code> clause includes the exceptions specified by the <code class="cCode">throws</code> clause of the corresponding <code class="cCode">ejbCreate</code> method.</li></div><a name="wp79938"> </a><div class="pSmartList1"><li>The <code class="cCode">throws</code> clause contains the <code class="cCode">javax.ejb.CreateException</code>. </li></div></ul></div><a name="wp79940"> </a><p class="pBody">These rules apply for a finder method:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79941"> </a><div class="pSmartList1"><li>The name begins with <code class="cCode">find</code>.</li></div><a name="wp79942"> </a><div class="pSmartList1"><li>The return type is the entity bean's local interface type, or a collection of those types. </li></div><a name="wp79943"> </a><div class="pSmartList1"><li>The <code class="cCode">throws</code> clause contains the <code class="cCode">javax.ejb.FinderException</code>.</li></div><a name="wp79944"> </a><div class="pSmartList1"><li>The <code class="cCode">findByPrimaryKey</code> method must be defined.</li></div></ul></div><a name="wp79946"> </a><p class="pBody">An excerpt of the <code class="cCode">LocalPlayerHome</code> interface follows.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package team;import java.util.*;import javax.ejb.*;public interface LocalPlayerHome extends EJBLocalHome { public LocalPlayer create (String id, String name, String position, double salary) throws CreateException; public LocalPlayer findByPrimaryKey (String id) throws FinderException; public Collection findByPosition(String position) throws FinderException; ... public Collection findByLeague(LocalLeague league) throws FinderException; ... }<a name="wp79947"> </a></pre></div><a name="wp79949"> </a><h3 class="pHeading2">Local Interface</h3><a name="wp79953"> </a><p class="pBody">This interface defines the business and access methods that a local client may invoke. The <code class="cCode">PlayerBean</code> class implements two business methods: <code class="cCode">getLeagues</code> and <code class="cCode">getSports</code>. It also defines several <code class="cCode">get</code> and <code class="cCode">set</code> access methods for the persistent and relationship fields. The <code class="cCode">set</code> methods are hidden from the bean's clients because they are not defined in the <code class="cCode">LocalPlayer</code> interface. However, the <code class="cCode">get</code> methods are exposed to the clients by the interface:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package team;import java.util.*;import javax.ejb.*;public interface LocalPlayer extends EJBLocalObject { public String getPlayerId(); public String getName(); public String getPosition(); public double getSalary(); public Collection getTeams(); public Collection getLeagues() throws FinderException; public Collection getSports() throws FinderException;}<a name="wp79955"> </a></pre></div> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <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="CMP2.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="CMP4.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"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -