📄 cmp4.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>Method Invocations in RosterApp</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="CMP3.html" /> <link rel="Next" href="CMP5.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="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"> <blockquote><a name="wp80101"> </a><h2 class="pHeading1">Method Invocations in RosterApp</h2><a name="wp80102"> </a><p class="pBody">To show how the various components interact, this section describes the sequence of method invocations that occur for particular functions. The source code for the components is in the <code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/cmproster</code> directory.</p><a name="wp80103"> </a><h3 class="pHeading2">Creating a Player</h3><a name="wp80104"> </a><h4 class="pHeading3">1. RosterClient</h4><a name="wp80105"> </a><p class="pBody">The <code class="cCode">RosterClient</code> invokes the <code class="cCode">createPlayer</code> business method of the <code class="cCode">RosterEJB</code> session bean to create a new player. In the following line of code, the type of the <code class="cCode">myRoster</code> object is <code class="cCode">Roster</code>, the remote interface of <code class="cCode">RosterEJB</code>. The argument of the <code class="cCode">createPlayer</code> method is a <code class="cCode">PlayerDetails</code> object, which encapsulates information about a particular player. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">myRoster.createPlayer(new PlayerDetails("P1", "Phil Jones", "goalkeeper", 100.00));<a name="wp80106"> </a></pre></div><a name="wp80107"> </a><h4 class="pHeading3">2. RosterEJB</h4><a name="wp80108"> </a><p class="pBody">The <code class="cCode">createPlayer</code> method of the <code class="cCode">RosterEJB</code> session bean creates a new instance of the <code class="cCode">PlayerEJB</code> entity bean. Because the access of <code class="cCode">PlayerEJB</code> is local, the <code class="cCode">create</code> method is defined in the local home interface, <code class="cCode">LocalPlayerHome</code>. The type of the <code class="cCode">playerHome</code> object is <code class="cCode">LocalPlayerHome</code>. Here is the source code for the <code class="cCode">createPlayer</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void createPlayer(PlayerDetails details) { try { LocalPlayer player = playerHome.create(details.getId(), details.getName(), details.getPosition(), details.getSalary());} catch (Exception ex) { throw new EJBException(ex.getMessage()); }}<a name="wp80109"> </a></pre></div><a name="wp80110"> </a><h4 class="pHeading3">3. PlayerEJB</h4><a name="wp80111"> </a><p class="pBody">The <code class="cCode">ejbCreate</code> method assigns the input arguments to the bean's persistent fields by calling the <code class="cCode">set</code> access methods. At the end of the transaction that contains the create call, the container saves the persistent fields in the database by issuing a SQL <code class="cCode">INSERT</code> statement. The code for the <code class="cCode">ejbCreate</code> method follows.</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="wp80113"> </a></pre></div><a name="wp80114"> </a><h3 class="pHeading2">Adding a Player to a Team</h3><a name="wp80115"> </a><h4 class="pHeading3">1. RosterClient</h4><a name="wp80116"> </a><p class="pBody">The <code class="cCode">RosterClient</code> calls the <code class="cCode">addPlayer</code> business method of the <code class="cCode">RosterEJB</code> session bean to add player P1 to team T1. The <code class="cCode">P1</code> and <code class="cCode">T1</code> parameters are the primary keys of the <code class="cCode">PlayerEJB</code> and <code class="cCode">TeamEJB</code> instances, respectively.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> myRoster.addPlayer("P1", "T1");<a name="wp80117"> </a></pre></div><a name="wp80118"> </a><h4 class="pHeading3">2. RosterEJB</h4><a name="wp80120"> </a><p class="pBody">The <code class="cCode">addPlayer</code> method performs two steps. First, it calls <code class="cCode">findByPrimaryKey</code> to locate the <code class="cCode">PlayerEJB</code> and <code class="cCode">TeamEJB</code> instances. Second, it invokes the <code class="cCode">addPlayer</code> business method of the <code class="cCode">TeamEJB</code> entity bean. Here is the source code for the <code class="cCode">addPlayer</code> method of the <code class="cCode">RosterEJB</code> session bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void addPlayer(String playerId, String teamId) { try { LocalTeam team = teamHome.findByPrimaryKey(teamId); LocalPlayer player = playerHome.findByPrimaryKey(playerId); team.addPlayer(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); }}<a name="wp80121"> </a></pre></div><a name="wp80122"> </a><h4 class="pHeading3">3. TeamEJB</h4><a name="wp80123"> </a><p class="pBody">The <code class="cCode">TeamEJB</code> entity bean has a relationship field named <code class="cCode">players</code>, a <code class="cCode">Collection</code> that represents the players that belong to the team. The access methods for the <code class="cCode">players</code> relationship field are as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public abstract Collection getPlayers();public abstract void setPlayers(Collection players);<a name="wp80125"> </a></pre></div><a name="wp80126"> </a><p class="pBody">The <code class="cCode">addPlayer</code> method of <code class="cCode">TeamEJB</code> invokes the <code class="cCode">getPlayers</code> access method to fetch the <code class="cCode">Collection</code> of related <code class="cCode">LocalPlayer</code> objects. Next, the <code class="cCode">addPlayer</code> method invokes the <code class="cCode">add</code> method of the <code class="cCode">Collection</code> interface. Here is the source code for the <code class="cCode">addPlayer</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void addPlayer(LocalPlayer player) { try { Collection players = getPlayers(); players.add(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); }}<a name="wp80128"> </a></pre></div><a name="wp80129"> </a><h3 class="pHeading2">Removing a Player</h3><a name="wp80130"> </a><h4 class="pHeading3">1. RosterClient</h4><a name="wp80131"> </a><p class="pBody">To remove player <code class="cCode">P4</code>, the client would invoke the <code class="cCode">removePlayer</code> method of the <code class="cCode">RosterEJB</code> session bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">myRoster.removePlayer("P4");<a name="wp80132"> </a></pre></div><a name="wp80133"> </a><h4 class="pHeading3">2. RosterEJB</h4><a name="wp80134"> </a><p class="pBody">The <code class="cCode">removePlayer</code> method locates the <code class="cCode">PlayerEJB</code> instance by calling <code class="cCode">findBy-PrimaryKey</code> and then invokes the <code class="cCode">remove</code> method on the instance. This invocation signals the container to delete the row in the database that corresponds to the <code class="cCode">PlayerEJB</code> instance. The container also removes the item for this instance from the <code class="cCode">players</code> relationship field in the <code class="cCode">TeamEJB</code> entity bean. By this removal, the container automatically updates the <code class="cCode">TeamEJB-PlayerEJB</code> relationship. Here is the <code class="cCode">removePlayer</code> method of the <code class="cCode">RosterEJB</code> session bean:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void removePlayer(String playerId) { try { LocalPlayer player = playerHome.findByPrimaryKey(playerId); player.remove(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -