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

📄 6.mht

📁 Enterprise Java Bean的PPT文档,很好的东西.
💻 MHT
📖 第 1 页 / 共 5 页
字号:
ItemBean=20
will be be created within that package as shown below.</B></P>
<P><IMG height=3D560=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/ItemsBean.png" =
width=3D522=20
align=3Dleft border=3D0 name=3DGraphic3><BR clear=3Dleft><FONT =
color=3D#800000><I>Note: In=20
comparison with our earlier BMP Entity Beans (Customer &amp; Manager), =
more tags=20
have been generated at class level. Note also that CMP doesn't require a =
Data=20
Access Object (DAO) interface, as communication between database and =
bean is=20
controlled by the container.</I></FONT></P>
<P align=3Dleft>Let's first the generate EJB classes and then we will =
examine=20
these tags.</P>
<P><B>Go to node ItemBean under au.com.tusc.cmp &gt; LombozJ2EE &gt; Add =
EJB to=20
Module &gt; Select MyStoreMgr &gt; Ok.</B></P>
<P><B>Go to MyStoreMgr node &gt; LombozJ2EE &gt; Generate EJB =
classes.</B></P>
<P><FONT color=3D#800000><I>Note: All these steps are covered in =
previous chapters=20
(chapters 3 and 1) in detail, so please refer to these if you have any =
queries.=20
</I></FONT></P>
<P>Now, let's see what files have been generated by Xdoclet.</P>
<P><B>As shown below, the files generated are nearly the same as for =
BMP, except=20
there are no Primary Key or DAO classes, and there is now ItemCMP which =
extends=20
the ItemBean class. The remainder are the same as for BMP entity =
beans</B>.</P>
<P><IMG height=3D306=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/ItemsBeanGeneratedFil=
es.png"=20
width=3D216 align=3Dleft border=3D0 name=3DGraphic4><BR =
clear=3Dleft><B>Now, let's examine=20
these new tags, some of which have been covered in previous chapters. =
</B></P>
<OL>
  <LI>
  <P style=3D"MARGIN-BOTTOM: 0in"><B>@ejb.bean </B>tag provides =
information about=20
  the EJB. It is the one compulsory tag for all EJBs<B>.</B></P>
  <LI>
  <P style=3D"MARGIN-BOTTOM: 0in"><B>@ejb.persistence tag is being used =
at two=20
  levels, at class level and method level. </B>At class level it =
provides=20
  information about the persistence of a CMP entity bean, that is which =
database=20
  table this bean is going to interact with, which will provide that=20
  persistence. At method level it provides information about the mapping =
of the=20
  bean's persistent attributes to columns in that database table.</P>
  <LI>
  <P><B>@ejb.finder</B> tag defines a finder method for the home =
interface. This=20
  requires the EJB QL query to fetch the data and a signature for the =
method.=20
  This tag can be used for multiple finder methods.</P>
  <LI>
  <P style=3D"MARGIN-BOTTOM: 0in"><B>@ejb.persistence-field</B> method =
level tag=20
  is being deprecated in favour of @ejb.persistence tag, it provided =
information=20
  about persistent fields.</P>
  <LI>
  <P style=3D"MARGIN-BOTTOM: 0in"><B>@ejb.pk-field</B> tag defines the =
primary=20
  key.</P></LI></OL>
