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

📄 chapter6.htm

📁 使用eclipse 开发 j2ee应用的教程
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0047)http://www.tusc.com.au/tutorial/html/chap6.html -->
<HTML><HEAD><TITLE>Chapter6 - Creating a CMP Entity Bean</TITLE>
<META http-equiv=CONTENT-TYPE content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1400" name=GENERATOR>
<META content="Glen McCallum and Vishal Sharma" name=AUTHOR>
<META content=20030729;9513900 name=CREATED>
<META content=Vishal name=CHANGEDBY>
<META content=20030911;15553500 name=CHANGED>
<META 
content="Tutorial for building J2EE Applications using Eclipse, Lomboz, Jboss and Xdoclet." 
name=DESCRIPTION>
<META 
content="Tutorial, J2EE, Lomboz, Eclipse, Jboss, Xdoclet, J2EE Patterns, EJB, Servlets, JSP, BMP, CMP, Session, Configuration, DAO" 
name=KEYWORDS>
<META content="14 days" name=revisit-after><!-- Meta tags --><!-- Copyright 漏 2003 TUSC. All rights reserved.--><!--- Header --></HEAD>
<BODY lang=en-US dir=ltr>
<FORM name=searchForm action=../../../../search/index.php method=post>
<TABLE style="PAGE-BREAK-BEFORE: always" cellSpacing=0 cellPadding=4 
width="100%" bgColor=#0a6a91 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width=154>
      <P><A 
      href="http://www.tusc.com.au/news_events/event_details.php?id=44"><IMG 
      alt=TUSC src="chapter6.files/tuscJ2eeLogo.gif" align=bottom border=0 
      name=Graphic1></A></P></TD>
    <TD vAlign=top width=276>
      <P><IMG height=73 alt="Reliable, On-Time Delivery." 
      src="chapter6.files/reliable.gif" width=268 align=bottom border=0 
      name=Graphic2><!--search table --></P></TD>
    <TD width=21></TD>
    <TD width=43>
      <P><IMG height=9 alt=Search src="chapter6.files/search.gif" width=41 
      align=bottom border=0 name=Graphic3></P></TD>
    <TD width=119>
      <P><INPUT maxLength=50 size=12 name=KEYWORD></P></TD>
    <TD width=31>
      <P><INPUT type=image height=18 width=29 src="chapter6.files/go.gif" 
      align=bottom name=go><INPUT type=hidden value=Y name=SECTION_ALL></P></TD>
    <TD vAlign=top width=50>
      <P><IMG height=73 alt=Spacer src="chapter6.files/invis.gif" width=50 
      align=bottom border=0 name=Graphic5></P></TD>
    <TD vAlign=top width=178>
      <P><A href="http://www.logec.com/"><IMG alt="LOGEC's Event Correlation" 
      src="chapter6.files/logec.gif" align=bottom border=0 name=Graphic57></A> 
      </P></TD>
    <TD width=14>
      <P><IMG height=10 alt=spacer src="chapter6.files/invis.gif" width=14 
      align=bottom border=0 name=Graphic58> </P></TD></TR></TBODY></TABLE></FORM>
