中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > JavaBeans
EJB设计模式2
作者:xxcc 时间:2001-11-17 11:08 出处:互联网 责编:chinaitpower
              摘要:EJB设计模式2

 

设计模式2
为了避免设计模式1的缺点,我们介绍一下封装
entity bean值域的value objec的概念。value object,
用某些语言的术语来说,就是一个结构类型,因为他们
和corba的结构类型非常类似。


Value Object code snippet for Company
public class CompanyStruct implements
java.io.Serializable {
public Integer comId; //Primary Key
public String comName;
public String comDescription;
public java.sql.Timestamp mutationDate;
}

Value Object code snippet for Employee
public class EmployeeStruct implements
java.io.Serializable {
public Integer empId; //Primary Key
public Integer comId; //Foreign Key
public String empFirstName;
public String empLastName;
public java.sql.Timestamp mutationDate;
}

现在,公司和雇员的entity bean可以把上面的一个结构类型作为
ejbCreate()的一个参数。由于这个结构封装了entity的所有字段
的值,entity bean只需要一个getdata()和setdata()方法就可以
对所有的字段进行操作。


Code snippet for an Entity Bean’s create()
public Integer ejbCreate(CompanyStruct struct) throws
CreateException {
this.comId = struct.comId;
this.comName = struct.comName;
this.comDescription = struct.comDescription;
this.mutationDate = struct.mutationDate;
return null;
}
Code snippet for an Entity Bean’s getData()
public CompanyStruct getData() {
CompanyStruct result = new CompanyStruct();
result.comId = this.comId;
result.comName = this.comName;
result.comDescription = this.comDescription;
result.mutationDate = this.mutationDate;
return result;
}
Code snippet for an Entity Bean’s setData()
public void setData(CompanyStruct struct) {
this.comName = struct.comName;
this.comDescription = struct.comDescription;
this.mutationDate = struct.mutationDate;;
}


跟设计模式1中使用单独的get()和set()方法去操作特定字段不同,
在设计模式2中,我们避免这种情况而只需要进行一次远程调用就
可以了。现在,只有一个事务通过一次远程调用就操作了所有的数
据。这样,我们就避免了设计模式1的大部分缺点,除了建立bean
之间的关系外。
虽然setdata()方法可以对所有字段赋值,但是,borland appserver
提供了一种智能更新的特性,只有被修改过的字段才会被重新写入数
据库,如果没有字段被修改,那么ejbStore()方法将会被跳过。
borland程序员开发指南(EJB)有更详细的描述。
同样,在entity bean和struct之间存在这重复的代码,比如同
样的字段声明。这意味着任何数据库表结构的修改都会导致
entity beabn和struct的改变,这使得同步entity和struct变得
困难起来。
一个小小的改进可以从一定程度上避免这种情况,
就是在ebCreate()方法中调用setddata()方法,这可以消除一
些冗余的代码。


Code snippet for an Entity Bean’s create()
public Integer ejbCreate(CompanyStruct struct) throws
CreateException {
this.comId = struct.comId; //set the primary key
setData(struct);//this removes some redundant code
return null;
}
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有