chapter 6.
creating a cmp entity bean 【程序编程相关:Struts中tiles的使用】
【推荐阅读:spring+hibernate开发网站】
【扩展信息:java.net.URLClassLoa】
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.
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 :
create a cmp bean named items under package au.com.tusc.cmp.
implement the ejbcreate method, with the values of all attributes being passed as arguments and then assigned to the attributes using mutator (setter) methods.
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)"
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()"
add a business method to get item details with the signature:
public itemdata getitemdata()
add another business method to register delivery of items with the signature:
public void fillstock(java.lang.integer quantity)
add callback methods, required for getting/setting bean context for bean with signatures:
public void setentitycontext(entitycontext ctx)
public void unsetentitycontext()
deploy the item bean.
add a field to the storeaccess bean to store the item reference (obtained from jndi lookup):
private itemlocalhome itemlocalhome
in the ejbcreate method of the storeaccess bean store this reference in the itemlocalhome variable by invoking the getlocalhome static method in itemutil.
add a business method to storeaccess bean with the signature:
public itemdata getitemdata(string itemid)
add another business method to storeaccess bean with the signature:
public java.util.arraylist getoutofstockitems()
add another business method to storeaccess bean with the signature:
public java.util.arraylist getitembysupplier(string supplierid)
create a test client named sessioncmpclient under package au.com.tusc.client.
run your client and test the bean.
create items cmp entity bean :... 下一页