📄 bmp3.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>Mapping Table Relationships for Bean-Managed Persistence</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="BMP2.html" /> <link rel="Next" href="BMP4.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="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"> <blockquote><a name="wp80001"> </a><h2 class="pHeading1">Mapping Table Relationships for Bean-Managed Persistence</h2><a name="wp80002"> </a><p class="pBody">In a relational database, tables can be related by common columns. The relationships between the tables affect the design of their corresponding entity beans. The entity beans discussed in this section are backed up by tables with the following types of relationships:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80003"> </a><div class="pSmartList1"><li>One-to-one </li></div><a name="wp80004"> </a><div class="pSmartList1"><li>One-to-many </li></div><a name="wp80005"> </a><div class="pSmartList1"><li>Many-to-many </li></div></ul></div><a name="wp80008"> </a><h3 class="pHeading2">One-to-One Relationships</h3><a name="wp80009"> </a><p class="pBody">In a one-to-one relationship, each row in a table is related to a single row in another table. For example, in a warehouse application, a <code class="cCode">storagebin</code> table might have a one-to-one relationship with a <code class="cCode">widget</code> table. This application would model a physical warehouse in which each storage bin contains one type of widget and each widget resides in one storage bin.</p><a name="wp80013"> </a><p class="pBody"><a href="BMP3.html#wp80021">Figure 21-1</a> illustrates the <code class="cCode">storagebin</code> and <code class="cCode">widget</code> tables. Because the <code class="cCode">storagebinid</code> uniquely identifies a row in the <code class="cCode">storagebin</code> table, it is that table's primary key. The <code class="cCode">widgetid</code> is the primary key of the <code class="cCode">widget</code> table. The two tables are related because the <code class="cCode">widgetid</code> is also a column in the <code class="cCode">storagebin</code> table. By referring to the primary key of the <code class="cCode">widget</code> table, the <code class="cCode">widgetid</code> in the <code class="cCode">storagebin</code> table identifies which widget resides in a particular storage bin in the warehouse. Because the <code class="cCode">widgetid</code> of the <code class="cCode">storagebin</code> table refers to the primary key of another table, it is called a <span style="font-style: italic">foreign key</span>. (The figures in this chapter denote a primary key with PK and a foreign key with FK.)</p><a name="wp80019"> </a><p class="pBody"></p><div align="left"><img src="images/Fig14.gif" height="107" width="378" alt="One-to-One Table Relationship" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p> <a name="80021"> </a><strong><font >Figure 21-1 One-to-One Table Relationship</font></strong></p><a name="wp80022"> </a><p class="pBody">A dependent (child) table includes a foreign key that matches the primary key of the referenced (parent) table. The values of the foreign keys in the <code class="cCode">storagebin</code> (child) table depend on the primary keys in the <code class="cCode">widget</code> (parent) table. For example, if the <code class="cCode">storagebin</code> table has a row with a <code class="cCode">widgetid</code> of 344, then the widget table should also have a row whose <code class="cCode">widgetid</code> is 344.</p><a name="wp80023"> </a><p class="pBody">When designing a database application, you may choose to enforce the dependency between the parent and child tables. There are two ways to enforce such a dependency: by defining a referential constraint in the database or by performing checks in the application code. The <code class="cCode">storagebin</code> table has a referential constraint named <code class="cCode">fk_widgetid</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">CREATE TABLE storagebin (storagebinid VARCHAR(3) CONSTRAINT pk_storagebin PRIMARY KEY, widgetid VARCHAR(3), quantity INTEGER, CONSTRAINT fk_widgetid FOREIGN KEY (widgetid) REFERENCES widget(widgetid));<a name="wp80025"> </a></pre></div><a name="wp80026"> </a><p class="pBody">The source code for the following example is in this directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><INSTALL>/j2eetutorial14/examples/ejb/storagebin/src/ <a name="wp80993"> </a></pre></div><a name="wp80027"> </a><p class="pBody">The <code class="cCode">StorageBinBean</code> and <code class="cCode">WidgetBean</code> classes illustrate the one-to-one relationship of the <code class="cCode">storagebin</code> and <code class="cCode">widget</code> tables. The <code class="cCode">StorageBinBean</code> class contains variables for each column in the <code class="cCode">storagebin</code> table, including the foreign key, <code class="cCode">widgetId</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">private String storageBinId;private String widgetId;private int quantity;<a name="wp80028"> </a></pre></div><a name="wp80030"> </a><p class="pBody">The <code class="cCode">ejbFindByWidgetId</code> method of the <code class="cCode">StorageBinBean</code> class returns the <code class="cCode">storageBinId</code> that matches a given <code class="cCode">widgetId</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public String ejbFindByWidgetId(String widgetId) throws FinderException { String storageBinId; try { storageBinId = selectByWidgetId(widgetId); } catch (Exception ex) { throw new EJBException("ejbFindByWidgetId: " + ex.getMessage()); } if (storageBinId == null) { throw new ObjectNotFoundException ("Row for widgetId " + widgetId + " not found."); } else { return storageBinId; }}<a name="wp80031"> </a></pre></div><a name="wp80032"> </a><p class="pBody">The <code class="cCode">ejbFindByWidgetId</code> method locates the <code class="cCode">widgetId</code> by querying the database in the <code class="cCode">selectByWidgetId</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">private String selectByWidgetId(String widgetId) throws SQLException { String storageBinId; makeConnection(); String selectStatement = "select storagebinid " + "from storagebin where widgetid = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, widgetId); ResultSet rs = prepStmt.executeQuery(); if (rs.next()) { storageBinId = rs.getString(1); } else { storageBinId = null; } prepStmt.close(); releaseConnection(); return storageBinId;}<a name="wp80033"> </a></pre></div><a name="wp80034"> </a><p class="pBody">To find out in which storage bin a widget resides, the <code class="cCode">StorageBinClient</code> program calls the <code class="cCode">findByWidgetId</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">String widgetId = "777";StorageBin storageBin = storageBinHome.findByWidgetId(widgetId);String storageBinId = (String)storageBin.getPrimaryKey();int quantity = storageBin.getQuantity();<a name="wp80035"> </a></pre></div><a name="wp80036"> </a><h4 class="pHeading3">Running the StorageBinEJB Example</h4><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp80037"> </a><div class="pSmartList1"><li>Create the <code class="cCode">storagebin</code> database table.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81114"> </a><div class="pSmartList2"><li>In a terminal window, go to this directory:</li></div><a name="wp81101"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/storagebin/</code></p><a name="wp80039"> </a><div class="pSmartList2"><li>Type this command:</li></div><a name="wp81217"> </a><p class="pBodyRelative"><code class="cCode">asant create-db_common</code></p></ol></div><a name="wp81210"> </a><div class="pSmartList1"><li>In <code class="cCode">deploytool</code>, deploy the <code class="cCode">StorageBinApp.ear</code> file, which is in this directory:</li></div><a name="wp81121"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/provided-ears/</code></p><a name="wp80041"> </a><div class="pSmartList1"><li>Run the client.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp81131"> </a><div class="pSmartList2"><li>In a terminal window, go to this directory:</li></div><a name="wp81132"> </a><p class="pBodyRelative"><code class="cCode"><</code><code class="cVariable">INSTALL</code><code class="cCode">>/j2eetutorial14/examples/ejb/storagebin/</code></p><a name="wp80044"> </a><div class="pSmartList2"><li>Type the following command on a single line:</li></div><a name="wp81142"> </a><p class="pBodyRelative"><code class="cCode">appclient -client StorageBinAppClient.jar </code></p><a name="wp81143"> </a><div class="pSmartList2"><li>The client should display the following:</li></div><a name="wp81140"> </a><p class="pBodyRelative"><code class="cCode">. . .<br />777 388 500 1.0 Duct Tape<br />. . .</code></p></ol></div></ol></div><a name="wp80048"> </a><h3 class="pHeading2">One-to-Many Relationships</h3><a name="wp80049"> </a><p class="pBody">If the primary key in a parent table matches multiple foreign keys in a child table, then the relationship is one-to-many. This relationship is common in database applications. For example, an application for a sports league might access a <code class="cCode">team</code> table and a <code class="cCode">player</code> table. Each team has multiple players, and each player belongs to a single team. Every row in the child table (<code class="cCode">player</code>) has a foreign key identifying the player's team. This foreign key matches the <code class="cCode">team</code> table's primary key.</p><a name="wp80050"> </a><p class="pBody">The sections that follow describe how you might implement one-to-many relationships in entity beans. When designing such entity beans, you must decide whether both tables are represented by entity beans, or just one.</p><a name="wp80053"> </a><h4 class="pHeading3">A Helper Class for the Child Table</h4><a name="wp80054"> </a><p class="pBody">Not every database table needs to be mapped to an entity bean. If a database table doesn't represent a business entity, or if it stores information that is contained in another entity, then the table should be represented with a helper class. In an online shopping application, for example, each order submitted by a customer can have multiple line items. The application stores the information in the database tables shown by <a href="BMP3.html#wp80063">Figure 21-2</a>.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -