Tutorial for building J2EE Applications using JBOSS and ECLIPSE-6

原创 2004年08月30日 17:35:00

Chapter 6.

blurulr6.gif

Creating a CMP Entity Bean

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.



J2EECaseStudyFlowDiagram.png


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.



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.

Tasks :

  1. Create a CMP bean named Items under package au.com.tusc.cmp.

  2. Implement the ejbCreate method, with the values of all attributes being passed as arguments and then assigned to the attributes using mutator (setter) methods.

  3. Add a finder method named findBySupplierID with the following query and signature:

    query "SELECT OBJECT(b) FROM MyStoreItem as b where b.supplierID = ?1"

    method "java.util.Collection findBySupplierID(java.lang.String supplierID)"

  4. Add a finder method named findByOutOfStock with the following query and signature:

    query "SELECT OBJECT(c) FROM MyStoreItem as c where c.quantity = 0"

    method "java.util.Collection findByOutOfStock()"

  5. Add a business method to get item details with the signature:

    public ItemData getItemData()

  6. Add another business method to register delivery of items with the signature:

    public void fillStock(java.lang.Integer quantity)

  7. Add callback methods, required for getting/setting bean context for bean with signatures:

    public void setEntityContext(EntityContext ctx)

    public void unsetEntityContext()

  8. Deploy the Item Bean.

  9. Add a field to the StoreAccess bean to store the Item reference (obtained from JNDI lookup):

    private ItemLocalHome itemLocalHome

  10. In the ejbCreate method of the StoreAccess bean store this reference in the itemLocalHome variable by invoking the getLocalHome static method in ItemUtil.

  11. Add a business method to StoreAccess Bean with the signature:

    public ItemData getItemData(String itemID)

  12. Add another business method to StoreAccess Bean with the signature:

    public java.util.ArrayList getOutOfStockItems()

  13. Add another business method to StoreAccess Bean with the signature:

    public java.util.ArrayList getItemBySupplier(String supplierID)

  14. Create a test client named SessionCMPClient under package au.com.tusc.client.

  15. Run your client and test the bean.


Create Items CMP Entity Bean :


Go To Package Explorer > Expand Mystore (project) node > select src, right click and a menu will pop up.

On the pop up menu > New > Lomboz EJB Creation Wizard.

Enter the package name au.com.tusc.cmp, the bean name Item and select the bean type as Container Manged Entity as shown below.

CreateEJBWizard.png


Go to Next and a new screen will pop up as shown below.

Enter MyStoreItem as the Schema Name.

Enter Item as the Table name.

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.

Press Add .. > It will add this field in Fields section, select this new field > Press Make Primary Key.

CreateEJBWizardAddPrimaryKey.png

Similarly, add all the rest of the fields of the items table as shown below.

Add .. Field: supplierID, Field Type: java.lang.String, Database Column: SUPPLIERID, SQL Type: VARCHAR.

Add .. Field: description, Field Type: java.lang.String, Database Column: DESCRIPTION, SQL Type: VARCHAR.

Add .. Field: quantity, Field Type: java.lang.Integer, Database Column: QUANTITY, SQL Type: INTEGER.

Add .. Field: price, Field Type: java.lang.Float, Database Column: PRICE, SQL Type: DECIMAL.


CreateEJBWizardAddAllFields.png


After adding all these fields, press Finish.

This will create a package named au.com.tusc.cmp under src, and ItemBean will be be created within that package as shown below.

ItemsBean.png
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.

Let's first the generate EJB classes and then we will examine these tags.

Go to node ItemBean under au.com.tusc.cmp > LombozJ2EE > Add EJB to Module > Select MyStoreMgr > Ok.

Go to MyStoreMgr node > LombozJ2EE > Generate EJB classes.

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.

Now, let's see what files have been generated by Xdoclet.

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.

ItemsBeanGeneratedFiles.png
Now, let's examine these new tags, some of which have been covered in previous chapters.

  1. @ejb.bean tag provides information about the EJB. It is the one compulsory tag for all EJBs.

  2. @ejb.persistence tag is being used at two levels, at class level and method level. 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.

  3. @ejb.finder 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.

  4. @ejb.persistence-field method level tag is being deprecated in favour of @ejb.persistence tag, it provided information about persistent fields.

  5. @ejb.pk-field tag defines the primary key.

The code snippet below shows how persistent attributes are declared in a CMP Entity Bean.

ItemsBeanAttributes.png

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.


Implement ejbCreate Method :

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.

Errata note :- There is an error in the figure shown below. The Integer attribute 'qunatity' should be called 'quantity'.

ItemsBeanEjbCreate.png
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).


Add Finder Method :


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.

Now let's add a finder method to our bean class to find items supplied by a particular supplier.

In order to add this finder method we have to declare a class level tag as discussed above, that is @ejb.finder.

So, add this tag:


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.

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.

ItemsLocalHome.png
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(because of the <entitycmp/> tag in ejbGenerate.xml).


Add another finder method to find out-of-stock items in MyStore.

Add the following tag.


Code snippet from ItemLocalHome after generating EJB classes for these finder methods.

ItemsLocalHomeByStock.png

Now, generate your EJB classes and analyze the relevant classes. All the finder methods are complete. Let's add some business methods now.

Add Business Methods :

Now, add a business method with this signature:

public ItemData getItemData()

.. with Interface type as local.

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 Interface type as local because these methods will be invoked within the same Java Virtual Machine.

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.

ItemsBeanGetItemsData.png


Add another business method with the following signature:

public void fillStock (Integer quantity)

.. 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.


Code snippet of fillStock in ItemBean shown below.

ItemsBeanDeliverItem.png


Add Callback Method

Unlike BMP (where they were generated) we have to add callback methods which will be overridden in the ItemCMP class.

First import the following package: javax.ejb.EntityContext

Add a field to store the entity context.

protected EntityContext eContext;

Add a method setEntityContext with entityContext as its parameter and assign that to your entityContext variable.

Similarly add a method unsetEntityContext, which sets the entityContext variable to null.

Code snippet for both methods is shown below.

ItemsBeanEnityContext.png
Now all business and finder methods are complete, so it's time to generate EJB classes.

Let's examine the generated ItemCMP class, which is of most interest .

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.

ItemCMPAbstractMethods.png


All the callback methods we have implemented are being overridden as shown below.

ItemCMPCallBackMethods.png
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.

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.

Note : We don't have to write any descriptors for the data sources as we did with Session and BMP beans, as the EJB container is responsible for that.

ejbJarFirst.png
As shown in the code snippet above from the ejb-jar.xml file, all abstract methods are generated as persistent fields under the tag <cmp-field> because of the tag @persistence-field declared at each accessor method. Also the primary key class descriptor is generated under the <primary-key-class> tag and the primary key field is generated under the <primarykey-field> because of the tag @field-pk declared for the relevant attribute(s).

Descriptors for finder methods are generated along with the query defined to fetch the data as shown below. These finder tags are generated by the @ejb.finder tag declared at class level.

ejbJarSecond.png
And in the jboss.xml file, the following descriptors are generated due to the tag @ejb.bean declared at class level, as shown below.

jboss.png
Note : @ejb.bean tag has been covered in previous chapters.

Item Bean functionality is complete in and ready for deployment.

Deploy Item Bean :

Go to Lomboz J2EE View > expand node MyStore > expand MyStoreMgr > select Jboss 3.2.1 ALL.

Right click > select Debug Sever on the pop up menu.

Note : This is to start your server, if you are already running your server then skip these steps and go to the next one.

Go to MyStoreMgr node in LombozJ2EE view > right click > select Deploy on the pop up menu as shown below.

LombozJ2EEViewDeployBean.png


Note : All these steps have been detailed in previous chapters (1 and 3), so, please refer to them.

Messages in the console will indicate whether deployment of your bean has been successful or not.

Now, lets modify StoreAccessBean to invoke methods on ItemBean.

Add a field to store our Item reference (obtained from JNDI lookup).


In the ejbCreate method store this reference in the itemLocalHome variable by invoking the getLocalHome static method in the ItemUtil class as shown in this code snippet below from StoreAccess Bean.

StoreAccessEjbCreate.png


Add a Business Method to StoreAccess :

Add another business method to the StoreAccess Bean which will invoke this business method on our Item Bean.

Now, add a business method with this signature:

public ItemData getItemData(String itemID)

.. with Interface type as Remote. As managers will log on to MyStore with username, once authenticated they will be identified by their userid. They can then retrieve their account details from MyStore using that userid. A Manager can invoke methods on the Item Bean to examine the inventory of MyStore.

Note : Steps to create business methods are covered in previous chapters.

Now invoke one of Item's finder methods via the reference variable we have created in the ejbCreate method.

        

Now invoke the business method of Customer on this reference.


Code snippet for this business method is shown below.

StoreAccessGetItemData.png


Add another business method to StoreAccess Bean.

Add a business method with this signature:

public java.util.ArrayList getOutOfStockItems()

.. with Interface type as Remote. This will return the items which are out of stock in MyStore.

Create two variables of type Collection and ArrayList respectively, as the finder method for Items returns a Collection and this method will return an ArrayList, after populating items which are out of stock from the returned Collection.

      

Now invoke one of the finder methods of Item on the reference variable we have created in the ejbCreate method.


