站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题J2EE设计模式:CMP到BMP模式(一)

J2EE设计模式:CMP到BMP模式(一)

摘要: J2EE设计模式:CMP到BMP模式(一)
google_ad_client = "pub-4944583547581781"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_channel ="1429059098"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_url = "000000"; google_color_text = "000000"; // EJB2.0终于给了我们一个可操作的容器管理持久(CMP)模型。只要有可能,在我的EJB项目中我就用CMP beans;但是,由于这件奇怪的事情,我需要把我的实体变成bean管理(BMP)的。我使用下面的模式让我干净利落的从CMP模型转换到bean管理持久模型。
在这篇文章中,我们将讨论:

1.CMP 2.0: 发生了什么变化?

2.Inventory(货物清单)EJB应用程序

3.开发一个CMP bean

4.移植CMP bean到BMP

CMP 2.0: 发生了什么变化?

当EJB问世的时候,CMP引起了很大的反响,当EJB2.0问世的时候,又对CMP模型进行了修订,它给了我们真正想要的特性:诸如关系和查询语言标准化。

与EJB1.1相比,当我们用EJB2.0写一个CMP bean时,我们的写法有很大的不同。我们创建抽象类,而不是创建被容器管理变量的公共域,我们像JavaBean一样创建抽象的属性(getters and setters)。这准许特定厂商的持久性管理器用他们自己的方式实现数据访问器(accessors)。

这将帮助他们(厂商)提出像这样的逻辑:

●由于他们不调用任何set方法,所以在ejbStore()中不做任何动作

●他们仅仅改变一个域,所以我们只在UPDATE查询语句中set那个域

●我们延迟装载一些数据,所以当用户用GET方法请求数据的时候我们才读取他

顺便提一个问题:为什么我们必须在抽象类中创建抽象方法?为什么持久性管理器不能在派生类中创建方法?

答:我们必须在抽象类中访问这些方法。例如:在ejbCreate()中,我们通过传入参数来SET它们。

Inventory EJB 应用程序

为了说明该实体模型,我们将看到一个简单的应用用该实体模型化该货物清单(Inventory)系统。我们应用程序包括以下组成部分:

Inventory实体Bean: 这是我们的焦点。他将映射到数据库表Inventory,该表纪录了条目的名字(主关键字),价格和仓库中条目的数目

价格无状态会话Bean:该Bean用Inventory实体Bean来得到条目的价格,它用本地(local)接口来访问该实体

价格客户: 这个命令行应用程序在会话(session)上运行该方法来测试所有工作都运行得很好

关键点是当我们把这个Inventory实体从 CMP移植到BMP实现时候,什么也不要做改变

让我们看一下用CMP实现Inventory实体

开发一个CMP bean

inventory CMP要我们创建一个抽象类(依照实体说明),XML描述文件告诉框架应该影射什么,图一说明了我们的条目看起来是什么样子



抽象实体bean

该抽象实体有下列属性:

实现了javax.ejb.EntityBean接口

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
abstract public class InventoryBean implements EntityBean
</td></tr></table>


实现在实体接口中声明的方法

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
public void setEntityContext(EntityContext context) {

   ctx = context;

}

public void unsetEntityContext() {

   ctx = null;

}

public void ejbActivate() {}

public void ejbPassivate() {}

public void ejbRemove() throws RemoveException {}

public void ejbStore() {}

public void ejbLoad() {}
</td></tr></table>


实现ejbCreate()和ejbPostCreate()方法该方法对应于HOME接口中的Create()方法(用抽象set方法set所有参数)

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
public String ejbCreate(String item, float price, int stock)

 throws CreateException {

      setItem(item);

      setPrice(price);

      setStock(stock);

      return null;

}
</td></tr></table>


public void ejbPostCreate(String item, float price, int stock) throws CreateException {}

实现抽象get和set方法

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
public abstract String getItem();

public abstract void setItem(String item);

public abstract float getPrice();

public abstract void setPrice(float price);

public abstract int getStock();

public abstract void setStock(int stock);
</td></tr></table>


实现一个助手方法来访问实体上下文(在后面的BMPbean中我们将用到它)

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
public EntityContext getEntityContext() {

   return ctx;

}
</td></tr></table>


EJB部署描述符

我们创建了一个CMP实体类,现在该创建部署描述符了.首先我们将创建一个标准的"ejb-jar.xml",然后我们需要配置与厂商有关的信息.我们将说明WebLogic 6.1 部署描述符,我们选一个.

标准 ejb-jar.xml

基本的实体配置信息:

我们将配置类名,用局部变量,在实体内部它们是局部变量.我们通过持久类型XML标签告诉容器该实体是CMP

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>
&lt;entity&gt;

  &lt;ejb-name&gt;InventoryBean&lt;/ejb-name&gt;

  &lt;local-home&gt;InventoryHome&lt;/local-home&gt;

  &lt;local&gt;Inventory&gt;/local&lt;

  &lt;ejb-class&gt;InventoryBean&lt;/ejb-class&gt;

  &lt;persistence-type&gt;Container&lt;/persistence-type&gt;

  &lt;prim-key-class&gt;java.lang.String&lt;/prim-key-class&gt;

  &lt;reentrant&gt;False&lt;/reentrant&gt;
</td></tr></table>


接着,我们将告诉容器那些域是容器管理的,item域是主键类[未完待续]

原文:http://www.onjava.com/pub/a/onjava/2002/04/10/j2eedesign.html

【关于作者】

本文由minpeng_2001翻译,minpeng_2001,j2ee的忠实fans.可以点击http://www.matrix.org.cn/user_view.asp?username=minpeng_2001查看他的信息.

↑返回目录
前一篇: J2EE设计模式:CMP到BMP模式(二)
后一篇: 开发基于JBoss的J2EE应用