1. 创建数据库表XBONUS:
a. 启动wcs服务器;
b. IE打开:URL: http://localhost/webapp/wcs/admin/servlet/db.jsp;
c. 插入sql:
Derby/DB2: create table XBONUS (MEMBERID BIGINT NOT NULL, BONUSPOINT INTEGER NOT NULL, OPTCOUNTER SMALLINT NOT NULL,
constraint p_xbonus primary key (MEMBERID), constraint f_xbonus foreign key (MEMBERID) references users (users_id) on delete cascade);
Oracle: create table XBONUS (MEMBERID NUMBER NOT NULL,BONUSPOINT INTEGER NOT NULL, OPTCOUNTER SMALLINT NOT NULL,
constraint p_xbonus primary key (MEMBERID), constraint f_xbonus foreign key (MEMBERID references users (users_id) on delete cascade);
2. 创建BonusBean实体Bean:
a. 右键 Deployment Descriptor: WebSphereCommerceServerExtensionsData; 选择
- 选择 Entity bean with container-managed persistence (CMP) fields
- 在 Bean name 项, 输入 Bonus
- Source folder 项保持默认ejbModule
- 在Default package项, 输入 com.ibm.commerce.extension.objects
- CMP Version 选项选择1.x
- 点击 Next
b. 在Enterprise Bean Details窗口,为BONUS的MEMBERID and BONUSPOINT等列增加CMP属性
- 选择 default CMP attribute id:java.lang.Integer 并 点击 Remove
- 点击 Add
- 在 Name 项, 输入 memberId
- 在Type 项, 输入 java.lang.Long
- 勾选Key Field , 表示为Bean和数据表主键标识
- 点击 Apply
- 在Name 项, 输入 bonusPoint
- 在Type 项, 输入 java.lang.Integer
- 确保 Access with getter and setter methods 勾选,用于自动生成get、set方法
- 点击 Apply
- 在 Name 项, 输入 optCounter
- 在 Type 项, 输入 short
- 取消 Access with getter and setter metheos 选项
- 点击 Apply 并关闭CMP窗口
- 确保 Use the single key attribute type for the key class 选项不被选择(取消选中), 然后点击 Next
b. 在EJB Java Class Details窗口,增加Bonus实体Bean调用
- 选择bean superclass, 点击 Browse.
- 在 Type Selection 窗口, Select a class using: (any) 项, 输入 ECEntityBean 并点击OK.
- 点击 Add 指定远程接口.
- 在 Type Selection 窗口, Select an interface using: (any) 项, 输入 Protectable 并点击 OK 选择 com.ibm.commerce.security.Protectable.
- 点击 Next.
- 取消选中 Add bean to Class Diagram.
- 点击 Finish.
3. 为新建Bean设置隔离等级(isolation level):
4. 为Bean设置安全标识(security identity):
b. 在Security Identity (Method Level)栏目中点击Add
5. 为Bean方法设置安全作用(security role):
b. 在 Method Permissions 栏目中点击Add
6. 增删BonusBean参数和方法:
a. 打开 .java
b. 删除下面声明和方法(可在Outline view窗口操作)
删除参数:myEntityCtx
删除方法:getEntityContext()、setEntityContext(EntityContext)、unsetEntityContext()
c. 增加getMemberId方法
public java.lang.Long getMemberId() {
return memberId;
}
d. Outline view 窗口中右键 getMemberId 方法,选择
7. 为BonusHome接口增加FinderHelper方法:
a. 打开
8. 增加ejbCreate and ejbPostCreate方法,使用ejbCreate进行数据库插入操作:
a. 打开
方法,选择 ,添加到home interface
c. 增加ejbPostCreate(Long, Integer)方法
9. 增加getOwner and fulfills方法:
a. 打开
public java.lang.Long getOwner()
throws java.lang.Exception {
return getMemberId();
}
public boolean fulfills(Long member, String relationship)
throws java.lang.Exception {
if ("creator".equalsIgnoreCase(relationship))
{
return member.equals(getMemberId());
}
return false;
}
10. 创建数据库和表定义:
a. 右键WebSphereCommerceServerExtensionsData 选择
d. 在
右键Columns 选项卡新建对应列,如下:
,输入XBONUS, 在
f. 点击Finish
11. 关联XBONUS表和entity bean:
a. 右键 WebSphereCommerceServerExtensionsData EJB project 选择
12. 使用乐观锁:
a. Bean tab > Bonus bean
>b. 在 Concurrency Control 栏目中勾选 Enable optimistic locking
13. 在optcounter设置相关属性:
a. 在
窗口 Enterprise Beans 选项卡中选择 optcounter : short
c. 在 Properties 视图中,为 OptimisticPredicate 选择 true
14. 编辑BonusBean.java的方法调用乐观锁类:
a. 在 ejbLoad() 方法第一行加上 super.ejbLoad();
b. 在 ejbStore()方法第一行加上 super.ejbStore();
c. ejbCreate(Long, Integer)方法修改为:
public com.ibm.commerce.extension.objects.BonusKey ejbCreate( java.lang.Long memberId,java.lang.Integer bonusPoint) throws javax.ejb.CreateException {
this.initializeFields();
_initLinks();
this.memberId=memberId;
this.bonusPoint=bonusPoint;
BonusKey myNewKey = new BonusKey(memberId);
this.initializeOptCounter(myNewKey);
return null;
}
15. 创建access bean:
a. 右键 WebSphereCommerceServerExtensionsData project 选择 点击 Next
b. 选择 Copy helper 点击 Next
c. 选择 Bonus bean 点击 Next
d. 在 Constructor Method 选项, 选择 findByPrimaryKey(com.ibm.commerce.extension.objects.BonusKey) 点击 Finish.
e. 在 Overview 选项卡中下拉找到 JNDI - CMP Connection Factory Binding 栏目, JNDI name 输入下面名称
17. 更新BonusAccessBean:
a. 右键 WebSphereCommerceServerExtensionsData 选择
b. 在 Select access beans to regenerate 窗口,点击 Deselect All
c. 只选择 BonusAccessBean
d. 点击 Finish