Now iterate through the collection of out of stock items and add to the ArrayList.


Code snippet for this business method is shown below.

StoreAccessGetItemOutOfStock.png


Add another business method to StoreAccess Bean.

Add a business method with this signature:

public java.util.ArrayList getItemBySupplier(String supplierID)

.. with Interface type as Remote. This will return the items which are provided to MyStore by a given supplier.

Create two variables of type Collection and ArrayList respectively as the finder method for Items returns a Collection and this method will return ArrayList, after populating items which are supplied by a particular supplier from the returned Collection.

      

Now invoke one of the finder methods of Item on the reference variable we have created in the ejbCreate method.


Now iterate through the collection of items for this supplier and add to the ArrayList.


StoreAccessGetItemBySupplier.png


Now all the methods in StoreAccess Bean for accessing Item's business methods have been added. The only remaining bit is the deployment descriptors required for linking/referencing of StoreAccess and Item Bean. So we will the add two tags shown below.

StoreAccessDeploymentDescriptors.png


First add the tag shown below at class level in StoreAccess Bean.


This tag will generate deployment descriptors in 'ejb-jar.xml', as StoreAccessBean has to know which bean it is referring to, what is its view-type and ref-name. This will generate these descriptors as shown below.

Note : View type is local as both are in the same Java Virtual Machine, otherwise it would be Remote. Secondly ref-name is generated as ItemLocalHome, as we are using that rather than ItemHome (which was also generated, but is used in the Remote case).

ejbJarStoreAccess.png


Now add a second tag (shown below) at class level in StoreAccess Bean.


This tag will generate deployment descriptors in 'jboss.xml', as the application server has to know what jndi-name the Item bean has been registered with. This will generate these descriptors as shown below.

Note : Ref-name and jndi-name are used for bean as local (in same JVM).

jbossStoreAccess.png
Note : We can see in the code snippet above the deployment descriptors generated by tag @jboss. For the view type 'local' it generates incorrect deployment descriptors, as discussed in the previous chapter. So every time we use this tag we have to change the <ejb-ref> to <ejb-local-ref> before deployment. Caution here, that you do this change manually when you finally finish regenerating your EJB classes, because every time you regenerate your classes, 'jboss.xml' will be overwritten.

Now our Item Bean is complete after these changes, so deploy your bean again now, from the Lomboz J2EE View, as per the steps shown above and in previous chapters. Messages will appear in the console showing the status of deployment.

Once the bean is deployed successfully, create a test client which will invoke the loginUser method on StoreAccess Bean , getCustomerData on Customer Bean, getManagerData on Manager Bean and getOutOfStockItems on Item Beam.


Create your Test Client :


Go to Project MytStore node > select src node and expand it > select au.com.tusc.client package > right click.

Select New on the pop up menu > select Lomboz EJB Test Client Wizard.

Select package name au.com.tusc.client, name as SessionCMPClient and select Ejb Home as au.com.tusc.session.StoreAccessHome and Ejb Interface as au.com.tusc.session.StoreAccess.

This will generate required methods for you in your SessionCMPClient class and you only have to invoke loginUser, getCustomerData, getManagerData (Manager Bean was developed as part of an exercise in the previous chapter) and getOutOfStockItems as shown below.

Now, the last step is to write the code for your client.

In order to access out of stock items we need an Iterator and an ArrayList for items. So, declare two variables of these respective types, and import the packages for these types (which are java.util.ArrayList and java.util.Iterator).


And now add these lines of code to invoke the methods mentioned above.










                                                







Test your Client :

Now, in order to test your client, Select SessionCMPClient node > Go at top level menu and select the icon with the 'Running Man'.

On that select 'Run as' > select Java Application, as shown below.

RunTestClient.png


Now on your console, if you get a reply saying two items out of stock which are 'CALCULATOR' and 'CLOCK', then your call is successful as shown below.

ResultTestClient.png


Exercise :