<H2 align=center><!--- Header finished -->Tutorial for building J2EE 
Applications using JBOSS and ECLIPSE <IMG height=2 
src="chapter6.files/redrulr5.gif" width=775 align=middle border=0 
name=Graphic1></H2>
<P><BR><BR></P>
<H1>Chapter 6. </H1>
<P><IMG height=2 src="chapter6.files/blurulr6.gif" width=785 align=middle 
border=0 name=Graphic2></P>
<H2>Creating a CMP Entity Bean</H2>
<P>This chapter covers how to create a Container Managed Persistence (CMP) EJB 
component. We will create two CMP beans, Item and Supplier as shown below. The 
Item bean will be responsible for storing the details of items, such as their 
availabability and their prices, for MyStore. The Supplier Bean stores details 
of Suppliers to MyStore. Both beans interact with corresponding tables in the 
database. In CMP this interaction is controlled by the container, in this case 
the JBOSS CMP container. </P>
<P><BR><BR></P>
<P><IMG height=353 src="chapter6.files/J2EECaseStudyFlowDiagram.png" width=438 
align=left border=0 name=Graphic16><BR clear=left><BR><BR></P>
<P>All Items have been assigned a unique itemId for housekeeping purposes within 
MyStore, and all suppliers have been assigned a unique supplierID in addition to 
their username which is what they use in accessing the services of MyStore.</P>
<P><BR><BR></P>
<P><FONT color=#800000><I>Note : It is normal practice to access the business 
methods of CMP beans via a Session bean, that encapsulates the business logic 
and acts as an interface to the lower-level EJB components. In this case 
Supplier and Items are accessed via StoreAccess.</I></FONT></P>
<H3>Tasks :</H3>
<OL>
  <LI>
  <P>Create a CMP bean named Items under package au.com.tusc.cmp.</P>
  <LI>
  <P>Implement the ejbCreate method, with the values of all attributes being 
  passed as arguments and then assigned to the attributes using mutator (setter) 
  methods. </P>
  <LI>
  <P>Add a finder method named findBySupplierID with the following query and 
  signature: </P>
  <P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><FONT size=3><B>query 
  "SELECT OBJECT(b) FROM MyStoreItem as b where b.supplierID = ?1" 
  </B></FONT></FONT></FONT></P>
  <P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>method 
  "java.util.Collection findBySupplierID(java.lang.String supplierID)" 
  </B></FONT></FONT></P>
  <LI>
  <P>Add a finder method named findByOutOfStock with the following query and 
  signature: </P>
  <P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><FONT size=3><B>query 
  "SELECT OBJECT(c) FROM MyStoreItem as c where c.quantity = 0" 
  </B></FONT></FONT></FONT></P>
  <P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>method 
  "java.util.Collection findByOutOfStock()" </B></FONT></FONT></P>
  <LI>
  <P>Add a business method to get item details with the signature:</P>
  <P><B>public ItemData getItemData()</B></P>
  <LI>
  <P>Add another business method to register delivery of items with the 
  signature:</P>
  <P><B>public void fillStock(java.lang.Integer quantity)</B></P>
  <LI>
  <P>Add callback methods, required for getting/setting bean context for bean 
  with signatures:</P>
  <P><FONT color=#7f0055><B><FONT color=#000000>public void</FONT> <FONT 
  color=#000000>setEntityContext(EntityContext ctx)</FONT></B></FONT></P>
  <P><FONT color=#000000><B>public void unsetEntityContext()</B></FONT></P>
  <LI>
  <P>Deploy the Item Bean.</P>
  <LI>
  <P>Add a field to the StoreAccess bean to store the Item reference (obtained 
  from JNDI lookup):</P>
  <P><B><FONT color=#000000>private ItemLocalHome</FONT> item<FONT 
  color=#000000>LocalHome</FONT></B></P>
  <LI>
  <P>In the ejbCreate method of the StoreAccess bean store this reference in the 
  itemLocalHome variable by invoking the getLocalHome static method in 
  ItemUtil.</P>
  <LI>
  <P>Add a business method to StoreAccess Bean with the signature:</P>
  <P><B><FONT color=#000000>public ItemData</FONT> <FONT 
  color=#000000>getItemData(String itemID) </FONT></B></P>
  <LI>
  <P>Add another business method to StoreAccess Bean with the signature:</P>
  <P><B><FONT face="Nimbus Roman No9 L"><FONT color=#000000>public 
  java.util.ArrayList getOutOfStockItems()</FONT></FONT></B> </P>
  <LI>
  <P>Add another business method to StoreAccess Bean with the signature:</P>
  <P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>public 
  java.util.ArrayList getItemBySupplier(String supplierID)</B></FONT></FONT></P>
  <LI>
  <P><FONT color=#000000>Create a test client named SessionCMPClient under 
  package au.com.tusc.client. </FONT></P>
  <LI>
  <P><FONT color=#000000><FONT face="Nimbus Roman No9 L">Run your client and 
  test the bean.</FONT></FONT></P></LI></OL>
<H3 style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><BR></H3>
<H3 style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><B>Create Items CMP Entity Bean 
:</B></H3>
<P style="MARGIN-BOTTOM: 0in"><BR></P>
<P><B>Go To Package Explorer &gt; Expand Mystore (project) node &gt; select src, 
right click and a menu will pop up.</B></P>
<P><B>On the pop up menu &gt; New &gt; Lomboz EJB Creation Wizard.</B></P>
<P><B>Enter the package name au.com.tusc.cmp, the bean name Item and select the 
bean type as Container Manged Entity as shown below.</B></P>
<P><IMG height=410 src="chapter6.files/CreateEJBWizard.png" width=525 align=left 
border=0 name=Graphic17><BR clear=left><BR><BR></P>
<P><B>Go to Next and a new screen will pop up as shown below.</B></P>
<P><B>Enter MyStoreItem as the Schema Name.</B></P>
<P><B>Enter Item as the Table name.</B></P>
<P><B>Under Persistent Fields first enter itemID as the Field, with a Field Type 
of java.lang.String, ITEMID as its Database column, and VARCHAR for its SQL 
Type. </B></P>
<P><B>Press Add .. &gt; It will add this field in Fields section, select this 
new field &gt; Press Make Primary Key.</B></P>
<P style="MARGIN-BOTTOM: 0in"><IMG height=466 
src="chapter6.files/CreateEJBWizardAddPrimaryKey.png" width=523 align=left 
border=0 name=Graphic18><BR clear=left><BR></P>
<P style="MARGIN-BOTTOM: 0in"><B>Similarly, add all the rest of the fields of 
the items table as shown below.</B></P>
<P style="MARGIN-BOTTOM: 0in"><B>Add .. Field: supplierID, Field Type: 
java.lang.String, Database Column: SUPPLIERID, SQL Type: VARCHAR. </B></P>
<P style="MARGIN-BOTTOM: 0in"><B>Add .. Field: description, Field Type: 
java.lang.String, Database Column: DESCRIPTION, SQL Type: VARCHAR. </B></P>
<P style="MARGIN-BOTTOM: 0in"><B>Add .. Field: quantity, Field Type: 
java.lang.Integer, Database Column: QUANTITY, SQL Type: INTEGER. </B></P>
<P style="MARGIN-BOTTOM: 0in"><B>Add .. Field: price, Field Type: 
java.lang.Float, Database Column: PRICE, SQL Type: DECIMAL. </B></P>
<P style="MARGIN-BOTTOM: 0in"><BR></P>
<P><IMG height=408 src="chapter6.files/CreateEJBWizardAddAllFields.png" 
width=748 align=left border=0 name=Graphic5><BR clear=left><BR><BR></P>

⌨️ 快捷键说明

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