⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ejb设计模式2.htm

📁 写给JSP初级程序员的书
💻 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&nbsp;bean值域的value&nbsp;objec的概念。value&nbsp;object,<BR>用某些语言的术语来说,就是一个结构类型,因为他们<BR>和corba的结构类型非常类似。<BR><BR><BR>Value&nbsp;Object&nbsp;code&nbsp;snippet&nbsp;for&nbsp;Company<BR>public&nbsp;class&nbsp;CompanyStruct&nbsp;implements<BR>java.io.Serializable&nbsp;{<BR>public&nbsp;Integer&nbsp;comId;&nbsp;//Primary&nbsp;Key<BR>public&nbsp;String&nbsp;comName;<BR>public&nbsp;String&nbsp;comDescription;<BR>public&nbsp;java.sql.Timestamp&nbsp;mutationDate;<BR>}<BR><BR>Value&nbsp;Object&nbsp;code&nbsp;snippet&nbsp;for&nbsp;Employee<BR>public&nbsp;class&nbsp;EmployeeStruct&nbsp;implements<BR>java.io.Serializable&nbsp;{<BR>public&nbsp;Integer&nbsp;empId;&nbsp;//Primary&nbsp;Key<BR>public&nbsp;Integer&nbsp;comId;&nbsp;//Foreign&nbsp;Key<BR>public&nbsp;String&nbsp;empFirstName;<BR>public&nbsp;String&nbsp;empLastName;<BR>public&nbsp;java.sql.Timestamp&nbsp;mutationDate;<BR>}<BR><BR>现在,公司和雇员的entity&nbsp;bean可以把上面的一个结构类型作为<BR>ejbCreate()的一个参数。由于这个结构封装了entity的所有字段<BR>的值,entity&nbsp;bean只需要一个getdata()和setdata()方法就可以<BR>对所有的字段进行操作。<BR><BR><BR>Code&nbsp;snippet&nbsp;for&nbsp;an&nbsp;Entity&nbsp;Bean’s&nbsp;create()<BR>public&nbsp;Integer&nbsp;ejbCreate(CompanyStruct&nbsp;struct)&nbsp;throws<BR>CreateException&nbsp;{<BR>this.comId&nbsp;=&nbsp;struct.comId;<BR>this.comName&nbsp;=&nbsp;struct.comName;<BR>this.comDescription&nbsp;=&nbsp;struct.comDescription;<BR>this.mutationDate&nbsp;=&nbsp;struct.mutationDate;<BR>return&nbsp;null;<BR>}<BR>Code&nbsp;snippet&nbsp;for&nbsp;an&nbsp;Entity&nbsp;Bean’s&nbsp;getData()<BR>public&nbsp;CompanyStruct&nbsp;getData()&nbsp;{<BR>CompanyStruct&nbsp;result&nbsp;=&nbsp;new&nbsp;CompanyStruct();<BR>result.comId&nbsp;=&nbsp;this.comId;<BR>result.comName&nbsp;=&nbsp;this.comName;<BR>result.comDescription&nbsp;=&nbsp;this.comDescription;<BR>result.mutationDate&nbsp;=&nbsp;this.mutationDate;<BR>return&nbsp;result;<BR>}<BR>Code&nbsp;snippet&nbsp;for&nbsp;an&nbsp;Entity&nbsp;Bean’s&nbsp;setData()<BR>public&nbsp;void&nbsp;setData(CompanyStruct&nbsp;struct)&nbsp;{<BR>this.comName&nbsp;=&nbsp;struct.comName;<BR>this.comDescription&nbsp;=&nbsp;struct.comDescription;<BR>this.mutationDate&nbsp;=&nbsp;struct.mutationDate;;<BR>}<BR><BR><BR>跟设计模式1中使用单独的get()和set()方法去操作特定字段不同,<BR>在设计模式2中,我们避免这种情况而只需要进行一次远程调用就<BR>可以了。现在,只有一个事务通过一次远程调用就操作了所有的数<BR>据。这样,我们就避免了设计模式1的大部分缺点,除了建立bean<BR>之间的关系外。<BR>虽然setdata()方法可以对所有字段赋值,但是,borland&nbsp;appserver<BR>提供了一种智能更新的特性,只有被修改过的字段才会被重新写入数<BR>据库,如果没有字段被修改,那么ejbStore()方法将会被跳过。<BR>borland程序员开发指南(EJB)有更详细的描述。<BR>同样,在entity&nbsp;bean和struct之间存在这重复的代码,比如同<BR>样的字段声明。这意味着任何数据库表结构的修改都会导致<BR>entity&nbsp;beabn和struct的改变,这使得同步entity和struct变得<BR>困难起来。<BR>一个小小的改进可以从一定程度上避免这种情况,<BR>就是在ebCreate()方法中调用setddata()方法,这可以消除一<BR>些冗余的代码。<BR><BR><BR>Code&nbsp;snippet&nbsp;for&nbsp;an&nbsp;Entity&nbsp;Bean’s&nbsp;create()<BR>public&nbsp;Integer&nbsp;ejbCreate(CompanyStruct&nbsp;struct)&nbsp;throws<BR>CreateException&nbsp;{<BR>this.comId&nbsp;=&nbsp;struct.comId;&nbsp;//set&nbsp;the&nbsp;primary&nbsp;key<BR>setData(struct);//this&nbsp;removes&nbsp;some&nbsp;redundant&nbsp;code<BR>return&nbsp;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 + -