Now, here is an exercise for you. In order to proceed further, implement Supplier as a CMP Entity Bean. The tasks are given below:

  1. Create a CMP Bean named Supplier under package au.com.tusc.cmp.

  2. Implement the ejbCreate method, with all attributes passed as arguments and then assigned to attributes using mutator methods.

  3. Add a find method named findUserID with query and signature:

    query "SELECT OBJECT(b) FROM MyStoreSupplier as b where b.userID = ?1"

    method au.com.tusc.cmp.SupplierLocal findUserID(java.lang.String userID)

    Note : The method signature is find<cmp attribute> instead of findByPrimaryKey, because finder methods for non-key CMP attributes use this convention. Accordingly the return type for finder methods will be either Collection or <entity type>, as specified in the EJB specification, sections 10.5.6 and 10.5.2 respectively.

  4. Add a business method to get supplier details with signature:

    public SupplierData getSupplierData()

  5. Add a business method to get request items from various suppliers with signature:

    public void requestItem(String itemID, Integer quantity)

  6. Add callback methods, required for setting/unsetting bean context with signatures:

    public void setEntityContext(EntityContext ctx)

    public void unsetEntityContext()

  7. Deploy the Supplier Bean.

  8. Add a field to StoreAcess Bean to store its reference:

    private SupplierLocalHome supplierLocalHome

  9. In the ejbCreate method of StoreAccess Bean store a reference in supplierLocalHome variable by invoking the getLocalHome static method in supplierUtil.

  1. Add a business method to StoreAccess Bean with signature:

    public ItemData getItemData(String itemID)

  2. Add the following tags for deployment at class level for linking/referencing Supplier.

    
    
 

    
  1. Test your Supplier Bean by running your Test Client created for Item named SessionCMPClient.

Note : All these steps are same as were done for Item. Implement this Bean which will be used in subsequent chapters.

In case you have difficulty, we have provided a SupplierBean class, modified StoreAccessBean class and SesiomCMPClient class. You can download these files under downloads below.

Downloads :

SupplierBean

StoreAccessBean

SessionCMPClient

《利用python做数据分析》第十章:时间序列分析

import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline //anac...
  • qq_16735407
  • qq_16735407
  • 2016年02月01日 16:33
  • 6180

freetype2.4.6在iOS上各版本的编译脚本

基调:这是一篇水文,技术含量不大,主要是留给以后的自己回顾用的。但提到的某些编译错误的解决过程有一定代表性,难保谁搜索到这篇文章不能从中找到解决自己手中问题的方法,如果不小心帮到了谁,也算无心的好事吧...
  • madongchunqiu
  • madongchunqiu
  • 2013年10月07日 21:41
  • 8842

Oracle10g集群详细操作步骤

一、环境 主机: OS: Windows 7 64bit VirtualBox版本:VirtualBox 4.0.8r71778 增加一个Loopback网卡,配置IP为: 10.10.100...
  • cyz0815
  • cyz0815
  • 2014年03月31日 17:21
  • 1154

J2EE开发技术简介之JBOSS

1.JBoss简介 JBoss是一个运行EJB的 J2EE应用服务器。它是开放源代码的项目,遵循最新的J2EE规范。从JBoss项目开始至今,它已经从一个EJB容器发展成为一个基于的J2EE的一...
  • xxq_2011
  • xxq_2011
  • 2015年03月31日 11:37
  • 351

MyEclipse 优化配置以及Building Workspace时间太长的问题解决

刚毕业入职,对Java的概念还不是很有把握。平时在学校学习也都是一些小的项目,复杂度也不高,配置什么的也比较简单。但进入公司工作后,感觉写代码还是好说,但是每当一个工程从SVN检出后,总需要花一天半天...
  • qq_26651077
  • qq_26651077
  • 2017年08月01日 20:01
  • 640

eclipse building workspace太慢或者 js出错问题解决

1.js文件错误解决办法   右键项目->properties->Builders(注:JavaScript Validator也会引起 build workspace太慢)   ...
  • crazzy0727
  • crazzy0727
  • 2016年11月03日 14:42
  • 3608

JBoss 目录结构

安装JBoss 会创建下列目录结构: 目录 描述  bin 启动和关闭JBoss 的脚本  client 客户端与JBoss 通信所需的Java 库(JARs)  docs 配置的样本文件(数据库配...
  • qiaqia609
  • qiaqia609
  • 2013年11月21日 11:28
  • 9101

Apache服务器和tomcat服务器和jboss服务器的区别

一概述:         Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。   A...
  • u013126379
  • u013126379
  • 2016年10月29日 22:17
  • 2041

jboss 目录详解及web部署

1、JBoss介绍 JBoss完全实现了J2EE的服务栈: EJB (Enterprise JavaBeans) JMS (Java Message Service) JTS/JTA (Jav...
  • wuxiuguo1
  • wuxiuguo1
  • 2014年01月15日 15:42
  • 3400

JBOSS集成的ActiveMQ处理消息

上篇博客对JBOSS和ActiveMQ集成做了一下实现,这篇用一个DEMO来看如何来用两者集成之后的ActiveMQ来处理消息。 这个DEMO是用STRUTS2+Spring3+MDB实现的,主要的功...
  • lfsf802
  • lfsf802
  • 2013年05月15日 09:50
  • 3269
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Tutorial for building J2EE Applications using JBOSS and ECLIPSE-6
举报原因:
原因补充:

(最多只允许输入30个字)