国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

EJB設計模式2

2019-11-18 12:36:30
字體:
供稿:網(wǎng)友

  為了避免設計模式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;
  }
  
  現(xiàn)在,公司和雇員的entity bean可以把上面的一個結構類型作為ejbCreate()的一個參數(shù)。由于這個結構封裝了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中,我們避免這種情況而只需要進行一次遠程調(diào)用就可以了。現(xiàn)在,只有一個事務通過一次遠程調(diào)用就操作了所有的數(shù)據(jù)。這樣,我們就避免了設計模式1的大部分缺點,除了建立bean之間的關系外。
  
  雖然setdata()方法可以對所有字段賦值,但是,borland appserver提供了一種智能更新的特性,只有被修改過的字段才會被重新寫入數(shù)據(jù)庫,假如沒有字段被修改,那么ejbStore()方法將會被跳過。borland程序員開發(fā)指南(EJB)有更具體的描述。
  
  同樣,在entity bean和struct之間存在這重復的代碼,比如同樣的字段聲明。這意味著任何數(shù)據(jù)庫表結構的修改都會導致entity beabn和struct的改變,這使得同步entity和struct變得困難起來。
  
  就是在ebCreate()方法中調(diào)用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;
  }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 高密市| 噶尔县| 金溪县| 广昌县| 山阳县| 安化县| 北安市| 城固县| 普兰店市| 浦北县| 桑植县| 天峨县| 兴业县| 广昌县| 长顺县| 剑川县| 五莲县| 北安市| 洪雅县| 门源| 汤阴县| 石柱| 东阳市| 桑植县| 青岛市| 连平县| 汕尾市| 巴东县| 灵山县| 廊坊市| 方山县| 南涧| 乌兰县| 弥勒县| 海阳市| 隆昌县| 武川县| 长武县| 镇原县| 宜兰县| 梅河口市|