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

来源:互联网 发布:php动态网页设计 编辑:程序博客网 时间:2024/06/08 16:07

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 列表选择Bonusbean,点击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 列表选择Bonusbean,点击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 


 

原创粉丝点击