📄 chapter6.htm
字号:
<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 ItemBean
will be be created within that package as shown below.</B></P>
<P><IMG height=560 src="chapter6.files/ItemsBean.png" width=522 align=left
border=0 name=Graphic3><BR clear=left><FONT color=#800000><I>Note: In comparison
with our earlier BMP Entity Beans (Customer & Manager), more tags have been
generated at class level. Note also that CMP doesn't require a Data Access
Object (DAO) interface, as communication between database and bean is controlled
by the container.</I></FONT></P>
<P align=left>Let's first the generate EJB classes and then we will examine
these tags.</P>
<P><B>Go to node ItemBean under au.com.tusc.cmp > LombozJ2EE > Add EJB to
Module > Select MyStoreMgr > Ok.</B></P>
<P><B>Go to MyStoreMgr node > LombozJ2EE > Generate EJB classes.</B></P>
<P><FONT color=#800000><I>Note: All these steps are covered in previous chapters
(chapters 3 and 1) in detail, so please refer to these if you have any queries.
</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
there are no Primary Key or DAO classes, and there is now ItemCMP which extends
the ItemBean class. The remainder are the same as for BMP entity beans</B>.</P>
<P><IMG height=306 src="chapter6.files/ItemsBeanGeneratedFiles.png" width=216
align=left border=0 name=Graphic4><BR clear=left><B>Now, let's examine these new
tags, some of which have been covered in previous chapters. </B></P>
<OL>
<LI>
<P style="MARGIN-BOTTOM: 0in"><B>@ejb.bean </B>tag provides information about
the EJB. It is the one compulsory tag for all EJBs<B>.</B></P>
<LI>
<P style="MARGIN-BOTTOM: 0in"><B>@ejb.persistence tag is being used at two
levels, at class level and method level. </B>At class level it provides
information about the persistence of a CMP entity bean, that is which database
table this bean is going to interact with, which will provide that
persistence. At method level it provides information about the mapping of the
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
requires the EJB QL query to fetch the data and a signature for the method.
This tag can be used for multiple finder methods.</P>
<LI>
<P style="MARGIN-BOTTOM: 0in"><B>@ejb.persistence-field</B> method level tag
is being deprecated in favour of @ejb.persistence tag, it provided information
about persistent fields.</P>
<LI>
<P style="MARGIN-BOTTOM: 0in"><B>@ejb.pk-field</B> tag defines the primary
key.</P></LI></OL>
<P style="MARGIN-BOTTOM: 0in">The code snippet below shows how persistent
attributes are declared in a CMP Entity Bean.</P>
<P style="MARGIN-BOTTOM: 0in"><IMG height=558
src="chapter6.files/ItemsBeanAttributes.png" width=376 align=left border=0
name=Graphic6><BR clear=left><BR></P>
<P style="MARGIN-BOTTOM: 0in"><FONT color=#800000><I>Note: All persistent
attributes are declared with abstract accessor and mutator methods in ItemBean.
Note also that in the case of a composite primary key you have to specify the
@ejb.pk-field tag on all other attributes/properties which combine to form the
overall key as well.</I></FONT></P>
<P style="MARGIN-BOTTOM: 0in"><BR></P>
<H3>Implement ejbCreate Method :</H3>
<P>The ejbCreate method is created by method is created by the Lomboz Bean
wizard, but we still have to add some code to complete it. Modify the signature
of ejbCreate, passing all the attributes as parameters and then setting all
these attributes using their associated mutator methods, as shown below.</P>
<P><FONT color=#800000><I>Errata note :- There is an error in the figure shown
below. The Integer attribute 'qunatity' should be called 'quantity'.
</I></FONT></P>
<P><IMG height=300 src="chapter6.files/ItemsBeanEjbCreate.png" width=526
align=left border=0 name=Graphic9><BR clear=left><FONT color=#800000><I>Note :
The other interesting aspect of ejbCreate here is its return type, as its return
type has to be same as the primary key type (e.g. it has to of type String,
Integer, Float, or whatever). In this case it is String - the type of itemID,
and when implemented it should return null.(Refer to the EJB Spec 10.5.1).
</I></FONT></P>
<P style="MARGIN-BOTTOM: 0in"><BR></P>
<H3 style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in">Add Finder Method :</H3>
<P style="MARGIN-BOTTOM: 0in"><BR></P>
<P><FONT color=#800000><I>Note: An entity bean's home interface defines one or
more finder methods, to find an entity object or collection of entity objects.
The name of each finder method starts with the prefix 'find', such as findPrice
or findQuantity in our case. Every finder method except findByPrimaryKey(key)
must be associated with a query element in the deployment descriptor. The entity
bean provider declares the EJB QL finder query and associates it with the finder
method in the deployment descriptor. A finder method is normally characterized
by an EJB QL query string specified via the query element. Refer to the EJB Spec
10.5.6. This is covered in the Exercise section of this chapter. </I></FONT></P>
<P style="MARGIN-BOTTOM: 0in"><B>Now let's add a finder method to our bean class
to find items supplied by a particular supplier</B>. </P>
<P style="MARGIN-BOTTOM: 0in"><B>In order to add this finder method we have to
declare a class level tag as discussed above, that is @ejb.finder.</B></P>
<P style="MARGIN-BOTTOM: 0in"><B>So, add this tag:</B></P><PRE><FONT face=Sans><FONT size=3><FONT color=#3f5fbf>@ejb.finder</FONT> </FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#3f5fbf>query="SELECT</FONT> <FONT color=#3f5fbf>OBJECT(a)</FONT> <FONT color=#3f5fbf>FROM</FONT> <FONT color=#3f5fbf>MyStoreItem</FONT> <FONT color=#3f5fbf>a</FONT> <FONT color=#3f5fbf>where</FONT> <FONT color=#3f5fbf>a.supplierID</FONT> <FONT color=#3f5fbf>=</FONT> <FONT color=#3f5fbf>?1"</FONT> </FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#3f5fbf>signature="java.util.Collection</FONT> <FONT color=#3f5fbf>findBySupplierID(java.lang.String</FONT> <FONT color=#3f5fbf>supplierID)"</FONT> </FONT></FONT></PRE>
<P><FONT color=#800000><I>Note : In EJB QL, instead of the name of the table,
the schema name is used (in this case it is MyStoreItem, rather then specifying
Item as you would for an SQL query). Similarly the column names that would be
used in SQL are replaced by their corresponding attributes as declared in the
bean.</I></FONT></P>
<P style="MARGIN-BOTTOM: 0in"><B>Generate your EJB classes to see what this tag
and the previous finder tag have created in the Home Interface as shown below in
this code snippet from the ItemLocalHome interface.</B></P>
<P style="MARGIN-BOTTOM: 0in"><IMG height=442
src="chapter6.files/ItemsLocalHome.png" width=582 align=left border=0
name=Graphic7><BR clear=left><B>It has four methods, including the two finder
methods generated by the finder tags declared at class level. The other two,
create and findByPrimaryKey are created by Xdoclet(</B>because of the
<entitycmp/> tag in ejbGenerate.xml<B>).</B></P>
<P style="MARGIN-BOTTOM: 0in"><BR></P>
<P style="MARGIN-BOTTOM: 0in"><B>Add another finder method to find out-of-stock
items in MyStore.</B></P>
<P style="MARGIN-BOTTOM: 0in"><B>Add the following tag.</B></P><PRE><FONT color=#3f5fbf><FONT face=Sans><FONT size=3>@ejb.finder </FONT></FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#3f5fbf>query="SELECT</FONT> <FONT color=#3f5fbf>OBJECT(c)</FONT> <FONT color=#3f5fbf>FROM</FONT> <FONT color=#3f5fbf>MyStoreItem</FONT> <FONT color=#3f5fbf>c</FONT> <FONT color=#3f5fbf>where</FONT> <FONT color=#3f5fbf>c.quantity</FONT> <FONT color=#3f5fbf>=</FONT> <FONT color=#3f5fbf>0"</FONT> </FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#3f5fbf>signature="java.util.Collection</FONT> <FONT color=#3f5fbf>findByOutOfStock()"</FONT></FONT></FONT></PRE>
<P><B>Code snippet from ItemLocalHome after generating EJB classes for these
finder methods.</B></P>
<P><IMG height=490 src="chapter6.files/ItemsLocalHomeByStock.png" width=450
align=left border=0 name=Graphic24><BR clear=left><BR>Now, generate your EJB
classes and analyze the relevant classes. All the finder methods are 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=#000000><FONT face="Nimbus Roman No9 L"><B>public ItemData
getItemData()</B></FONT></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>.. with Interface type
as local.</B></FONT></FONT></P>
<P><FONT color=#800000><I><FONT face="Nimbus Roman No9 L">Note : The steps to
add a business method are covered in previous chapters (1 and 3), so please
refer to them. Also we have chosen the</FONT> Interface type as local because
these methods will be invoked within the same Java Virtual Machine.
</I></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><FONT size=3><B>This will
provide description of individual items in MyStore. Add some debug statements
and return an instance of ItemData as shown below in this code snippet from the
Item bean. </B></FONT></FONT></FONT></P>
<P><IMG height=168 src="chapter6.files/ItemsBeanGetItemsData.png" width=380
align=left border=0 name=Graphic8><BR clear=left><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
to MyStore after the delivery of further items. Add some debug statements and
following lines to complete the method.</B></P><PRE><FONT face=Sans><FONT size=3>Integer qty = <FONT color=#7f0055>new</FONT> Integer( (quantity.intValue() + getQuantity().intValue()) ) ;</FONT></FONT>
<FONT face=Sans><FONT size=3><FONT color=#000000>setQuantity</FONT> <FONT color=#000000>(</FONT> <FONT color=#000000>qty</FONT> <FONT color=#000000>);</FONT></FONT></FONT></PRE>
<P>Code snippet of fillStock in ItemBean shown below.</P>
<P><IMG height=212 src="chapter6.files/ItemsBeanDeliverItem.png" width=584
align=left border=0 name=Graphic10><BR clear=left><BR><BR></P>
<H3>Add Callback Method</H3>
<P>Unlike BMP (where they were generated) we have to add callback methods which
will be overridden in the ItemCMP class.</P>
<P>First import the following package: <B><FONT face=Sans><FONT
color=#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
assign that to your entityContext variable.</B></P>
<P><B>Similarly add a method unsetEntityContext, which sets the entityContext
variable to null.</B></P>
<P><B>Code snippet for both methods is shown below.</B></P>
<P><IMG height=318 src="chapter6.files/ItemsBeanEnityContext.png" width=394
align=left border=0 name=Graphic11><BR clear=left><FONT color=#000000><FONT
face="Nimbus Roman No9 L"><B>Now all business and finder methods are complete,
so it's time to generate EJB classes.</B></FONT></FONT></P>
<P><B><FONT face="Nimbus Roman No9 L"><FONT color=#000000>Let's examine the
generated</FONT></FONT></B> <B>ItemCMP class, which is of most interest</B>
.</P>
<P>Unlike our BMP bean all persistent attribute behavior is being overridden by
abstract methods. This is because the EJB container is responsible for
maintaining their persistence. </P>
<P><IMG height=586 src="chapter6.files/ItemCMPAbstractMethods.png" width=444
align=left border=0 name=Graphic22><BR clear=left><BR><BR></P>
<P>All the callback methods we have implemented are being overridden as shown
below. </P>
<P><IMG height=166 src="chapter6.files/ItemCMPCallBackMethods.png" width=378
align=left border=0 name=Graphic23><BR clear=left><FONT color=#800000><I>Note :
There are no ejbFinder methods in this class as with BMP beans, as all this is
controlled by the container. Also as pointed out before, there is no PrimaryKey
class generated and there is no need for a DAO class, as this too is controlled
by the container.</I></FONT></P>
<P><FONT color=#000000><FONT face="Nimbus Roman No9 L"><B>Now, before we deploy
our bean, we will just have a look at ejb-jar.xml and jboss.xml to see what
descriptors are generated.</B></FONT></FONT></P>
<P><FONT color=#800000><FONT face="Nimbus Roman No9 L"><I>Note : We don't have
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -