WCS学习笔记(第八天) - 创建entity bean实现

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; 选择New > Enterprise Bean

  1. 选择 Entity bean with container-managed persistence (CMP) fields
  2. 在 Bean name 项, 输入 Bonus
  3. Source folder 项保持默认ejbModule
  4. Default package项, 输入 com.ibm.commerce.extension.objects
  5. CMP Version 选项选择1.x
  6. 点击 Next

 

  b. 在Enterprise Bean Details窗口,为BONUS的MEMBERID and BONUSPOINT等列增加CMP属性

  1. 选择 default CMP attribute id:java.lang.Integer 并 点击 Remove
  2. 点击 Add
  3. 在 Name 项, 输入 memberId
  4. Type 项, 输入 java.lang.Long
  5. 勾选Key Field , 表示为Bean和数据表主键标识
  6. 点击 Apply
  7. Name 项, 输入 bonusPoint
  8. Type 项, 输入 java.lang.Integer
  9. 确保 Access with getter and setter methods 勾选,用于自动生成get、set方法
  10. 点击 Apply
  11. Name 项, 输入 optCounter
  12. Type 项, 输入 short
  13. 取消 Access with getter and setter metheos 选项 
  14. 点击 Apply 并关闭CMP窗口
  15. 确保 Use the single key attribute type for the key class 选项不被选择(取消选中), 然后点击 Next

 

  b. 在EJB Java Class Details窗口,增加Bonus实体Bean调用

  1. 选择bean superclass, 点击 Browse.
  2. 在 Type Selection 窗口, Select a class using: (any) 项, 输入 ECEntityBean 并点击OK.
  3. 点击 Add 指定远程接口.
  4. 在 Type Selection 窗口, Select an interface using: (any) 项, 输入 Protectable 并点击 OK 选择 com.ibm.commerce.security.Protectable.
  5. 点击 Next.
  6. 取消选中 Add bean to Class Diagram.
  7. 点击 Finish.

 

 

 

3. 为新建Bean设置隔离等级(isolation level):
  a. 打开 WebSphereCommerceServerExtensionsData > Deployment Descriptor > Entity Beans > Bonus , 点击Access 选项卡

  b. 在Isolation Level栏目中点击Add

  c. 在 Add Isolation Level 窗口,选择 Read committed ,点击Next

  d. 在 Beans found 列表选择 Bonus bean,点击Next

  e. 在 Methods found 列表选择 Bonus 及其所有*方法,点击Finish


 

4. 为Bean设置安全标识(security identity):
  a. 打开 WebSphereCommerceServerExtensionsData > Deployment Descriptor > Entity Beans > Bonus , 点击Access 选项卡

  b. 在Security Identity (Method Level)栏目中点击Add

  c. 选择 Use identity of EJB server ,点击Next ,(The Use identity of EJB server field ensures that all EJB beans run under the same identity for security)

  d. 在 Beans found 列表选择 Bonus bean,点击Next

  e. 在 Methods found 列表选择 Bonus 及其所有*方法,点击Finish

 

5. 为Bean方法设置安全作用(security role):
  a. 打开 WebSphereCommerceServerExtensionsData > Deployment Descriptor > Entity Beans > Bonus , 点击 Assembly 选项卡

  b. 在 Method Permissions 栏目中点击Add

  c. 选择 Security Roles  >  WCSecurityRole ,点击Next ,(This is the default security role for all EJB beans in WebSphere Commerce)

  d. 在 Beans found 列表选择 Bonus bean,点击Next

  e. 在 Methods found 列表点击 Apply to All 选择 Bonus 及其所有*方法,点击Finish

 

6. 增删BonusBean参数和方法:
  a. 打开 WebSphereCommerceServerExtensionsData > Entity Beans > Bonus > BonusBean  或者Ctrl+Shift+R搜索BonusBean.java

  b. 删除下面声明和方法(可在Outline view窗口操作)

         删除参数:myEntityCtx

         删除方法:getEntityContext()setEntityContext(EntityContext)unsetEntityContext()

  c.  增加getMemberId方法

public java.lang.Long getMemberId() {
   return memberId;
}

  d. Outline view 窗口中右键 getMemberId  方法,选择 Enterprise Bean > Promote to Remote Interface , 出现小R图标,表明该方法已被提升到远程接口

  e. 保存修改

 

7. 为BonusHome接口增加FinderHelper方法:
  a. 打开 WebSphereCommerceServerExtensionsData > Entity Beans > Bonus   , 在Beans pane, 选择 Bonus bean

  b. 点击 AddFinders text box , 如下:

 

  c. Name 项, 输入 findByMemberId

  d. 点击 Add 到 Parameters text box,在 Name 项, 输入 memberId , 在 Type 项, 输入 java.lang.Long , 点击OK 

 f.Return Type list, 选择 com.ibm.commerce.extension.objects.Bonus, 点击 Next

  g. Finder type 选项, 选择 WhereClauseFinderDescriptor

  h. Finder statement, 输入T1.MEMBERID = ?, 后点击 Finish 

 

 

8. 增加ejbCreate and ejbPostCreate方法,使用ejbCreate进行数据库插入操作:
  a. 打开 BonusBean.java

  b. 增加ejbCreate(Long, Integer)方法 ,并在Outline view右键ejbCreate(Long, Integer)方法,选择Enterprise Bean > Promote to Home Interface,添加到home interface

  c.  增加ejbPostCreate(Long, Integer)方法

 

9. 增加getOwner and fulfills方法:
  a. 打开 BonusBean.java

  b. 增加getOwner and fulfills方法

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 选择New > Other , 选择Data > Physical Data Model 并输入 MyDBSchema 名称

  b. 选择数据库:

        DB2/Derby : DB2 for Linux, UNIX, and Windows

        Oracle : Oracle

  c. 选择数据库版本:

       DB2/Derby : Version 9.5

        Oracle : Version 11

 

  d. 在WebSphereCommerceServerExtensionsData > Data Models > MyDBSchema.dbm 右键 Database > Add Data Object > Schemaname项输入NULLID

  e. 右键NULLID > Add Data Object > Table,输入XBONUS, 在 Columns 选项卡新建对应列,如下:

 

  f. 点击Finish

 

 

11. 关联XBONUS表和entity bean:

  a. 右键 WebSphereCommerceServerExtensionsData EJB project 选择 Java EE > EJB to RDB Mapping (1.x - 2.x) > Generate Map , 并窗口中 Specify the backend folder that will contain generated mapping file, database schema and other runtime information. Use an existing backend folder 保持默认值,点击Next

  b. 选择 Meet In The Middle 点击 Next

  c. 选择 None 点击 Finish

  d. 打开Map.mapxmi,右键 Bonus bean 选择 Match By Name

 

12. 使用乐观锁:

  a. WebSphereCommerceServerExtensionsData > Deployment Descriptor  >  Bean tab > Bonus bean

  b. 在 Concurrency Control 栏目中勾选 Enable optimistic locking

 

 

13. 在optcounter设置相关属性:

  a. 在WebSphereCommerceServerExtensionsData > Deployment Descriptor > Maps > Derby 10.0 > mall , 双击打开 mall: DERBY_V100

  b. 在 Overview 窗口 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 选择 New > Others... , 选择 EJB > Access Bean 点击 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 输入下面名称

 

16. 生成部署代码:

  a. 右键 WebSphereCommerceServerExtensionsData 选择 Java EE > Prepare for Deployment

 


17. 更新BonusAccessBean:

 a. 右键 WebSphereCommerceServerExtensionsData 选择 Java EE > Access Beans > Regenerate Access Beans 

 b. 在 Select access beans to regenerate 窗口,点击 Deselect All

 c. 只选择 BonusAccessBean

 d. 点击 Finish

 

参考: http://pic.dhe.ibm.com/infocenter/wchelp/v7r0m0/topic/com.ibm.commerce.developer.tutorial.doc/tutorial/ttd18.htm  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值