Enterprise Beans
Let's take a closer look at the access paths between the clients, enterprise beans, and database tables. As you can see, the end-user clients (web and application clients) access only the session beans. Within the enterprise bean tier, the session beans use Java Persistence entities. On the back end of the application, the entities access the database tables that store the entity states.
Note: The source code for these enterprise beans is in the
<
INSTALL
>/javaeetutorial5/examples/dukesbank/src/com/sun/ebank/ejb/
directory.
Session Beans
The Duke's Bank application has three session beans:
AccountControllerBean
,CustomerControllerBean
, andTxControllerBean
. (Tx
stands for a business transaction, such as transferring funds.) These session beans provide a client's view of the application's business logic. Hidden from the clients are the server-side routines that implement the business logic, access databases, manage relationships, and perform error checking.AccountControllerBean
The business methods of the
AccountControllerBean
session bean perform tasks that fall into the following categories: creating and removing entities, managing the account-customer relationship, and getting the account information.The following methods create and remove entities:
These methods of the
AccountControllerBean
session bean call thecreate
andremove
methods of theAccount
entity. ThecreateAccount
andremoveAccount
methods throw application exceptions to indicate invalid method arguments. ThecreateAccount
method throws anIllegalAccountTypeException
if thetype
argument is neitherChecking
,Savings
,Credit
, norMoney Market
. ThecreateAccount
method also looks up the specified customer exists by invoking theEntityManager.find
method. If the result of this verification isnull
, thecreateAccount
method throws aCustomerNotFoundException
.The following methods manage the account-customer relationship:
The
Account
andCustomer
entities have a many-to-many relationship. A bank account can be jointly held by more than one customer, and a customer can have multiple accounts.In the Duke's Bank application, the
addCustomerToAccount
andremoveCustomerFromAccount
methods of theAccountControllerBean
session bean manage the account-customer relationship. TheaddCustomerToAccount
method, for example, starts by verifying that the customer exists. To create the relationship, theaddCustomerToAccount
method first looks up theCustomer
andAccount
entities using theEntityManager.find
method, then it calls theAccount.addCustomer
method to associate the customer with the account.The following methods get the account information:
The
AccountControllerBean
session bean has twoget
methods. ThegetAccountsOfCustomer
method returns all of the accounts of a given customer by invoking thegetAccounts
method of theAccount
entity. Instead of implementing aget
method for every instance variable, theAccountControllerBean
has agetDetails
method that returns an object (AccountDetails
) that encapsulates the entire state of anAccount
entity. Because it can invoke a single method to retrieve the entire state, the client avoids the overhead associated with multiple remote calls.CustomerControllerBean
A client creates a
Customer
entity by invoking thecreateCustomer
method of theCustomerControllerBean
session bean. To remove a customer, the client calls theremoveCustomer
method, which invokes theEntityManager.remove
method ofCustomer
.The
CustomerControllerBean
session bean has two methods that return multiple customers:getCustomersOfAccount
andgetCustomersOfLastName
.getCustomersOfAccount
calls thegetCustomers
method of theAccount
entity.getCustomersOfLastName
uses theCustomer.FindByLastName
named query to search the database for customers with a matching last name, which is a named parameter to the query.TxControllerBean
The
TxControllerBean
session bean handles bank transactions. In addition to itsget
methods,getTxsOfAccount
andgetDetails
, theTxControllerBean
bean has several methods that change the balances of the bank accounts:These methods access an
Account
entity to verify the account type and to set the new balance. Thewithdraw
anddeposit
methods are for standard accounts, whereas themakeCharge
andmakePayment
methods are for accounts that include a line of credit. If thetype
method argument does not match the account, these methods throw anIllegalAccountTypeException
. If a withdrawal were to result in a negative balance, thewithdraw
method throws anInsufficientFundsException
. If a credit charge attempts to exceed the account's credit line, themakeCharge
method throws anInsufficientCreditException
.The
transferFunds
method also checks the account type and new balance; if necessary, it throws the same exceptions as thewithdraw
andmakeCharge
methods. ThetransferFunds
method subtracts from the balance of oneAccount
instance and adds the same amount to another instance. Both of these steps must complete to ensure data integrity. If either step fails, the entire operation is rolled back and the balances remain unchanged. ThetransferFunds
method, like all methods in session beans that use container-managed transaction demarkation, has an implicitRequired
transaction attribute. That is, you don't need to explicitly decorate the method with a@TransactionAttribute
annotation.Java Persistence Entities
For each business entity represented in our simple bank, the Duke's Bank application has a matching Java Persistence API entity:
The purpose of these entities is to provide an object view of these database tables:
bank_account
,bank_customer
, andbank_tx
. For each column in a table, the corresponding entity has an instance variable. Because they use the Java Persistence API, the entities contain no SQL statements that access the tables. The enterprise bean container manages all data in the underlying data source, including adding, updating, and deleting data from the database tables.Unlike the session beans, the entities do not validate method parameters. During the design phase, we decided that the session beans would check the parameters and throw the application exceptions, such as
CustomerNotInAccountException
andIllegalAccountTypeException
. Consequently, if some other application were to include these entities, its session beans would also have to validate the method parameters. We could have added validation code to the entity's methods, but decided not to in order to keep the business logic separate from the entity data.Helper Classes
The EJB JAR files include several helper classes that are used by the enterprise beans. The source code for these classes is in the
<
INSTALL
>/javaeetutorial5/examples/dukesbank/dukesbank-ejb/src/java/com/sun/tutorial/javaee/dukesbank/util/
directory. Table 38-1 briefly describes the helper classes.
Database Tables
A database table of the Duke's Bank application can be categorized by its purpose: representing business entities.
Tables Representing Business Entities
Figure 38-2 shows the relationships between the database tables. The
bank_customer
andbank_account
tables have a many-to-many relationship: A customer can have several bank accounts, and each account can be owned by more than one customer. This many-to-many relationship is implemented by the cross-reference table namedbank_customer_account_xref
. Thebank_account
andbank_tx
tables have a one-to-many relationship: A bank account can have many transactions, but each transaction refers to a single account.
Figure 38-2 uses several abbreviations. PK stands for primary key, the value that uniquely identifies a row in a table. FK is an abbreviation for foreign key, which is the primary key of the related table. Tx is short for transaction, such as a deposit or withdrawal.
Protecting the Enterprise Beans
In the Java EE platform, you protect an enterprise bean by specifying the security roles that can access its methods. In the Duke's Bank application, you define two roles--
bankCustomer
andbankAdmin
--because two categories of operations are defined by the enterprise beans.A user in the
bankAdmin
role will be allowed to perform administrative functions: creating or removing an account, adding a customer to or removing a customer from an account, setting a credit line, and setting an initial balance. A user in thebankCustomer
role will be allowed to deposit, withdraw, and transfer funds, make charges and payments, and list the account's transactions. Notice that there is no overlap in functions that users in either role can perform.The system restricts access to these functions to the appropriate role by setting method permissions on selected methods of the
CustomerControllerBean
,AccountControllerBean
, andTxControllerBean
enterprise beans. For example, by allowing only users in thebankAdmin
role to access thecreateAccount
method in theAccountControllerBean
enterprise bean, you deny users in thebankCustomer
role (or any other role) permission to create bank accounts.