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

首頁 > 學院 > 開發設計 > 正文

EJB設計模式(2)

2019-11-18 14:44:22
字體:
來源:轉載
供稿:網友

  EJB設計模式(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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉峪关市| 太谷县| 长治县| 宾阳县| 天门市| 凭祥市| 宣恩县| 湘西| 宁强县| 杭州市| 平谷区| 南漳县| 天镇县| 孟连| 邯郸县| 镇远县| 开远市| 乐东| 石渠县| 鄂州市| 神农架林区| 湟源县| 裕民县| 绥德县| 永泰县| 西宁市| 黑龙江省| 历史| 措勤县| 安龙县| 平遥县| 阿城市| 桓台县| 巴塘县| 和田县| 福海县| 景德镇市| 若尔盖县| 沅陵县| 固原市| 新乡市|