PlayerBean (public abstract)LocalPlayerHomeLocalPlayerEntityBean 介面ejbCreate/ejbPostCreateget/set 抽象方法get/set 抽象方法finalize()
public abstract String getPlayerId(); public abstract void setPlayerId(String id);
public abstract String getName(); public abstract void setName(String name);
public abstract String getPosition(); public abstract void setPosition(String position);
public abstract double getSalary(); public abstract void setSalary(double salary);
命名慣例:get/set + 欄位名 (首字大寫)。容器自動管理儲存/同步。
public abstract Collection getTeams();
public abstract void setTeams(Collection teams);
public abstract Collection ejbSelectLeagues(LocalPlayer player) throws FinderException;
public abstract Collection ejbSelectSports(LocalPlayer player) throws FinderException;
public Collection getLeagues() throws FinderException {
LocalPlayer player = (LocalPlayer) context.getEJBLocalObject();
return ejbSelectLeagues(player);
}
public Collection getSports() throws FinderException {
LocalPlayer player = (LocalPlayer) context.getEJBLocalObject();
return ejbSelectSports(player);
}
public String ejbCreate(String id, String name, String position, double salary) throws CreateException {
setPlayerId(id); setName(name); setPosition(position); setSalary(salary);
return null; // CMP 必須回傳 null
}
public void ejbPostCreate(String id, String name, String position, double salary) throws CreateException {
// 建立關聯關係程式碼放這裡
}
ejbRemove 通常為空,容器自動刪除資料庫記錄。ejbLoad/ejbStore 由容器自動管理。
public interface LocalPlayerHome extends EJBLocalHome {
LocalPlayer create(String id, String name, String position, double salary) throws CreateException;
LocalPlayer findByPrimaryKey(String id) throws FinderException;
Collection findByPosition(String position) throws FinderException;
Collection findByLeague(LocalLeague league) throws FinderException;
}
public interface LocalPlayer extends EJBLocalObject {
String getPlayerId(); String getName(); String getPosition(); double getSalary();
Collection getTeams();
Collection getLeagues() throws FinderException;
Collection getSports() throws FinderException;
// set 方法不應暴露於 Local Interface
}
後續將介紹 TeamEJB、LeagueEJB 實作、部署描述符關聯定義、EJB QL 查詢語句撰寫。