Creating the session beanIn the J2EE Hierarchy view, select New -> Enterprise Bean from the ItsoProGuideEJB context menu.. Figure 12-40 Creating a new session bean page 1 Select the Sess
Trang 1Figure 12-37 Object-relational mapping editor The Outline view of the mapping editor summarizes our mapping activities (Figure 12-38)
Trang 2Figure 12-38 Outline view of the mapping editor Save the mapping (Ctrl-s) and close the editor
Implementing the session facade
The last EJB that we have to build is the facade: the BankEJB stateless session bean (Figure 12-39)
Figure 12-39 Business model facade
m:m
1:m
TransRecord
Customer
Account Facade
Business Model
BankEJB
Trang 3Creating the session bean
In the J2EE Hierarchy view, select New -> Enterprise Bean from the ItsoProGuideEJB context menu Check the project name and click Next to advance to the wizard’s first page (Figure 12-40)
Figure 12-40 Creating a new session bean (page 1) Select the Session bean option and type BankEJB in the Bean name field Type itso.ejb.model.facade in the Default package field and click Next to continue (Figure 12-41):
Make sure that you have the correct selections Stateless and Container
Note that this time around, Application Developer suggests that you create a remote client view instead of a local client view This is because the
environment knows that session beans are normally used to implement the model’s facade and, as such, need remote interfaces as opposed to local ones
The next wizard’s page is just like the one shown in Figure 12-14 on page 394 and need not be altered as well
You may as well click Finish to complete the creation of your session bean
Trang 4Figure 12-41 Creating a new session bean (page 2)
If you open the J2EE Hierarchy view, you will be able to see the newly created session bean (Figure 12-42)
Trang 5Note that for session beans, just three types are generated by the wizard: the remote home interface, the remote component interface, and the bean class There is no need to have a key class because session beans are not persistent
Creating an EJB reference
An EJB reference is a logical (alternative) name used to locate the home interface of an enterprise bean used by an application Using a logical name instead of the complete JNDI name allows applications to use hard coded strings
to reference other beans and still be easy to maintain
The link between the logical name and the real bean is defined in the EJB deployment descriptor and does not involve changing the code At deployment, the EJB reference is bound to the enterprise bean's home in the target
operational environment The container makes the application's EJB references available in a JNDI naming context
In Application Developer, EJB references are edited using the EJB deployment descriptor editor (Figure 12-43)
Figure 12-43 EJB references
As you can see, some references have already been defined for you Application Developer automatically does that when you create relationships among entity
logical name
Trang 6Select the BankEJB bean and click Add A dialog that lets you select the kind of reference to add is displayed (Figure 12-44)
Figure 12-44 Reference types
EJBs may reference other EJBs either through a remote or local interface They may also reference resources and security roles Since our entity beans are all local to the session bean, select EJB local reference and click Next to continue
The next dialog (Figure 12-46) lets you specify the information of the referenced local EJB:
You do not have to fill in all the fields, because Application Developer knows all the information if only you let it know which EJB you would like to
reference
Locate the Link field, but instead of typing the information, click Browse to open the Link Selection dialog
Select the Customer bean from the drop-down combo box and click OK
Click Finish to create the reference
Trang 7Figure 12-45 Adding an EJB local reference Repeat the same process for the Account and the TransRecord bean When you are done, the References page of the EJB deployment descriptor should look like Figure 12-46
Trang 8Editing the session bean
To be compatible with our existing control and presentation layers, the improved model layer must expose the very same set of operations that were made public
by the previous JavaBean implementation These operations are as follows:
public BigDecimal deposit(String accountID, BigDecimal amount) throws AccountDoesNotExistException, CreateException
public BigDecimal withdraw(String accountID, BigDecimal amount) throws InsufficientFundsException, AccountDoesNotExistException, CreateException
public BigDecimal transfer(String accountID1, String accountID2, BigDecimal amount) throws InsufficientFundsException,
AccountDoesNotExistException, CreateException
public Customer getCustomer(String customerID) throws CustomerDoesNotExistException
public Account getAccount(String accountID) throws AccountDoesNotExistException
public Account[] getAccounts(String customerID) throws CustomerDoesNotExistException
public TransRecord[] getTransactions(String accountID) throws AccountDoesNotExistException
Caching the initial context and the EJB homes
Open the BankEJBBean class with a Java editor To implement the methods listed above, we will make use of some private helper methods and properties
Declare the following properties inside the class definition body:
private Context context;
private AccountLocalHome accountHome;
private CustomerLocalHome customerHome;
private TransRecordLocalHome transRecordHome;
They will hold, respectively, cached references to the initial naming context and the homes of the entity beans
Errors may be reported because of missing import statements Correct them by selecting Source -> Organize Imports When asked to inform the complete name
Note: The EJB methods can throw additional exceptions, for example,
CreateException is thrown if the create of a TransRecord fails
Trang 9Now make Application Developer generate getter methods for the three properties Select Source -> Generate Getter and Setter and then select only the getter methods for all three properties
Change all three methods from public to private, and then perform changes to the code as shown in Figure 12-47
Figure 12-47 BankEJB bean getter methods All three properties will be lazy-initialized when the getters are called for the first time Add the appropriate imports to the javax.naming.InitialContext and javax.naming.NamingException types
Now enter the remaining three helper methods (Figure 12-48)
private Context getContext() throws NamingException {
if (context == null) context = new InitialContext();
return context;
} private AccountLocalHome getAccountHome() {
if (accountHome == null) accountHome = (AccountLocalHome)lookup("ejb/Account"));
return accountHome;
} private CustomerLocalHome getCustomerHome() {
if (customerHome == null) customerHome = (CustomerLocalHome)lookup("ejb/Customer");
return customerHome;
} private TransRecordLocalHome getTransRecordLocalHome() {
if (transRecordHome == null) transRecordHome = (TransRecordLocalHome)lookup("ejb/TransRecord");
return transRecordHome;
}
Note: The get methods to retrieve the local homes of the entity beans invoke
the lookup helper method with the name of the EJB reference that we defined for the session bean, for example, ejb/Account
Trang 10Figure 12-48 Beanking bean private helper methods
Adding business methods
To complete the facade, we have to enter its public methods as shown in Example 12-1
Example 12-1 Business methods of BankEJB session bean
public BigDecimal deposit(String accountID, BigDecimal amount)
throws AccountDoesNotExistException, CreateException {
AccountLocal account = getAccountLocal(accountID);
account.deposit(amount);
getTransRecordHome().create("C", amount, account);
return account.getBalance();
}
public BigDecimal withdraw(String accountID, BigDecimal amount)
throws InsufficientFundsException, AccountDoesNotExistException,
private Object lookup(String referenceName) {
try { return getContext().lookup("java:comp/env/" + referenceName); } catch (NamingException e) { throw new EJBException(e); }
}
private CustomerLocal getCustomerLocal(String customerID)
throws CustomerDoesNotExistException { try { return getCustomerHome().findByPrimaryKey
(new CustomerKey(Integer.valueOf(customerID).intValue()));
} catch (FinderException e) { throw new CustomerDoesNotExistException(); }
}
private AccountLocal getAccountLocal(String accountID)
throws AccountDoesNotExistException { try { return getAccountHome().findByPrimaryKey(accountID);
} catch (FinderException e) { throw new AccountDoesNotExistException(); }
}
Notes:
The lookup method uses the context with the java:comp/env/ejbreference argument to retrieve a home object
We do not require a getTransRecordLocal method Transaction records are only created (using the home) or retrieved from an account through the relationship