📄 bmp4.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>Primary Keys 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="BMP3.html" /> <link rel="Next" href="BMP5.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="BMP3.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="BMP5.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="wp81606"> </a><h2 class="pHeading1">Primary Keys for Bean-Managed Persistence</h2><a name="wp80183"> </a><p class="pBody">You specify the primary key class in the entity bean's deployment descriptor. In most cases, your primary key class will be a <code class="cCode">String</code>, an <code class="cCode">Integer</code>, or some other class that belongs to the J2SE or J2EE standard libraries. For some entity beans, you will need to define your own primary key class. For example, if the bean has a composite primary key (that is, one composed of multiple fields), then you must create a primary key class.</p><a name="wp80185"> </a><h3 class="pHeading2">The Primary Key Class</h3><a name="wp80186"> </a><p class="pBody">The following primary key class is a composite key--the <code class="cCode">productId</code> and <code class="cCode">vendorId</code> fields together uniquely identify an entity bean.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class ItemKey implements java.io.Serializable { public String productId; public String vendorId; public ItemKey() { }; public ItemKey(String productId, String vendorId) { this.productId = productId; this.vendorId = vendorId; } public String getProductId() { return productId; } public String getVendorId() { return vendorId; } public boolean equals(Object other) { if (other instanceof ItemKey) { return (productId.equals(((ItemKey)other).productId) && vendorId.equals(((ItemKey)other).vendorId)); } return false; } public int hashCode() { return productId.concat(vendorId).hashCode(); }}<a name="wp80188"> </a></pre></div><a name="wp80189"> </a><p class="pBody">For bean-managed persistence, a primary key class must meet these requirements:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80190"> </a><div class="pSmartList1"><li>The access control modifier of the class is <code class="cCode">public</code>.</li></div><a name="wp80191"> </a><div class="pSmartList1"><li>All fields are declared as <code class="cCode">public</code>.</li></div><a name="wp80192"> </a><div class="pSmartList1"><li>The class has a public default constructor.</li></div><a name="wp80193"> </a><div class="pSmartList1"><li>The class implements the <code class="cCode">hashCode()</code> and <code class="cCode">equals(Object other)</code> methods.</li></div><a name="wp80194"> </a><div class="pSmartList1"><li>The class is serializable.</li></div></ul></div><a name="wp80195"> </a><h3 class="pHeading2">Primary Keys in the Entity Bean Class</h3><a name="wp80197"> </a><p class="pBody">With bean-managed persistence, the <code class="cCode">ejbCreate</code> method assigns the input parameters to instance variables and then returns the primary key class:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public ItemKey ejbCreate(String productId, String vendorId, String description) throws CreateException { if (productId == null || vendorId == null) { throw new CreateException( "The productId and vendorId are required."); } this.productId = productId; this.vendorId = vendorId; this.description = description; return new ItemKey(productId, vendorId);}<a name="wp80198"> </a></pre></div><a name="wp80200"> </a><p class="pBody">The <code class="cCode">ejbFindByPrimaryKey</code> verifies the existence of the database row for the given primary key:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public ItemKey ejbFindByPrimaryKey(ItemKey primaryKey) throws FinderException { try { if (selectByPrimaryKey(primaryKey)) return primaryKey; ...}private boolean selectByPrimaryKey(ItemKey primaryKey) throws SQLException { String selectStatement = "select productid " + "from item where productid = ? and vendorid = ?"; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, primaryKey.getProductId()); prepStmt.setString(2, primaryKey.getVendorId()); ResultSet rs = prepStmt.executeQuery(); boolean result = rs.next(); prepStmt.close(); return result;}<a name="wp80201"> </a></pre></div><a name="wp80202"> </a><h3 class="pHeading2">Getting the Primary Key</h3><a name="wp80204"> </a><p class="pBody">A client can fetch the primary key of an entity bean by invoking the <code class="cCode">getPrimaryKey</code> method of the <code class="cCode">EJBObject</code> class:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SavingsAccount account;...String id = (String)account.getPrimaryKey();<a name="wp80205"> </a></pre></div><a name="wp80206"> </a><p class="pBody">The entity bean retrieves its own primary key by calling the <code class="cCode">getPrimaryKey</code> method of the <code class="cCode">EntityContext</code> class:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">EntityContext context;...String id = (String) context.getPrimaryKey(); <a name="wp80207"> </a></pre></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="BMP3.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="BMP5.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 + -