现就一个银行账户实体Bean:AccountBean的编写规则来说起。
AccountEJB由三个文件组成:
l 实体Bean类(AccountBean)
l Home接口(AccountHome)
l Remote接口(Account)
步骤如下:
首先实现EntityBean接口:public class AccountBean implements EntityBean{. . .}
一、a.声明实体环境上下文:protected EntityContext ctx;
b.声明Bean-管理 实体状态域
private String accountID;// 注:主键不一定要与数据库中主键字段同名
private String ownerName;
private double balance;
c.创建一个构造方法public AccountBean() {. . .}
二、编写商业逻辑方法
public void deposit(double amt) throwsAccountException { . . .}//存款
public void withdraw(double amt) throws AccountException {. . .}//取款
三、编写实体状态域的getter/setter方法
public double getBalance() {. . .}
public void setOwnerName(String name) {. . .}
public String getOwnerName() {. . .}
public String getAccountID() {. . .}
public void setAccountID(String id) {. . .}
四、编写主接口商业逻辑方法(方法以"ejbHome"开头),它独立于任何一个具体账户实例
public double ejbHomeGetTotalBankValue() throws AccountException {. . . }
五、实现EntityBean接口中的方法(这些方法被容器调用)
public void ejbActivate() {...}
public void ejbRemove() throws RemoveException {...}
public void ejbPassivate() {...}
public void ejbLoad() {...}
public void ejbStore() {...}
public void setEntityContext(EntityContext ctx) {...}
public void unsetEntityContext() {...}
六、编写创建(必须至少一对ejbCreate、ejbPostCreate方法)和查找方法
public void ejbPostCreate(String accountID,String ownerName) {...}
public AccountPK ejbCreate(String accountID, String ownerName) throws CreateException{...}
public AccountPK ejbFindByPrimaryKey(AccountPK key) throws FinderException {...}
public Collection ejbFindByOwnerName(String name) throws FinderException {...}
七、定义从连接池获取jdbc数据库连接的方法
public Connection getConnection() throwsException{. . .}
-------------------------------------------------------------------------------------------------------------------------------------
现在来编写远程商业接口Account,它继承了EJBObject接口:public interface Account extends EJBObject {. . .}
一、上面AccountBean中的商业逻辑的接口
public void deposit(double amt) throws AccountException, RemoteException;
public void withdraw(double amt) throws AccountException, RemoteException;
二、上面AccountBean中的getter/setter方法的接口
public double getBalance() throws RemoteException;
public String getOwnerName() throws RemoteException;
public void setOwnerName(String name) throws RemoteException;
public String getAccountID() throws RemoteException;
public void setAccountID(String id) throws RemoteException;
-------------------------------------------------------------------------------------------------------------------------------------
现在来编写主接口AccountHome,它继承了EJBHome接口:public interface AccountHome extends EJBHome {. . .}
一、编写create()方法与AccountBean中ejbCreate()方法对应,返回类型为远程商业接口类型Account(账户)
public Account create(String accountID, String ownerName) throws CreateException, RemoteException;
二、编写查找方法
a.通过主键查找账户Account,与AccountBean中ejbFindByPrimaryKey()方法对应:
public Account findByPrimaryKey(AccountPK key) throws FinderException, RemoteException;
b.通过ownerName(账户拥有者)查找账户,与AccountBean中ejbFindByOwnerName()方法对应:
public Collection findByOwnerName(String name) throws FinderException, RemoteException;
三、编写独立的主接口商业方法,它独立于任何一个具体的账户,与AccountBean中ejbHome