<P style=3D"MARGIN-BOTTOM: 0in">The code snippet below shows how =
persistent=20
attributes are declared in a CMP Entity Bean.</P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D558=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/ItemsBeanAttributes.p=
ng"=20
width=3D376 align=3Dleft border=3D0 name=3DGraphic6><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><FONT color=3D#800000><I>Note: All =
persistent=20
attributes are declared with abstract accessor and mutator methods in =
ItemBean.=20
Note also that in the case of a composite primary key you have to =
specify the=20
@ejb.pk-field tag on all other attributes/properties which combine to =
form the=20
overall key as well.</I></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<H3>Implement ejbCreate Method :</H3>
<P>The ejbCreate method is created by method is created by the Lomboz =
Bean=20
wizard, but we still have to add some code to complete it. Modify the =
signature=20
of ejbCreate, passing all the attributes as parameters and then setting =
all=20
these attributes using their associated mutator methods, as shown =
below.</P>
<P><FONT color=3D#800000><I>Errata note :- There is an error in the =
figure shown=20
below. The Integer attribute 'qunatity' should be called 'quantity'.=20
</I></FONT></P>
<P><IMG height=3D300=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/ItemsBeanEjbCreate.pn=
g"=20
width=3D526 align=3Dleft border=3D0 name=3DGraphic9><BR =
clear=3Dleft><FONT=20
color=3D#800000><I>Note : The other interesting aspect of ejbCreate here =
is its=20
return type, as its return type has to be same as the primary key type =
(e.g. it=20
has to of type String, Integer, Float, or whatever). In this case it is =
String -=20
the type of itemID, and when implemented it should return null.(Refer to =
the EJB=20
Spec 10.5.1). </I></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<H3 style=3D"MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in">Add Finder Method =
:</H3>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P><FONT color=3D#800000><I>Note: An entity bean's home interface =
defines one or=20
more finder methods, to find an entity object or collection of entity =
objects.=20
The name of each finder method starts with the prefix 'find', such as =
findPrice=20
or findQuantity in our case. Every finder method except =
findByPrimaryKey(key)=20
must be associated with a query element in the deployment descriptor. =
The entity=20
bean provider declares the EJB QL finder query and associates it with =
the finder=20
method in the deployment descriptor. A finder method is normally =
characterized=20
by an EJB QL query string specified via the query element. Refer to the =
EJB Spec=20
10.5.6. This is covered in the Exercise section of this chapter. =
</I></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Now let's add a finder method to our =
bean class=20
to find items supplied by a particular supplier</B>. </P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>In order to add this finder method we =
have to=20
declare a class level tag as discussed above, that is =
@ejb.finder.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>So, add this tag:</B></P><PRE><FONT =
face=3DSans><FONT size=3D3><FONT color=3D#3f5fbf>@ejb.finder</FONT> =
</FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT =
color=3D#3f5fbf>query=3D"SELECT</FONT> <FONT =
color=3D#3f5fbf>OBJECT(a)</FONT> <FONT color=3D#3f5fbf>FROM</FONT> <FONT =
color=3D#3f5fbf>MyStoreItem</FONT> <FONT color=3D#3f5fbf>a</FONT> <FONT =
color=3D#3f5fbf>where</FONT> <FONT color=3D#3f5fbf>a.supplierID</FONT> =
<FONT color=3D#3f5fbf>=3D</FONT> <FONT color=3D#3f5fbf>?1"</FONT>  =
</FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT =
color=3D#3f5fbf>signature=3D"java.util.Collection</FONT> <FONT =
color=3D#3f5fbf>findBySupplierID(java.lang.String</FONT> <FONT =
color=3D#3f5fbf>supplierID)"</FONT>  </FONT></FONT></PRE>
<P><FONT color=3D#800000><I>Note : In EJB QL, instead of the name of the =
table,=20
the schema name is used (in this case it is MyStoreItem, rather then =
specifying=20
Item as you would for an SQL query). Similarly the column names that =
would be=20
used in SQL are replaced by their corresponding attributes as declared =
in the=20
bean.</I></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Generate your EJB classes to see what =
this tag=20
and the previous finder tag have created in the Home Interface as shown =
below in=20
this code snippet from the ItemLocalHome interface.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D442=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/ItemsLocalHome.png" =
width=3D582=20
align=3Dleft border=3D0 name=3DGraphic7><BR clear=3Dleft><B>It has four =
methods,=20
including the two finder methods generated by the finder tags declared =
at class=20
level. The other two, create and findByPrimaryKey are created by=20
Xdoclet(</B>because of the &lt;entitycmp/&gt; tag in=20
ejbGenerate.xml<B>).</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Add another finder method to find =
out-of-stock=20
items in MyStore.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Add the following =
tag.</B></P><PRE><FONT color=3D#3f5fbf><FONT face=3DSans><FONT =
size=3D3>@ejb.finder </FONT></FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT =
color=3D#3f5fbf>query=3D"SELECT</FONT> <FONT =
color=3D#3f5fbf>OBJECT(c)</FONT> <FONT color=3D#3f5fbf>FROM</FONT> <FONT =
color=3D#3f5fbf>MyStoreItem</FONT> <FONT color=3D#3f5fbf>c</FONT> <FONT =
color=3D#3f5fbf>where</FONT> <FONT color=3D#3f5fbf>c.quantity</FONT> =
<FONT color=3D#3f5fbf>=3D</FONT> <FONT color=3D#3f5fbf>0"</FONT>  =
</FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT =
color=3D#3f5fbf>signature=3D"java.util.Collection</FONT> <FONT =
color=3D#3f5fbf>findByOutOfStock()"</FONT></FONT></FONT></PRE>
<P><B>Code snippet from ItemLocalHome after generating EJB classes for =
these=20
finder methods.</B></P>
<P><IMG height=3D490=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/ItemsLocalHomeByStock=
.png"=20
width=3D450 align=3Dleft border=3D0 name=3DGraphic24><BR =
clear=3Dleft><BR>Now, generate=20
your EJB classes and analyze the relevant classes. All the finder =
methods are=20
complete. Let's add some business methods now.</P>
<H3>Add Business Methods :</H3>
<P><B>Now, add a business method with this signature:</B></P>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><B>public =
ItemData=20
getItemData()</B></FONT></FONT></P>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><B>.. with =
Interface type=20
as local.</B></FONT></FONT></P>
<P><FONT color=3D#800000><I><FONT face=3D"Nimbus Roman No9 L">Note : The =
steps to=20
add a business method are covered in previous chapters (1 and 3), so =
please=20
refer to them. Also we have chosen the</FONT> Interface type as local =
because=20
these methods will be invoked within the same Java Virtual Machine.=20
</I></FONT></P>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><FONT =
size=3D3><B>This will=20
provide description of individual items in MyStore. Add some debug =
statements=20
and return an instance of ItemData as shown below in this code snippet =
from the=20
Item bean. </B></FONT></FONT></FONT></P>
<P><IMG height=3D168=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/ItemsBeanGetItemsData=
.png"=20
width=3D380 align=3Dleft border=3D0 name=3DGraphic8><BR =
clear=3Dleft><BR><BR></P>
<P><B>Add another business method with the following signature:</B></P>
<P><B>public void fillStock (Integer quantity)</B></P>
<P><B>.. with Interface type as local. This will increment the items =
available=20
to MyStore after the delivery of further items. Add some debug =
statements and=20
following lines to complete the method.</B></P><PRE><FONT =
face=3DSans><FONT size=3D3>Integer qty   =3D <FONT =
color=3D#7f0055>new</FONT> Integer(  (quantity.intValue() + =
getQuantity().intValue()) ) ;</FONT></FONT>
<FONT face=3DSans><FONT size=3D3><FONT =
color=3D#000000>setQuantity</FONT> <FONT color=3D#000000>(</FONT> <FONT =
color=3D#000000>qty</FONT> <FONT =
color=3D#000000>);</FONT></FONT></FONT></PRE>
<P>Code snippet of fillStock in ItemBean shown below.</P>
<P><IMG height=3D212=20
src=3D"http://www.tusc.com.au/tutorial/images/chap6/ItemsBeanDeliverItem.=
png"=20
width=3D584 align=3Dleft border=3D0 name=3DGraphic10><BR =
clear=3Dleft><BR><BR></P>
<H3>Add Callback Method</H3>
<P>Unlike BMP (where they were generated) we have to add callback =
methods which=20
will be overridden in the ItemCMP class.</P>
<P>First import the following package: <B><FONT face=3DSans><FONT=20
color=3D#000000>javax.ejb.EntityContext</FONT></FONT></B></P>
<P><B>Add a field to store the entity context.</B></P>
<P>protected EntityContext eContext;</P>
<P><B>Add a method setEntityContext with entityContext as its parameter =
and=20
assign that to your entityContext variable.</B></P>
<P><B>Similarly add a method unsetEntityContext, which sets the =
entityContext=20
variable to null.</B></P>
<P><B>Code snippet for both methods is shown below.</B></P>
<P><IMG height=3D318=20

⌨️ 快捷键说明

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