Developing EJB by WSAD
Prerequisite:
1. WSAD 402
2. DB2/UDB 7.2.1 or greater version
3. Has DB2 sample database created
Part One: Creat a new EJB Project
1. Perspective -> Open -> Other -> J2EE Perspective.
2. File -> New -> EJB Project
Project Name: AccountAndTransferEJBModule
EAR Project: AccountAndTransferEAR
Next -> Next -> Finish
Verify:
J2EE Perspective -> J2EE View
EJB Modules -> AccountAndTransferEJBModule
Enterprise Applications -> AccountAndTransferEAR
Part Two: Creating a CMP entity bean
1. Create a CMP
File -> New -> Enterprise Bean
Bean Name: Account
EJB Project: AccountAndTransferEJBModule
Source folder: ejbModule
Default package: com.ibm.wsad.spc
-> Next
Bean supertype: <none>
Bean class: com.ibm.wsad.spc.AccountBean
Home interface: com.ibm.wsad.spc.AccountHome
Remote interface: com.ibm.wsad.spc.Account
Key class: com.ibm.wsad.spc.AccountKey
CMP attributes:
1). accountId: long (key field)
2). type: int
3). balance: float
-> Next
import javax.ejb.*
-> Finish
Verify:
J2EE Perspective-> J2EE View
EJB Modules -> AccountAndTransferEJBModule -> Account
2. Create a custom exception
File -> New -> Other -> Java -> Java Class.
Folder: /AccountAndTransferEJBModule/ejbModule
Package: com.ibm.wsad.spc
Name: InsufficientFundsException
Superclass: java.lang.Exception
Check "Constructor from superclass"
-> Finish
3. Add two methods to AccountBean.java
/* adds the specified amount to the account balance and returns
* the new balance
*/
public float add(float amount) {
balance += amount;
return balance;
}
/* subtracts the specified amount from the account balance and
* returns the new balance
*/
public float subtract(float amount) throws InsufficientFundsException {
if (balance < amount) {
throw new InsufficientFundsException("Insufficient funds");
}
balance -= amount;
return balance;
}
4. Add these two methods to Remote Interface
Double Click J2EE Perspective -> J2EE View -> EJB Modules -> AccountAndTransferEJBModule -> Account -> AccountBean
Outline view
Right mouse click add(float) -> Promote to Remote Interface
Right mouse click substract(float) -> Promote to Remote Interface
Verify:
J2EE Perspective -> J2EE View -> EJB Modules -> AccountAndTransferEJBModule -> Account -> Account
/* adds the specified amount to the account balance and returns
* the new balance
*/
public float add(float amount) throws java.rmi.RemoteException;
/* subtracts the specified amount from the account balance and
* returns the new balance
*/
public float subtract(float amount) throws InsufficientFundsException, java.rmi.RemoteException;
5. Create Top Down EJB/RDB Mapping
J2EE Perspective -> Navigator View
Right-click AccountAndTransferEJBModule
Generate -> EJB to RDB maping
Select Top Down way to build EJB to RDB maping
&
[1] [2] [3] 下一页
(责任编辑:笑虎)