📄 bmp2.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>The SavingsAccountEJB Example</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="BMP.html" /> <link rel="Next" href="BMP3.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="BMP.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="BMP3.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="wp79709"> </a><h2 class="pHeading1">The SavingsAccountEJB Example</h2><a name="wp79711"> </a><p class="pBody">The entity bean illustrated in this section represents a simple bank account. The state of <code class="cCode">SavingsAccountEJB</code> is stored in the <code class="cCode">savingsaccount</code> table of a relational database. The <code class="cCode">savingsaccount</code> table is created by the following SQL statement:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">CREATE TABLE savingsaccount (id VARCHAR(3) CONSTRAINT pk_savingsaccount PRIMARY KEY, firstname VARCHAR(24), lastname VARCHAR(24), balance NUMERIC(10,2));<a name="wp79714"> </a></pre></div><a name="wp79715"> </a><p class="pBody">The <code class="cCode">SavingsAccountEJB</code> example requires the following code:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79716"> </a><div class="pSmartList1"><li>Entity bean class (<code class="cCode">SavingsAccountBean</code>)</li></div><a name="wp79717"> </a><div class="pSmartList1"><li>Home interface (<code class="cCode">SavingsAccountHome</code>)</li></div><a name="wp79718"> </a><div class="pSmartList1"><li>Remote interface (<code class="cCode">SavingsAccount</code>)</li></div></ul></div><a name="wp79719"> </a><p class="pBody">This example also makes use of the following classes:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79721"> </a><div class="pSmartList1"><li>A utility class named <code class="cCode">InsufficientBalanceException</code></li></div><a name="wp79722"> </a><div class="pSmartList1"><li>A client class called <code class="cCode">SavingsAccountClient</code></li></div></ul></div><a name="wp79723"> </a><p class="pBody">The source code for this example is in this directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><INSTALL>/j2eetutorial14/ejb/savingsaccount/src/ <a name="wp80937"> </a></pre></div><a name="wp79725"> </a><h3 class="pHeading2">Entity Bean Class</h3><a name="wp79726"> </a><p class="pBody">The sample entity bean class is called <code class="cCode">SavingsAccountBean</code>. As you look through its code, note that it meets the requirements of any entity bean with bean-managed persistence. First of all, it implements the following:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79727"> </a><div class="pSmartList1"><li><code class="cCode">EntityBean</code> interface</li></div><a name="wp79728"> </a><div class="pSmartList1"><li>Zero or more <code class="cCode">ejbCreate</code> and <code class="cCode">ejbPostCreate</code> methods</li></div><a name="wp79729"> </a><div class="pSmartList1"><li>Finder methods</li></div><a name="wp79730"> </a><div class="pSmartList1"><li>Business methods</li></div><a name="wp79731"> </a><div class="pSmartList1"><li>Home methods</li></div></ul></div><a name="wp79732"> </a><p class="pBody">In addition, an entity bean class with bean-managed persistence has these requirements:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79733"> </a><div class="pSmartList1"><li>The class is defined as <code class="cCode">public</code>.</li></div><a name="wp79734"> </a><div class="pSmartList1"><li>The class cannot be defined as <code class="cCode">abstract</code> or <code class="cCode">final</code>.</li></div><a name="wp79735"> </a><div class="pSmartList1"><li>It contains an empty constructor.</li></div><a name="wp79736"> </a><div class="pSmartList1"><li>It does not implement the <code class="cCode">finalize</code> method.</li></div></ul></div><a name="wp79739"> </a><h4 class="pHeading3">The EntityBean Interface</h4><a name="wp79742"> </a><p class="pBody">The <code class="cCode"><a href="http://java.sun.com/j2ee/tutorial/api/javax/ejb/EntityBean.html" target="_blank">EntityBean</a></code> interface extends the <code class="cCode"><a href="http://java.sun.com/j2ee/tutorial/api/javax/ejb/EnterpriseBean.html" target="_blank">EnterpriseBean</a></code> interface, which extends the <code class="cCode">Serializable</code> interface. The <code class="cCode">EntityBean</code> interface declares a number of methods, such as <code class="cCode">ejbActivate</code> and <code class="cCode">ejbLoad</code>, which you must implement in your entity bean class. These methods are discussed in later sections.</p><a name="wp79743"> </a><h4 class="pHeading3">The ejbCreate Method</h4><a name="wp79746"> </a><p class="pBody">When the client invokes a <code class="cCode">create</code> method, the EJB container invokes the corresponding <code class="cCode">ejbCreate</code> method. Typically, an <code class="cCode">ejbCreate</code> method in an entity bean performs the following tasks:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79749"> </a><div class="pSmartList1"><li>Inserts the entity state into the database</li></div><a name="wp79750"> </a><div class="pSmartList1"><li>Initializes the instance variables</li></div><a name="wp79752"> </a><div class="pSmartList1"><li>Returns the primary key</li></div></ul></div><a name="wp79753"> </a><p class="pBody">The <code class="cCode">ejbCreate</code> method of <code class="cCode">SavingsAccountBean</code> inserts the entity state into the database by invoking the private <code class="cCode">insertRow</code> method, which issues the SQL <code class="cCode">INSERT </code>statement. Here is the source code for the <code class="cCode">ejbCreate</code> method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public String ejbCreate(String id, String firstName, String lastName, BigDecimal balance) throws CreateException { if (balance.signum() == -1) { throw new CreateException ("A negative initial balance is not allowed."); } try { insertRow(id, firstName, lastName, balance); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); } this.id = id; this.firstName = firstName; this.lastName = lastName; this.balance = balance; return id;}<a name="wp79756"> </a></pre></div><a name="wp79757"> </a><p class="pBody">Although the <code class="cCode">SavingsAccountBean</code> class has just one <code class="cCode">ejbCreate</code> method, an enterprise bean may contain multiple <code class="cCode">ejbCreate</code> methods. For an example, see the <code class="cCode">CartEJB.java</code> source code in this directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><INSTALL>/j2eetutorial14/examples/ejb/cart/src/<a name="wp80944"> </a></pre></div><a name="wp79758"> </a><p class="pBody">When writing an <code class="cCode">ejbCreate</code> method for an entity bean, be sure to follow these rules:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79759"> </a><div class="pSmartList1"><li>The access control modifier must be <code class="cCode">public</code>.</li></div><a name="wp79760"> </a><div class="pSmartList1"><li>The return type must be the primary key.</li></div><a name="wp79761"> </a><div class="pSmartList1"><li>The arguments must be legal types for the Java RMI API.</li></div><a name="wp79762"> </a><div class="pSmartList1"><li>The method modifier cannot be <code class="cCode">final</code> or <code class="cCode">static.</code></li></div></ul></div><a name="wp79764"> </a><p class="pBody">The <code class="cCode">throws</code> clause may include the <code class="cCode">javax.ejb.CreateException</code> and exceptions that are specific to your application. An <code class="cCode">ejbCreate</code> method usually throws a <code class="cCode">CreateException</code> if an input parameter is invalid. If an <code class="cCode">ejbCreate</code> method cannot create an entity because another entity with the same primary key already exists, it should throw a <code class="cCode">javax.ejb.DuplicateKeyException</code> (a subclass of <code class="cCode">CreateException</code>). If a client receives a <code class="cCode">CreateException</code> or a <code class="cCode">DuplicateKeyException</code>, it should assume that the entity was not created.</p><a name="wp79765"> </a><p class="pBody">The state of an entity bean may be directly inserted into the database by an application that is unknown to the J2EE server. For example, a SQL script might insert a row into the <code class="cCode">savingsaccount</code> table. Although the entity bean for this row was not created by an <code class="cCode">ejbCreate</code> method, the bean can be located by a client program.</p><a name="wp79767"> </a><h4 class="pHeading3">The ejbPostCreate Method</h4><a name="wp79769"> </a><p class="pBody">For each <code class="cCode">ejbCreate</code> method, you must write an <code class="cCode">ejbPostCreate</code> method in the entity bean class. The EJB container invokes <code class="cCode">ejbPostCreate</code> immediately after it calls <code class="cCode">ejbCreate</code>. Unlike the <code class="cCode">ejbCreate</code> method, the <code class="cCode">ejbPostCreate</code> method can invoke the <code class="cCode">getPrimaryKey</code> and <code class="cCode">getEJBObject</code> methods of the <code class="cCode">EntityContext</code> interface. For more information on the <code class="cCode">getEJBObject</code> method, see the section <a href="Session4.html#wp79850">Passing an Enterprise Bean's Object Reference</a>. Often, your <code class="cCode">ejbPostCreate</code> methods will be empty. </p><a name="wp79776"> </a><p class="pBody">The signature of an <code class="cCode">ejbPostCreate</code> method must meet the following requirements:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp79777"> </a><div class="pSmartList1"><li>The number and types of arguments must match a corresponding <code class="cCode">ejbCreate</code> method.</li></div><a name="wp79778"> </a><div class="pSmartList1"><li>The access control modifier must be <code class="cCode">public</code>.</li></div><a name="wp79779"> </a><div class="pSmartList1"><li>The method modifier cannot be <code class="cCode">final</code> or <code class="cCode">static</code>.</li></div><a name="wp79780"> </a><div class="pSmartList1"><li>The return type must be <code class="cCode">void</code>.</li></div></ul></div><a name="wp79781"> </a><p class="pBody">The <code class="cCode">throws</code> clause may include the <code class="cCode">javax.ejb.CreateException</code> and exceptions that are specific to your application.</p><a name="wp79782"> </a><h4 class="pHeading3">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -