📄 6.mht
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: Chapter6 - Creating a CMP Entity Bean
Date: Sun, 5 Dec 2004 11:52:20 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="----=_NextPart_000_0000_01C4DAC0.E00D98E0";
type="text/html"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
This is a multi-part message in MIME format.
------=_NextPart_000_0000_01C4DAC0.E00D98E0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.tusc.com.au/tutorial/html/chap6.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Chapter6 - Creating a CMP Entity Bean</TITLE>
<META http-equiv=3DCONTENT-TYPE content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1479" name=3DGENERATOR>
<META content=3D"Glen McCallum and Vishal Sharma" name=3DAUTHOR>
<META content=3D20030729;9513900 name=3DCREATED>
<META content=3DVishal name=3DCHANGEDBY>
<META content=3D20030911;15553500 name=3DCHANGED>
<META=20
content=3D"Tutorial for building J2EE Applications using Eclipse, =
Lomboz, Jboss and Xdoclet."=20
name=3DDESCRIPTION>
<META=20
content=3D"Tutorial, J2EE, Lomboz, Eclipse, Jboss, Xdoclet, J2EE =
Patterns, EJB, Servlets, JSP, BMP, CMP, Session, Configuration, DAO"=20
name=3DKEYWORDS>
<META content=3D"14 days" name=3Drevisit-after><!-- Meta tags --><!-- =
Copyright =C2=A9 2003 TUSC. All rights reserved.--><!--- Header =
--></HEAD>
<BODY lang=3Den-US dir=3Dltr>
<FORM name=3DsearchForm action=3D../../../../search/index.php =
method=3Dpost>
<TABLE style=3D"PAGE-BREAK-BEFORE: always" cellSpacing=3D0 =
cellPadding=3D4=20
width=3D"100%" bgColor=3D#0a6a91 border=3D0>
<TBODY>
<TR>
<TD vAlign=3Dtop width=3D154>
<P><A href=3D"http://www.tusc.com.au./contact_us.php"><IMG =
height=3D125=20
alt=3DTUSC=20
=
src=3D"http://www.tusc.com.au/tutorial/images/header/tuscJ2eeLogo.gif"=20
width=3D149 align=3Dbottom border=3D0 =
name=3DGraphic1></A></P></TD>
<TD vAlign=3Dtop width=3D276>
<P><IMG height=3D73 alt=3D"Reliable, On-Time Delivery."=20
src=3D"http://www.tusc.com.au/tutorial/images/header/reliable.gif" =
width=3D268=20
align=3Dbottom border=3D0 name=3DGraphic2><!--search table =
--></P></TD>
<TD width=3D21></TD>
<TD width=3D43>
<P><IMG height=3D9 alt=3DSearch=20
src=3D"http://www.tusc.com.au/tutorial/images/elements/search.gif" =
width=3D41=20
align=3Dbottom border=3D0 name=3DGraphic3></P></TD>
<TD width=3D119>
<P><INPUT maxLength=3D50 size=3D12 name=3DKEYWORD></P></TD>
<TD width=3D31>
<P><INPUT type=3Dimage height=3D18 width=3D29=20
src=3D"http://www.tusc.com.au/tutorial/images/btns/go.gif" =
align=3Dbottom=20
name=3Dgo><INPUT type=3Dhidden value=3DY =
name=3DSECTION_ALL></P></TD>
<TD vAlign=3Dtop width=3D50>
<P><IMG height=3D73 alt=3DSpacer=20
src=3D"http://www.tusc.com.au/tutorial/images/invis.gif" =
width=3D50=20
align=3Dbottom border=3D0 name=3DGraphic5></P></TD>
<TD vAlign=3Dtop width=3D154>
<P><A href=3D"http://www.tusc.com.au./contact_us.php"><IMG =
height=3D125=20
alt=3DTUSC=20
=
src=3D"http://www.tusc.com.au/tutorial/images/header/tuscJ2eeLogo.gif"=20
width=3D149 align=3Dbottom border=3D0 =
name=3DGraphic1></A></P></TD>
<TD width=3D14>
<P><IMG height=3D10 alt=3Dspacer=20
src=3D"http://www.tusc.com.au/tutorial/images/invis.gif" =
width=3D14=20
align=3Dbottom border=3D0 name=3DGraphic58> =
</P></TD></TR></TBODY></TABLE></FORM>
<H2 align=3Dcenter><!--- Header finished -->Tutorial for building J2EE=20
Applications using JBOSS and ECLIPSE <IMG height=3D2=20
src=3D"http://www.tusc.com.au/tutorial/images/redrulr5.gif" width=3D775 =
align=3Dmiddle=20
border=3D0 name=3DGraphic1></H2>
<P><BR><BR></P>
<H1>Chapter 6. </H1>
<P><IMG height=3D2 =
src=3D"http://www.tusc.com.au/tutorial/images/blurulr6.gif"=20
width=3D785 align=3Dmiddle border=3D0 name=3DGraphic2></P>
<H2>Creating a CMP Entity Bean</H2>
<P>This chapter covers how to create a Container Managed Persistence =
(CMP) EJB=20
component. We will create two CMP beans, Item and Supplier as shown =
below. The=20
Item bean will be responsible for storing the details of items, such as =
their=20
availabability and their prices, for MyStore. The Supplier Bean stores =
details=20
of Suppliers to MyStore. Both beans interact with corresponding tables =
in the=20
database. In CMP this interaction is controlled by the container, in =
this case=20
the JBOSS CMP container. </P>
<P><BR><BR></P>
<P><IMG height=3D353=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/J2EECaseStudyFlowDiag=
ram.png"=20
width=3D438 align=3Dleft border=3D0 name=3DGraphic16><BR =
clear=3Dleft><BR><BR></P>
<P>All Items have been assigned a unique itemId for housekeeping =
purposes within=20
MyStore, and all suppliers have been assigned a unique supplierID in =
addition to=20
their username which is what they use in accessing the services of =
MyStore.</P>
<P><BR><BR></P>
<P><FONT color=3D#800000><I>Note : It is normal practice to access the =
business=20
methods of CMP beans via a Session bean, that encapsulates the business =
logic=20
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=20
passed as arguments and then assigned to the attributes using mutator =
(setter)=20
methods. </P>
<LI>
<P>Add a finder method named findBySupplierID with the following query =
and=20
signature: </P>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><FONT =
size=3D3><B>query=20
"SELECT OBJECT(b) FROM MyStoreItem as b where b.supplierID =3D ?1"=20
</B></FONT></FONT></FONT></P>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><B>method=20
"java.util.Collection findBySupplierID(java.lang.String supplierID)"=20
</B></FONT></FONT></P>
<LI>
<P>Add a finder method named findByOutOfStock with the following query =
and=20
signature: </P>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><FONT =
size=3D3><B>query=20
"SELECT OBJECT(c) FROM MyStoreItem as c where c.quantity =3D 0"=20
</B></FONT></FONT></FONT></P>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><B>method=20
"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=20
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=20
with signatures:</P>
<P><FONT color=3D#7f0055><B><FONT color=3D#000000>public void</FONT> =
<FONT=20
color=3D#000000>setEntityContext(EntityContext =
ctx)</FONT></B></FONT></P>
<P><FONT color=3D#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=20
from JNDI lookup):</P>
<P><B><FONT color=3D#000000>private ItemLocalHome</FONT> item<FONT=20
color=3D#000000>LocalHome</FONT></B></P>
<LI>
<P>In the ejbCreate method of the StoreAccess bean store this =
reference in the=20
itemLocalHome variable by invoking the getLocalHome static method in=20
ItemUtil.</P>
<LI>
<P>Add a business method to StoreAccess Bean with the signature:</P>
<P><B><FONT color=3D#000000>public ItemData</FONT> <FONT=20
color=3D#000000>getItemData(String itemID) </FONT></B></P>
<LI>
<P>Add another business method to StoreAccess Bean with the =
signature:</P>
<P><B><FONT face=3D"Nimbus Roman No9 L"><FONT color=3D#000000>public=20
java.util.ArrayList getOutOfStockItems()</FONT></FONT></B> </P>
<LI>
<P>Add another business method to StoreAccess Bean with the =
signature:</P>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><B>public=20
java.util.ArrayList getItemBySupplier(String =
supplierID)</B></FONT></FONT></P>
<LI>
<P><FONT color=3D#000000>Create a test client named SessionCMPClient =
under=20
package au.com.tusc.client. </FONT></P>
<LI>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L">Run your =
client and=20
test the bean.</FONT></FONT></P></LI></OL>
<H3 style=3D"MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><BR></H3>
<H3 style=3D"MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in"><B>Create Items CMP =
Entity Bean=20
:</B></H3>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P><B>Go To Package Explorer > Expand Mystore (project) node > =
select src,=20
right click and a menu will pop up.</B></P>
<P><B>On the pop up menu > New > Lomboz EJB Creation =
Wizard.</B></P>
<P><B>Enter the package name au.com.tusc.cmp, the bean name Item and =
select the=20
bean type as Container Manged Entity as shown below.</B></P>
<P><IMG height=3D410=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/CreateEJBWizard.png" =
width=3D525=20
align=3Dleft border=3D0 name=3DGraphic17><BR clear=3Dleft><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=20
of java.lang.String, ITEMID as its Database column, and VARCHAR for its =
SQL=20
Type. </B></P>
<P><B>Press Add .. > It will add this field in Fields section, select =
this=20
new field > Press Make Primary Key.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D466=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/CreateEJBWizardAddPri=
maryKey.png"=20
width=3D523 align=3Dleft border=3D0 name=3DGraphic18><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Similarly, add all the rest of the =
fields of=20
the items table as shown below.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Add .. Field: supplierID, Field Type: =
java.lang.String, Database Column: SUPPLIERID, SQL Type: VARCHAR. =
</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Add .. Field: description, Field =
Type:=20
java.lang.String, Database Column: DESCRIPTION, SQL Type: VARCHAR. =
</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Add .. Field: quantity, Field Type:=20
java.lang.Integer, Database Column: QUANTITY, SQL Type: INTEGER. =
</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Add .. Field: price, Field Type:=20
java.lang.Float, Database Column: PRICE, SQL Type: DECIMAL. </B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P><IMG height=3D408=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/CreateEJBWizardAddAll=
Fields.png"=20
width=3D748 align=3Dleft border=3D0 name=3DGraphic5><BR =
clear=3Dleft><BR><BR></P>
<P><B>After adding all these fields, press Finish.</B></P>
<P><B>This will create a package named au.com.tusc.cmp under src, and =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -