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

📄 ejbconcepts4.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<a name="wp79910"> </a><p class="pBody">You'll probably find it helpful to sketch the abstract schema before writing any code. <a  href="EJBConcepts4.html#wp79919">Figure 18-1</a> represents a simple abstract schema that describes the relationships between three entity beans. These relationships are discussed further in the sections that follow.</p><a name="wp79917"> </a><p class="pBody"></p><div align="left"><img src="images/Fig8.gif" height="286" width="387" alt="High-Level View of an Abstract Schema" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="79919"> </a><strong><font >Figure 18-1    A High-Level View of an Abstract Schema</font></strong></p><a name="wp79921"> </a><h5 class="pHeading4">Persistent Fields</h5><a name="wp79922"> </a><p class="pBody">The persistent fields of an entity bean are stored in the underlying data store. Collectively, these fields constitute the state of the bean. At runtime, the EJB container automatically synchronizes this state with the database. During deployment, the container typically maps the entity bean to a database table and maps the persistent fields to the table's columns.</p><a name="wp79924"> </a><p class="pBody">A <code class="cCode">CustomerEJB</code> entity bean, for example, might have persistent fields such as <code class="cCode">firstName</code>, <code class="cCode">lastName</code>, <code class="cCode">phone</code>, and <code class="cCode">emailAddress</code>. In container-managed persistence, these fields are virtual. You declare them in the abstract schema, but you do not code them as instance variables in the entity bean class. Instead, the persistent fields are identified in the code by access methods (getters and setters). </p><a name="wp79928"> </a><h5 class="pHeading4">Relationship Fields</h5><a name="wp79931"> </a><p class="pBody">A <span style="font-style: italic">relationship field</span> is like a foreign key in a database table--it identifies a related bean. Like a persistent field, a relationship field is virtual and is defined in the enterprise bean class with access methods. But unlike a persistent field, a relationship field does not represent the bean's state. Relationship fields are discussed further in <a  href="EJBConcepts4.html#wp79950">Direction in Container-Managed Relationships</a>.</p><a name="wp79937"> </a><h4 class="pHeading3">Multiplicity in Container-Managed Relationships</h4><a name="wp79940"> </a><p class="pBody">There are four types of multiplicities:</p><a name="wp79942"> </a><p class="pBody"><b class="cBold">One-to-one</b>: Each entity bean instance is related to a single instance of another entity bean. For example, to model a physical warehouse in which each storage bin contains a single widget, <code class="cCode">StorageBinEJB</code> and <code class="cCode">WidgetEJB</code> would have a one-to-one relationship.</p><a name="wp79944"> </a><p class="pBody"><b class="cBold">One-to-many</b>: An entity bean instance may be related to multiple instances of the other entity bean. A sales order, for example, can have multiple line items. In the order application, <code class="cCode">OrderEJB</code> would have a one-to-many relationship with <code class="cCode">LineItemEJB</code>.</p><a name="wp79946"> </a><p class="pBody"><b class="cBold">Many-to-one</b>: Multiple instances of an entity bean may be related to a single instance of the other entity bean. This multiplicity is the opposite of a one-to-many relationship. In the example mentioned in the previous item, from the perspective of <code class="cCode">LineItemEJB</code> the relationship to <code class="cCode">OrderEJB</code> is many-to-one. </p><a name="wp79948"> </a><p class="pBody"><b class="cBold">Many-to-many</b>: The entity bean instances may be related to multiple instances of each other. For example, in college each course has many students, and every student may take several courses. Therefore, in an enrollment application, <code class="cCode">CourseEJB</code> and <code class="cCode">StudentEJB</code> would have a many-to-many relationship.</p><a name="wp79950"> </a><h4 class="pHeading3">Direction in Container-Managed Relationships</h4><a name="wp79951"> </a><p class="pBody">The direction of a relationship may be either bidirectional or unidirectional. In a <span style="font-style: italic">bidirectional</span> relationship, each entity bean has a relationship field that refers to the other bean. Through the relationship field, an entity bean's code can access its related object. If an entity bean has a relative field, then we often say that it &quot;knows&quot; about its related object. For example, if <code class="cCode">OrderEJB</code> knows what <code class="cCode">LineItemEJB</code> instances it has and if <code class="cCode">LineItemEJB</code> knows what <code class="cCode">OrderEJB</code> it belongs to, then they have a bidirectional relationship.</p><a name="wp79955"> </a><p class="pBody">In a <span style="font-style: italic">unidirectional</span> relationship, only one entity bean has a relationship field that refers to the other. For example, <code class="cCode">LineItemEJB</code> would have a relationship field that identifies <code class="cCode">ProductEJB</code>, but <code class="cCode">ProductEJB</code> would not have a relationship field for <code class="cCode">LineItemEJB</code>. In other words, <code class="cCode">LineItemEJB</code> knows about <code class="cCode">ProductEJB</code>, but <code class="cCode">ProductEJB</code> doesn't know which <code class="cCode">LineItemEJB</code> instances refer to it.</p><a name="wp79958"> </a><p class="pBody">EJB QL queries often navigate across relationships. The direction of a relationship determines whether a query can navigate from one bean to another. For example, a query can navigate from <code class="cCode">LineItemEJB</code> to <code class="cCode">ProductEJB</code>, but cannot navigate in the opposite direction. For <code class="cCode">OrderEJB</code> and <code class="cCode">LineItemEJB</code>, a query could navigate in both directions, since these two beans have a bidirectional relationship.</p><a name="wp79959"> </a><h3 class="pHeading2">When to Use Entity Beans</h3><a name="wp79960"> </a><p class="pBody">You should probably use an entity bean under the following conditions:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79961"> </a><div class="pSmartList1"><li>The bean represents a business entity, not a procedure. For example, <code class="cCode">CreditCardEJB</code> would be an entity bean, but <code class="cCode">CreditCardVerifierEJB</code> would be a session bean.</li></div><a name="wp79963"> </a><div class="pSmartList1"><li>The bean's state must be persistent. If the bean instance terminates or if the J2EE server is shut down, the bean's state still exists in persistent storage (a database).</li></div></ul></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="EJBConcepts3.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="EJBConcepts5.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 + -