📄 cmp4.html
字号:
Iterator i = players.iterator(); while (i.hasNext()) { LocalPlayer player = (LocalPlayer) i.next(); PlayerDetails details = new PlayerDetails(player.getPlayerId(), player.getName(), player.getPosition(), 0.00); playerList.add(details); } return playerList;}<a name="wp80177"> </a></pre></div><a name="wp80178"> </a><h3 class="pHeading2">Finding the Players by Position</h3><a name="wp80179"> </a><h4 class="pHeading3">1. RosterClient</h4><a name="wp80180"> </a><p class="pBody">The client starts the procedure by invoking the <code class="cCode">getPlayersByPosition</code> method of the <code class="cCode">RosterEJB</code> session bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">playerList = myRoster.getPlayersByPosition("defender");<a name="wp80181"> </a></pre></div><a name="wp80182"> </a><h4 class="pHeading3">2. RosterEJB</h4><a name="wp80183"> </a><p class="pBody">The <code class="cCode">getPlayersByPosition</code> method retrieves the <code class="cCode">players</code> list by invoking the <code class="cCode">findByPosition</code> method of the <code class="cCode">PlayerEJB</code> entity bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public ArrayList getPlayersByPosition(String position) { Collection players = null; try { players = playerHome.findByPosition(position); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } return copyPlayersToDetails(players);}<a name="wp80185"> </a></pre></div><a name="wp80186"> </a><h4 class="pHeading3">3. PlayerEJB</h4><a name="wp80187"> </a><p class="pBody">The <code class="cCode">LocalPlayerHome</code> interface defines the <code class="cCode">findByPosition</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public Collection findByPosition(String position) throws FinderException;<a name="wp80188"> </a></pre></div><a name="wp80189"> </a><p class="pBody">Because the <code class="cCode">PlayerEJB</code> entity bean uses container-managed persistence, the entity bean class (<code class="cCode">PlayerBean</code>) does not implement its finder methods. To specify the queries associated with the finder methods, EJB QL queries must be defined in the bean's deployment descriptor. For example, the <code class="cCode">findByPosition</code> method has this EJB QL query:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT DISTINCT OBJECT(p) FROM Player pWHERE p.position = ?1<a name="wp80191"> </a></pre></div><a name="wp80194"> </a><p class="pBody">At runtime, when the container invokes the <code class="cCode">findByPosition</code> method, it will execute the corresponding SQL <code class="cCode">SELECT</code> statement.</p><a name="wp80198"> </a><p class="pBody">For details about EJB QL, please refer to Chapter <a href="EJBQL.html#wp79663">24</a>. To learn how to view and edit an EJB QL query in <code class="cCode">deploytool</code>, see the section <a href="CMP6.html#wp83132">Finder/Select Methods Dialog Box (PlayerEJB)</a>.</p><a name="wp80202"> </a><h3 class="pHeading2">Getting the Sports of a Player</h3><a name="wp80203"> </a><h4 class="pHeading3">1. RosterClient</h4><a name="wp80204"> </a><p class="pBody">The client invokes the <code class="cCode">getSportsOfPlayer</code> method of the <code class="cCode">RosterEJB</code> session bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">sportList = myRoster.getSportsOfPlayer("P28");<a name="wp80205"> </a></pre></div><a name="wp80206"> </a><h4 class="pHeading3">2. RosterEJB</h4><a name="wp80207"> </a><p class="pBody">The <code class="cCode">getSportsOfPlayer</code> method returns an <code class="cCode">ArrayList</code> of <code class="cCode">String</code> objects that represent the sports of the specified player. It constructs the <code class="cCode">ArrayList</code> from a <code class="cCode">Collection</code> returned by the <code class="cCode">getSports</code> business method of the <code class="cCode">PlayerEJB entity </code>bean. Here is the source code for the <code class="cCode">getSportsOfPlayer</code> method of the <code class="cCode">RosterEJB</code> session bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public ArrayList getSportsOfPlayer(String playerId) { ArrayList sportsList = new ArrayList(); Collection sports = null; try { LocalPlayer player = playerHome.findByPrimaryKey(playerId); sports = player.getSports(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } Iterator i = sports.iterator(); while (i.hasNext()) { String sport = (String) i.next(); sportsList.add(sport); } return sportsList;}<a name="wp80208"> </a></pre></div><a name="wp80209"> </a><h4 class="pHeading3">3. PlayerEJB</h4><a name="wp80210"> </a><p class="pBody">The <code class="cCode">getSports</code> method is a wrapper for the <code class="cCode">ejbSelectSports</code> method. Since the parameter of the <code class="cCode">ejbSelectSports</code> method is of type <code class="cCode">LocalPlayer</code>, the <code class="cCode">getSports</code> method passes along a reference to the entity bean instance. The <code class="cCode">PlayerBean</code> class implements the <code class="cCode">getSports</code> method as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public Collection getSports() throws FinderException { LocalPlayer player = (team.LocalPlayer)context.getEJBLocalObject(); return ejbSelectSports(player);}<a name="wp80211"> </a></pre></div><a name="wp80213"> </a><p class="pBody">The <code class="cCode">PlayerBean</code> class defines the <code class="cCode">ejbSelectSports</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public abstract Collection ejbSelectSports(LocalPlayer player) throws FinderException;<a name="wp80214"> </a></pre></div><a name="wp80215"> </a><p class="pBody">The bean's deployment descriptor specifies the following EJB QL query for the <code class="cCode">ejbSelectSports</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT DISTINCT t.league.sportFROM Player p, IN (p.teams) AS tWHERE p = ?1<a name="wp80216"> </a></pre></div><a name="wp80217"> </a><p class="pBody">Because <code class="cCode">PlayerEJB</code> uses container-managed persistence, when the <code class="cCode">ejbSelectSports</code> method is invoked the EJB container will execute its corresponding SQL <code class="cCode">SELECT</code> statement.</p> </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="CMP3.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="CMP5.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 + -