Bean Managed Persistence   «Prev  Next»

Bean Managed Persistent Entity Bean - Exercise

Course project part 4:

Create the server side of the BankAccount bean-managed persistent entity bean
ObjectiveWrite and test the code for the server side of the bean-managed persistent BankAccount entity bean.

Background/overview

You will write the code for the BankAccountHome interface and the BankAccountBean from the skeletons provided.
You will deploy this bean in the J2EE server and then test it with the BankCustomer and BankCustomer2 clients.
The results should be the same as the in the previous exercise.

Download files

From the exercise skeleton files available on the Resources page, download the following files from the EJB-part2/skeletons/project-4 directory into a directory named bmt-bank.
Make this directory current.
BankAccount.java
BankCustomer2.java
createTable.sql
BankAccountBean.java
NSFException.java
runClient.bat
BankAccountHome.java
compileClient.bat
runClient2.bat
BankCustomer.java
compileEJB.bat
runIJ.bat

Instructions

Edit the BankAccountHome.java skeleton file so that it has the methods to create a new BankAccount and to find that BankAccount using the findByPrimaryKey() method. Edit the BankAccountBean.java skeleton file so that it implements the interface:

public interface BankAccount extends EJBObject {
public double getBalance() throws RemoteException;
public void credit(double amount) throws RemoteException;
public void debit(double amount)    
throws RemoteException, NSFException;
public String getInfo() throws RemoteException;
}

Add the ejbCreate() method that will create the entity object, and the ejbFindByPrimaryKey() method that will find an entity object and initialize the bean instance. The JDBC code is supplied so that you do not have to be a JDBC expert to complete this exercise. The steps for building this application and deploying the bean are similar to the steps involved in the previous exercise.
They are repeated here for your convenience.
When building the application and the DD in the deploytool do the following:
  1. Name the application BankAccountApp when creating the application.
  2. In step 3 indicate that it is an entity bean.
  3. In step 4 indicate that it is bean-managed persistence and the primary key class is java.lang.String.
  4. In step 7, click Add. Replace the ResourceRefName with "jdbc/BankAccountDB" which is the name of the database used in this application. Do not change the other two fields that are part of the entry you are adding.
  5. In step 9, indicate that the server manages the transactions. Indicate "Required" for the credit() and debit() methods.
  6. In step 11, add NSFException.class to the DD.
  7. When deploying the bean, set the JNDI name for the BankAccountDB to "jdbc/Cloudscape". Cloudscape is a full function database that is provided as part of the J2EE reference implementation.
  8. Set the JNDI name of the bean to "BankAccount".
  9. Before running the client do the following:
    1. In a separate window start the Cloudscape database with the command "cloudscape -start".
    2. Create the table (or clean out an existing one) by running the provided batch file as follows: "runIJ createTable.sql." This will create the database. Re-run this command each time you re-run the application otherwise you will get a duplicate row error when you try to create the accounts.
    3. Run the client BankCustomer with the script file "runClient."
  10. Run the BankCustomer2.java client so that it finds the bank account created in BankCustomer.java using the findByPrimaryKey() method.

Hints

Remember to start the database before running this application. The command to do this is "cloudscape -start". Be sure to create the table before you start, and to re-run this command after every iteration. If you don't, the BankCustomer client will bomb. Don't forget to set the transaction controls to "required" for both the credit and the debit methods. If you don't the database will not be updated.
If you need to start from scratch, shutdown the deploytool and the J2EE implementation and then run the command "cleanup". This will remove all the temporary files used by the server or the deploytool.
If you have problems, don't forget to look at the messages displayed in the J2EE server and the deploytool windows for an indication of what is going wrong. These will contain messages from your bean, the container, the server, and so on.
Make sure the name of the primary key parameter in the findByPrimaryKey() method in the home interface matches the name in the ejbFindByPrimaryKey() method in the bean.
If you have problems, refer back to hints in previous exercises for clues.

Submitting your exercise

When you have completed the exercise, paste the code for the BankAccountBean.java, BankAccountHome.java, and BankAccount.java into the text box below. Click the Submit button to submit your exercise. This will ensure you get credit for your work.