⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmp3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
      while (i.hasNext()) {         Customer customer = (Customer)i.next();         String id = (String)customer.getPrimaryKey();         customerIds.add(id);      }   &nbsp;&nbsp;} catch (Exception ex) {       throw new EJBException(&quot;Exception in loadCustomerIds: &quot; +          ex.getMessage());&nbsp;&nbsp;}}<a name="wp80115"> </a></pre></div><a name="wp80116"> </a><p class="pBody">If a customer's sales representative changes, the client program updates the database by calling the <code class="cCode">setSalesRepId</code> method of the <code class="cCode">CustomerBean</code> class. The next time a business method of the <code class="cCode">SalesRepBean</code> class is called, the <code class="cCode">ejbLoad</code> method invokes <code class="cCode">loadCustomerIds</code>, which refreshes the <code class="cCode">customerIds</code> variable. (To ensure that <code class="cCode">ejbLoad</code> is invoked before each business method, set the transaction attributes of the business methods to Required.) For example, the <code class="cCode">SalesRepClient</code> program changes the <code class="cCode">salesRepId</code> for a customer named Mary Jackson as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Customer mary = customerHome.findByPrimaryKey(&quot;987&quot;);mary.setSalesRepId(&quot;543&quot;);<a name="wp80117"> </a></pre></div><a name="wp80118"> </a><p class="pBody">The <code class="cCode">salesRepId</code> value 543 identifies a sales representative named Janice Martin. To list all of Janice's customers, the <code class="cCode">SalesRepClient</code> program invokes the <code class="cCode">getCustomerIds</code> method, iterates through the <code class="cCode">ArrayList</code> of identifiers, and locates each <code class="cCode">CustomerEJB</code> entity bean by calling its <code class="cCode">findByPrimaryKey</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SalesRep janice = salesHome.findByPrimaryKey(&quot;543&quot;);ArrayList a = janice.getCustomerIds();i = a.iterator();while (i.hasNext()) {   String customerId = (String)i.next();   Customer customer = customerHome.findByPrimaryKey(customerId);   String name = customer.getName();   System.out.println(customerId + &quot;: &quot; + name);}<a name="wp80120"> </a></pre></div><a name="wp80121"> </a><h4 class="pHeading3">Running the SalesRepEJB Example</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81244"> </a><div class="pSmartList1"><li>Create the <code class="cCode">salesrep</code> database table.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81245"> </a><div class="pSmartList2"><li>In a terminal window, go to this directory:</li></div><a name="wp81246"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/salesrep/ </code></p><a name="wp81247"> </a><div class="pSmartList2"><li>Type this command:</li></div><a name="wp81248"> </a><p class="pBodyRelative"><code class="cCode">asant create-db_common</code></p></ol></div><a name="wp81249"> </a><div class="pSmartList1"><li>In <code class="cCode">deploytool</code>, deploy the <code class="cCode">SalesRepApp.ear</code> file, which is in this directory:</li></div><a name="wp81250"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/provided-ears/</code></p><a name="wp81251"> </a><div class="pSmartList1"><li>Run the client.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81252"> </a><div class="pSmartList2"><li>In a terminal window, go to this directory:</li></div><a name="wp81253"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/salesrep/ </code></p><a name="wp81254"> </a><div class="pSmartList2"><li>Type the following command on a single line:</li></div><a name="wp81255"> </a><p class="pBodyRelative"><code class="cCode">appclient -client SalesRepAppClient.jar </code></p><a name="wp81256"> </a><div class="pSmartList2"><li>The client should display the following lines:</li></div><a name="wp81257"> </a><p class="pBodyRelative"><code class="cCode">. . .<br />customerId = 221<br />customerId = 388<br />customerId = 456<br />customerId = 844<br /><br />987: Mary Jackson<br />221: Alice Smith<br />388: Bill Williamson<br />456: Joe Smith<br />844: Buzz Murphy<br />. . .</code></p></ol></div></ol></div><a name="wp80134"> </a><h3 class="pHeading2">Many-to-Many Relationships</h3><a name="wp80135"> </a><p class="pBody">In a many-to-many relationship, each entity may be related to multiple occurrences of the other entity. For example, a college course has many students and each student may take several courses. In a database, this relationship is represented by a cross reference table containing the foreign keys. In <a  href="BMP3.html#wp80144">Figure 21-4</a>, the cross reference table is the <code class="cCode">enrollment</code> table. These tables are accessed by the <code class="cCode">StudentBean</code>, <code class="cCode">CourseBean</code>, and <code class="cCode">EnrollerBean</code> classes. </p><a name="wp80142"> </a><p class="pBody"></p><div align="left"><img src="images/Fig174.gif" height="281" width="319" alt="Many-to-Many Relationship: Students and Courses" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="80144"> </a><strong><font >Figure 21-4    Many-to-Many Relationship: Students and Courses</font></strong></p><a name="wp80145"> </a><p class="pBody">The source code for this example is in this directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;INSTALL&gt;/j2eetutorial14/examples/ejb/enroller/src/<a name="wp81025"> </a></pre></div><a name="wp80146"> </a><p class="pBody">The <code class="cCode">StudentBean</code> and <code class="cCode">CourseBean</code> classes are complementary. Each class contains an <code class="cCode">ArrayList</code> of foreign keys. The <code class="cCode">StudentBean</code> class contains an <code class="cCode">ArrayList</code> named <code class="cCode">courseIds</code>, which identifies the courses the student is enrolled in. Likewise, the <code class="cCode">CourseBean</code> class contains an <code class="cCode">ArrayList</code> named <code class="cCode">studentIds</code>.</p><a name="wp80148"> </a><p class="pBody">The <code class="cCode">ejbLoad</code> method of the <code class="cCode">StudentBean</code> class adds elements to the <code class="cCode">courseIds</code> <code class="cCode">ArrayList</code> by calling <code class="cCode">loadCourseIds</code>, a private method. The <code class="cCode">loadCourseIds</code> method gets the course identifiers from the <code class="cCode">EnrollerEJB</code> session bean. The source code for the <code class="cCode">loadCourseIds</code> method follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">private void loadCourseIds() {   courseIds.clear();   try {      Enroller enroller = enrollerHome.create();      ArrayList a = enroller.getCourseIds(studentId);      courseIds.addAll(a);&nbsp;&nbsp;} catch (Exception ex) {       throw new EJBException(&quot;Exception in loadCourseIds: &quot; +          ex.getMessage());&nbsp;&nbsp;}}<a name="wp80149"> </a></pre></div><a name="wp80150"> </a><p class="pBody">Invoked by the <code class="cCode">loadCourseIds</code> method, the <code class="cCode">getCourseIds</code> method of the <code class="cCode">EnrollerBean</code> class queries the <code class="cCode">enrollment</code> table:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">select courseid from enrollmentwhere studentid = ? <a name="wp80151"> </a></pre></div><a name="wp80153"> </a><p class="pBody">Only the <code class="cCode">EnrollerBean</code> class accesses the <code class="cCode">enrollment</code> table. Therefore, the <code class="cCode">EnrollerBean</code> class manages the student-course relationship represented in the <code class="cCode">enrollment</code> table. If a student enrolls in a course, for example, the client calls the <code class="cCode">enroll</code> business method, which inserts a row:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">insert into enrollmentvalues (studentid, courseid)<a name="wp80154"> </a></pre></div><a name="wp80155"> </a><p class="pBody">If a student drops a course, the <code class="cCode">unEnroll</code> method deletes a row:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">delete from enrollmentwhere studentid = ? and courseid = ?<a name="wp80156"> </a></pre></div><a name="wp80157"> </a><p class="pBody">And if a student leaves the school, the <code class="cCode">deleteStudent</code> method deletes all rows in the table for that student:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">delete from enrollmentwhere student = ?<a name="wp80158"> </a></pre></div><a name="wp80159"> </a><p class="pBody">The <code class="cCode">EnrollerBean</code> class does not delete the matching row from the <code class="cCode">student</code> table. That action is performed by the <code class="cCode">ejbRemove</code> method of the <code class="cCode">StudentBean</code> class. To ensure that both deletes are executed as a single operation, they should belong to the same transaction. See Chapter&nbsp;<a  href="Transaction.html#wp79660">25</a> <a  href="Transaction.html#wp79663"></a> for more information.</p><a name="wp80167"> </a><h4 class="pHeading3">Running the EnrollerEJB Example</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp81275"> </a><div class="pSmartList1"><li>Create the <code class="cCode">enroller</code> database table.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81276"> </a><div class="pSmartList2"><li>In a terminal window, go to this directory:</li></div><a name="wp81277"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/enroller/</code> </p><a name="wp81278"> </a><div class="pSmartList2"><li>Type this command:</li></div><a name="wp81279"> </a><p class="pBodyRelative"><code class="cCode">asant create-db_common</code> </p></ol></div><a name="wp81280"> </a><div class="pSmartList1"><li>In <code class="cCode">deploytool</code>, deploy the <code class="cCode">EnrollerApp.ear</code> file, which is in this directory:</li></div><a name="wp81281"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/provided-ears/</code> </p><a name="wp81282"> </a><div class="pSmartList1"><li>Run the client.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81283"> </a><div class="pSmartList2"><li>In a terminal window, go to this directory:</li></div><a name="wp81284"> </a><p class="pBodyRelative"><code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/ejb/enroller/</code> </p><a name="wp81285"> </a><div class="pSmartList2"><li>Type the following command on a single line:</li></div><a name="wp81286"> </a><p class="pBodyRelative"><code class="cCode">appclient -client EnrollerAppClient.jar</code> </p><a name="wp81287"> </a><div class="pSmartList2"><li>The client should display the following lines:</li></div><a name="wp81602"> </a><p class="pBodyRelative"><code class="cCode">. . .<br />Denise Smith:<br />220 Power J2EE Programming<br />333 XML Made Easy<br />777 An Introduction to Java Programming<br /><br />An Introduction to Java Programming:<br />823 Denise Smith<br />456 Joe Smith<br />388 Elizabeth Willis<br />. . .</code> </p></ol></div></ol></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="BMP2.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="BMP4.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 + -