📄 cmp7.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 Container-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="CMP6.html" /> <link rel="Next" href="CMP8.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="CMP6.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="CMP8.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="wp82626"> </a><h2 class="pHeading1">Primary Keys for Container-Managed Persistence</h2><a name="wp80312"> </a><p class="pBody">Sometimes you must implement the class and package it along with the entity bean. For example, if your entity bean requires a composite primary key (which is made up of multiple fields) or a primary key field is a Java programming language primitive type, then you need to provide a customized primary key class.</p><a name="wp80314"> </a><h3 class="pHeading2">The Primary Key Class</h3><a name="wp80315"> </a><p class="pBody">In the following example, the <code class="cCode">PurchaseOrderKey</code> class implements a composite key for the <code class="cCode">PurchaseOrderEJB</code> entity bean. The key is composed of two fields, <code class="cCode">productModel</code> and <code class="cCode">vendorId</code>, whose names must match two of the persistent fields in the entity bean class.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class PurchaseOrderKey implements java.io.Serializable { public String productModel; public String vendorId; public PurchaseOrderKey() { }; public boolean equals(Object other) { if (other instanceof PurchaseOrderKey) { return (productModel.equals( ((PurchaseOrderKey)other).productModel) && vendorId.equals( ((PurchaseOrderKey)other).vendorId)); } return false; } public int hashCode() { return productModel.concat(vendorId).hashCode(); }}<a name="wp83278"> </a></pre></div><a name="wp80319"> </a><p class="pBody">For container-managed persistence, a primary key class must meet the following requirements:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80320"> </a><div class="pSmartList1"><li>The access control modifier of the class is <code class="cCode">public</code>.</li></div><a name="wp80321"> </a><div class="pSmartList1"><li>All fields are declared as <code class="cCode">public</code>.</li></div><a name="wp80322"> </a><div class="pSmartList1"><li>The fields are a subset of the bean's persistent fields.</li></div><a name="wp80323"> </a><div class="pSmartList1"><li>The class has a public default constructor.</li></div><a name="wp80324"> </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="wp80325"> </a><div class="pSmartList1"><li>The class is serializable.</li></div></ul></div><a name="wp80326"> </a><h4 class="pHeading3">Primary Keys in the Entity Bean Class</h4><a name="wp80328"> </a><p class="pBody">In the <code class="cCode">PurchaseOrderBean</code> class, the following access methods define the persistent fields (<code class="cCode">vendorId</code> and <code class="cCode">productModel</code>) that make up the primary key:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public abstract String getVendorId();public abstract void setVendorId(String id); public abstract String getProductModel();public abstract void setProductModel(String name);<a name="wp80329"> </a></pre></div><a name="wp80331"> </a><p class="pBody">The next code sample shows the <code class="cCode">ejbCreate</code> method of the <code class="cCode">PurchaseOrderBean</code> class. The return type of the <code class="cCode">ejbCreate</code> method is the primary key, but the return value is <code class="cCode">null</code>. Although not required, the <code class="cCode">null</code> return value is recommended for container-managed persistence. This approach saves overhead because the bean does not have to instantiate the primary key class for the return value.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public PurchaseOrderKey ejbCreate (String vendorId, String productModel, String productName) throws CreateException {setVendorId(vendorId); setProductModel(productModel); setProductName(productName); return null;}<a name="wp80332"> </a></pre></div><a name="wp80334"> </a><h4 class="pHeading3">Generating Primary Key Values</h4><a name="wp80335"> </a><p class="pBody">For some entity beans, the value of a primary key has a meaning for the business entity. For example, in an entity bean that represents a player on a sports team, the primary key might be the player's driver's license number. But for other beans, the key's value is arbitrary--provided that it's unique. With container-managed persistence, these key values can be generated automatically by the EJB container. To take advantage of this feature, an entity bean must meet these requirements:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80337"> </a><div class="pSmartList1"><li>In the deployment descriptor, the primary key class is defined as a <code class="cCode">java.lang.Object</code>. The primary key field is not specified.</li></div><a name="wp80338"> </a><div class="pSmartList1"><li>In the home interface, the argument of the <code class="cCode">findByPrimaryKey</code> method must be a <code class="cCode">java.lang.Object</code>.</li></div><a name="wp80339"> </a><div class="pSmartList1"><li>In the entity bean class, the return type of the <code class="cCode">ejbCreate</code> method must be a <code class="cCode">java.lang.Object</code>.</li></div></ul></div><a name="wp80340"> </a><p class="pBody">In these entity beans, the primary key values are in an internal field that only the EJB container can access. You cannot associate the primary key with a persistent field or any other instance variable. However, you can fetch the bean's primary key by invoking the <code class="cCode">getPrimaryKey</code> method on the bean reference, and you can locate the bean by invoking its <code class="cCode">findByPrimaryKey</code> method.</p> </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="CMP6.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="CMP8.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 + -