📄 ejbconcepts4.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>What Is an Entity Bean?</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="EJBConcepts3.html" /> <link rel="Next" href="EJBConcepts5.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="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"> <blockquote><a name="wp79826"> </a><h2 class="pHeading1">What Is an Entity Bean?</h2><a name="wp79830"> </a><p class="pBody">An <span style="font-style: italic">entity bean</span> represents a business object in a persistent storage mechanism. Some examples of business objects are customers, orders, and products. In the J2EE Application Server, the persistent storage mechanism is a relational database. Typically, each entity bean has an underlying table in a relational database, and each instance of the bean corresponds to a row in that table. For code examples of entity beans, please refer to chapters <a href="BMP.html#wp80422"></a><a href="BMP.html#wp79663">21</a> and <a href="CMP.html#wp79663">22</a><a href="CMP.html#wp80501"></a>.</p><a name="wp79844"> </a><h3 class="pHeading2">What Makes Entity Beans Different from Session Beans?</h3><a name="wp79846"> </a><p class="pBody">Entity beans differ from session beans in several ways. Entity beans are persistent, allow shared access, have primary keys, and may participate in relationships with other entity beans.</p><a name="wp79847"> </a><h4 class="pHeading3">Persistence</h4><a name="wp79848"> </a><p class="pBody">Because the state of an entity bean is saved in a storage mechanism, it is persistent. <span style="font-style: italic">Persistence</span> means that the entity bean's state exists beyond the lifetime of the application or the J2EE server process. If you've worked with databases, you're familiar with persistent data. The data in a database is persistent because it still exists even after you shut down the database server or the applications it services.</p><a name="wp79850"> </a><p class="pBody">There are two types of persistence for entity beans: bean-managed and container-managed. With <span style="font-style: italic">bean-managed persistence</span>, the entity bean code that you write contains the calls that access the database. If your bean has container-managed persistence, the EJB container automatically generates the necessary database access calls. The code that you write for the entity bean does not include these calls. For additional information, see the section <a href="EJBConcepts4.html#wp79885">Container-Managed Persistence</a>.</p><a name="wp79857"> </a><h4 class="pHeading3">Shared Access</h4><a name="wp79858"> </a><p class="pBody">Entity beans may be shared by multiple clients. Because the clients might want to change the same data, it's important that entity beans work within transactions. Typically, the EJB container provides transaction management. In this case, you specify the transaction attributes in the bean's deployment descriptor. You do not have to code the transaction boundaries in the bean--the container marks the boundaries for you. See Chapter <a href="Transaction.html#wp79660">25</a><a href="Transaction.html#wp79663"></a> for more information.</p><a name="wp79867"> </a><h4 class="pHeading3">Primary Key</h4><a name="wp79869"> </a><p class="pBody">Each entity bean has a unique object identifier. A customer entity bean, for example, might be identified by a customer number. The unique identifier, or <span style="font-style: italic">primary key</span>, enables the client to locate a particular entity bean. For more information see the section <a href="BMP4.html#wp81606">Primary Keys for Bean-Managed Persistence</a>. </p><a name="wp79874"> </a><h4 class="pHeading3">Relationships</h4><a name="wp79875"> </a><p class="pBody">Like a table in a relational database, an entity bean may be related to other entity beans. For example, in a college enrollment application, <code class="cCode">StudentEJB</code> and <code class="cCode">CourseEJB</code> would be related because students enroll in classes.</p><a name="wp79877"> </a><p class="pBody">You implement relationships differently for entity beans with bean-managed persistence and those with container-managed persistence. With bean-managed persistence, the code that you write implements the relationships. But with container-managed persistence, the EJB container takes care of the relationships for you. For this reason, relationships in entity beans with container-managed persistence are often referred to as <span style="font-style: italic">container-managed relationships</span>.</p><a name="wp79885"> </a><h3 class="pHeading2">Container-Managed Persistence</h3><a name="wp79888"> </a><p class="pBody">The term <span style="font-style: italic">container-managed persistence</span> means that the EJB container handles all database access required by the entity bean. The bean's code contains no database access (SQL) calls. As a result, the bean's code is not tied to a specific persistent storage mechanism (database). Because of this flexibility, even if you redeploy the same entity bean on different J2EE servers that use different databases, you won't need to modify or recompile the bean's code. In short, your entity beans are more portable.</p><a name="wp79891"> </a><p class="pBody">In order to generate the data access calls, the container needs information that you provide in the entity bean's abstract schema.</p><a name="wp79892"> </a><h4 class="pHeading3">Abstract Schema</h4><a name="wp79895"> </a><p class="pBody">Part of an entity bean's deployment descriptor, the <span style="font-style: italic">abstract schema</span> defines the bean's persistent fields and relationships. The term <em class="cEmphasis">abstract</em> distinguishes this schema from the physical schema of the underlying data store. In a relational database, for example, the physical schema is made up of structures such as tables and columns.</p><a name="wp79899"> </a><p class="pBody">You specify the name of an abstract schema in the deployment descriptor. This name is referenced by queries written in the Enterprise JavaBeans Query Language ("EJB QL"). For an entity bean with container-managed persistence, you must define an EJB QL query for every finder method (except <code class="cCode">findByPrimaryKey</code>). The EJB QL query determines the query that is executed by the EJB container when the finder method is invoked. To learn more about EJB QL, see Chapter <a href="EJBQL.html#wp79663">24</a><a href="EJBQL.html#wp80587"></a>.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -