📄 ejb设计模式2.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://eps.www85.cn4e.com/java/article/devshow.asp?id=124 -->
<HTML><HEAD><title>csdn_EJB设计模式2</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<STYLE type=text/css>TD {
FONT-FAMILY: "Verdana", "Arial", "宋体"; FONT-SIZE: 9pt
}
A {
COLOR: #660000; TEXT-DECORATION: underline
}
A:hover {
COLOR: #660000; TEXT-DECORATION: none
}
.line {
LINE-HEIGHT: 14pt
}
</STYLE>
<META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff text=#000000>
<table><tbody>
<TR>
<TD height=21>
<DIV align=center><B><FONT size=3>EJB设计模式2 <BR><FONT
size=2> </FONT></FONT></FONT>
<HR align=center color=#cccccc noShade SIZE=1>
</DIV></TD></TR>
<TR>
<TD class=line><FONT
color=#333300>设计模式2<BR>为了避免设计模式1的缺点,我们介绍一下封装<BR>entity bean值域的value objec的概念。value object,<BR>用某些语言的术语来说,就是一个结构类型,因为他们<BR>和corba的结构类型非常类似。<BR><BR><BR>Value Object code snippet for Company<BR>public class CompanyStruct implements<BR>java.io.Serializable {<BR>public Integer comId; //Primary Key<BR>public String comName;<BR>public String comDescription;<BR>public java.sql.Timestamp mutationDate;<BR>}<BR><BR>Value Object code snippet for Employee<BR>public class EmployeeStruct implements<BR>java.io.Serializable {<BR>public Integer empId; //Primary Key<BR>public Integer comId; //Foreign Key<BR>public String empFirstName;<BR>public String empLastName;<BR>public java.sql.Timestamp mutationDate;<BR>}<BR><BR>现在,公司和雇员的entity bean可以把上面的一个结构类型作为<BR>ejbCreate()的一个参数。由于这个结构封装了entity的所有字段<BR>的值,entity bean只需要一个getdata()和setdata()方法就可以<BR>对所有的字段进行操作。<BR><BR><BR>Code snippet for an Entity Bean’s create()<BR>public Integer ejbCreate(CompanyStruct struct) throws<BR>CreateException {<BR>this.comId = struct.comId;<BR>this.comName = struct.comName;<BR>this.comDescription = struct.comDescription;<BR>this.mutationDate = struct.mutationDate;<BR>return null;<BR>}<BR>Code snippet for an Entity Bean’s getData()<BR>public CompanyStruct getData() {<BR>CompanyStruct result = new CompanyStruct();<BR>result.comId = this.comId;<BR>result.comName = this.comName;<BR>result.comDescription = this.comDescription;<BR>result.mutationDate = this.mutationDate;<BR>return result;<BR>}<BR>Code snippet for an Entity Bean’s setData()<BR>public void setData(CompanyStruct struct) {<BR>this.comName = struct.comName;<BR>this.comDescription = struct.comDescription;<BR>this.mutationDate = struct.mutationDate;;<BR>}<BR><BR><BR>跟设计模式1中使用单独的get()和set()方法去操作特定字段不同,<BR>在设计模式2中,我们避免这种情况而只需要进行一次远程调用就<BR>可以了。现在,只有一个事务通过一次远程调用就操作了所有的数<BR>据。这样,我们就避免了设计模式1的大部分缺点,除了建立bean<BR>之间的关系外。<BR>虽然setdata()方法可以对所有字段赋值,但是,borland appserver<BR>提供了一种智能更新的特性,只有被修改过的字段才会被重新写入数<BR>据库,如果没有字段被修改,那么ejbStore()方法将会被跳过。<BR>borland程序员开发指南(EJB)有更详细的描述。<BR>同样,在entity bean和struct之间存在这重复的代码,比如同<BR>样的字段声明。这意味着任何数据库表结构的修改都会导致<BR>entity beabn和struct的改变,这使得同步entity和struct变得<BR>困难起来。<BR>一个小小的改进可以从一定程度上避免这种情况,<BR>就是在ebCreate()方法中调用setddata()方法,这可以消除一<BR>些冗余的代码。<BR><BR><BR>Code snippet for an Entity Bean’s create()<BR>public Integer ejbCreate(CompanyStruct struct) throws<BR>CreateException {<BR>this.comId = struct.comId; //set the primary key<BR>setData(struct);//this removes some redundant code<BR>return null;<BR>}<BR><BR><BR></FONT></TD></TR>
<TR>
<TD height=5>
<HR align=center color=#cccccc noShade SIZE=1>
</TD></TR></TBODY></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -