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

📄 lib0083.html

📁 j2ee架构师手册
💻 HTML
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Using Entity Beans</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0082.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0084.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch12"></a>
<div class="section">
<h2 class="first-section-title"><a name="383"></a><a name="ch12lev1sec2"></a>Using Entity Beans</h2><p class="first-para">For readers who use entity beans, this section illustrates how to incorporate them into a layered architecture. This is not intended as comprehensive entity bean coverage. For more in-depth information on using entity beans, see <a href="LiB0088.html#427" target="_parent" class="chapterjump">Alur et al. (2001)</a>.</p>
<p class="para">Using the layered architecture as discussed throughout this book, entity bean usage occurs in data access objects and makes native JDBC code within data access objects unnecessary. In other words, each method in a DAO uses entity beans to retrieve and store data, not native JDBC. For example, consider a method on <span class="fixed">PurchaseOrderDAO</span> called <span class="fixed">getPOsForCustomer</span> that will look up all orders for a given customer. <a class="internaljump" href="#ch12list02">Listing 12.2</a> illustrates how the method uses an entity bean to retrieve information for the purchase orders and place them in <span class="fixed">PurchaseOrderVO</span> objects so that they can be used throughout the application.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 12.2: </span>Using Entity Beans for Data Access Objects</span><a name="384"></a><a name="ch12list02"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
    1:  public PurchaseOrderVO[] getPOsForCustomer (
    2:        String customerId)
    3:  {
    4:    PurchaseOrderVO[] po = null;<a name="385"></a><a name="IDX-158"></a>
    5:    try
    6:    {
    7:      // Look up Entity Bean reference
    8:      Context ctx = new InitialContext();
    9:      PurchaseOrderHome poHome = (PurchaseOrderHome)
   10:      PortableRemoteObject.narrow(
   11:        ctx.lookup(
   12:            "java:comp/env/BookSamples/PurchaseOrder"),
   13:        PurchaseOrderHome.class);
   14:      // &#8212;&gt; End of Entity Bean Lookup
   15:
   16:      PurchaseOrder poEjb = null;
   17:      Iterator poIt = null;
   18:      Collection poC =
   19:        poHome.findByCustomerId(customerId);
   20:      if (poC.size() &gt; 0)
   21:      {
   22:        po = new PurchaseOrderVO[poC.size()];
   23:        int i = 0;
   24:        poIt = poC.iterator();
   25:        while (poIt.hasNext())
   26:        {
   27:            poEjb = (PurchaseOrder)poIt.next();
   28:            po[i] = new PurchaseOrderVO();
   29:            po[i].setCustomerId(poEjb.getCustomerId());
   30:            po[i].setOrderNbr(poEjb.getOrderNbr());
   31:            po[i].setOrderDate(poEjb.getDateCreated());
   32:            po[i].setShipDate(poEjb.getDateShipped());
   33:
   34:            i++;
   35:        }
   36:      }
   37:
   38:    }
   39:    catch (Throwable t)
   40:    {
   41:        throw new SampleException
   42:              ( "Error searching PO. cust=" +
   43:                customerId, t);
   44:    }
   45:
   46:    return po;
   47:  }
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">
<i class="emphasis">Source:</i> /src/book/sample/dao/db/PurchaseOrderDAO.java</p>
<p class="para">Note that this example uses container-managed persistence and local interfaces from the 2.0 EJB specification.</p>
<p class="para">The example in <a class="internaljump" href="#ch12list02">listing 12.2</a> illustrates that in a layered architecture, DAOs <a name="386"></a><a name="IDX-159"></a>can have the same role within the application no matter what technology the objects use to interact with a database. Data access objects can be converted from using native JDBC to using entity beans (and vice versa) without affecting the rest of the application.</p>
<p class="last-para">As a performance enhancement, you may want to consider placing the context and JNDI look-up (lines 7 through 14 of <a class="internaljump" href="#ch12list02">listing 12.2</a>) in the constructor so that you don't repeat these operations for each call. I don't make this a general recommendation because it can cause availability issues in clustered environments.</p>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0082.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0084.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -