读书人

BMP实业Bean编写规则详解

发布时间: 2012-12-20 09:53:21 作者: rapoo

BMP实体Bean编写规则详解

现就一个银行账户实体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中ejbHomeGetTotalBankValue()方法对应:

public double getTotalBankValue() throws AccountException, RemoteException;

-------------------------------------------------------------

现在编写账户Account的主键类AccountPK,它实现Serializable接口:public class AccountPK implements java.io.Serializable {

主键类编写的格式很简单固定,如下:
public String accountID;//主键

public AccountPK(String id) {
this.accountID = id;
}

public AccountPK() {
}

public String toString() {
return accountID;
}

public int hashCode() {
return accountID.hashCode();
}

public boolean equals(Object account) {
return ((AccountPK)account).accountID.equals(accountID);
}
}

-------------------------------------------------------------

编写异常类AccountException,它继承Exception:

public class AccountException extends Exception {
public AccountException() {super();}

public AccountException(Exception e) {super(e.toString());}

public AccountException(String s) {super(s);}

}

-------------------------------------------------------------

部署描述信息:

ejb-jar.xml:

<?xml version="1.0"?>

<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>

<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>Account</ejb-name>
<home>examples.AccountHome</home>
<remote>examples.Account</remote>
<ejb-class>examples.AccountBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>examples.AccountPK</prim-key-class>
<reentrant>False</reentrant>

</entity>
</enterprise-beans>
</ejb-jar>

jboss.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd">

<jboss>

<enterprise-beans>

<entity>
<ejb-name>Account</ejb-name>
<jndi-name>AccountHome</jndi-name>
</entity>
</enterprise-beans>

</jboss>


persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

<persistence-unit name="bbmmppPU" transaction-type="JTA">
<jta-data-source>java:/MySqlDS</jta-data-source>
</persistence-unit>

</persistence>

至此,整个BMP实体Bean的编写流程就结束了!

详细的代码见:Myeclipse8+jboss5+BMP实体Bean开发实例详解

读书人网 >编程

热点推荐