The SavingsAccountBean Example
The entity bean illustrated in this section represents a simple bank account. The state of
SavingsAccountBeanis stored in thesavingsaccounttable of a relational database. Thesavingsaccounttable is created by the following SQL statement:CREATE TABLE savingsaccount (id VARCHAR(3) CONSTRAINT pk_savingsaccount PRIMARY KEY, firstname VARCHAR(24), lastname VARCHAR(24), balance NUMERIC(10,2));The
SavingsAccountBeanexample requires the following code:This example also uses the following classes:
The source code for this example is in this directory:
Entity Bean Class
The sample entity bean class is called
SavingsAccountBean. As you look through its code, note that it meets the requirements of any entity bean that uses bean-managed persistence. First, it implements the following:In addition, an entity bean class with bean-managed persistence has these requirements:
The EntityBean Interface
The
EntityBeaninterface extends theEnterpriseBeaninterface, which extends theSerializableinterface. TheEntityBeaninterface declares a number of methods, such asejbActivateandejbLoad, which you must implement in your entity bean class. These methods are discussed in later sections.The ejbCreate Method
When the client invokes a
createmethod, the EJB container invokes the correspondingejbCreatemethod. Typically, anejbCreatemethod in an entity bean performs the following tasks:The
ejbCreatemethod ofSavingsAccountBeaninserts the entity state into the database by invoking the privateinsertRowmethod, which issues the SQLINSERTstatement. Here is the source code for theejbCreatemethod:public String ejbCreate(String id, String firstName, String lastName, BigDecimal balance) throws CreateException { if (balance.signum() == -1) { throw new CreateException ("A negative initial balance is not allowed."); } try { insertRow(id, firstName, lastName, balance); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); } this.id = id; this.firstName = firstName; this.lastName = lastName; this.balance = balance; return id; }Although the
SavingsAccountBeanclass has only oneejbCreatemethod, an enterprise bean can contain multipleejbCreatemethods. For an example, see theCartBean.javasource code in this directory:When you write an
ejbCreatemethod for an entity bean, be sure to follow these rules:The
throwsclause can include thejavax.ejb.CreateExceptionand exceptions that are specific to your application. AnejbCreatemethod usually throws aCreateExceptionif an input parameter is invalid. If anejbCreatemethod cannot create an entity because another entity with the same primary key already exists, it should throw ajavax.ejb.DuplicateKeyException(a subclass ofCreateException). If a client receives aCreateExceptionor aDuplicateKeyException, it should assume that the entity was not created.The state of an entity bean can be directly inserted into the database by an application that is unknown to the Application Server. For example, an SQL script might insert a row into the
savingsaccounttable. Although the entity bean for this row was not created by anejbCreatemethod, the bean can be located by a client program.The ejbPostCreate Method
For each
ejbCreatemethod, you must write anejbPostCreatemethod in the entity bean class. The EJB container invokesejbPostCreateimmediately after it callsejbCreate. Unlike theejbCreatemethod, theejbPostCreatemethod can invoke thegetPrimaryKeyandgetEJBObjectmethods of theEntityContextinterface. For more information on thegetEJBObjectmethod, see the section Passing an Enterprise Bean's Object Reference. Often, yourejbPostCreatemethods will be empty.The signature of an
ejbPostCreatemethod must meet the following requirements:The
throwsclause can include thejavax.ejb.CreateExceptionand exceptions that are specific to your application.The ejbRemove Method
A client deletes an entity bean by invoking the
removemethod. This invocation causes the EJB container to call theejbRemovemethod, which deletes the entity state from the database. In theSavingsAccountBeanclass, theejbRemovemethod invokes a private method nameddeleteRow, which issues an SQLDELETEstatement. TheejbRemovemethod is short:public void ejbRemove() { try { deleteRow(id); catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } } }If the
ejbRemovemethod encounters a system problem, it should throw thejavax.ejb.EJBException. If it encounters an application error, it should throw ajavax.ejb.RemoveException. For a comparison of system and application exceptions, see the section deploytool Tips for Entity Beans with Bean-Managed Persistence.An entity bean can also be removed directly by a database deletion. For example, if an SQL script deletes a row that contains an entity bean state, then that entity bean is removed.
The ejbLoad and ejbStore Methods
If the EJB container needs to synchronize the instance variables of an entity bean with the corresponding values stored in a database, it invokes the
ejbLoadandejbStoremethods. TheejbLoadmethod refreshes the instance variables from the database, and theejbStoremethod writes the variables to the database. The client cannot callejbLoadandejbStore.If a business method is associated with a transaction, the container invokes
ejbLoadbefore the business method executes. Immediately after the business method executes, the container callsejbStore. Because the container invokesejbLoadandejbStore, you do not have to refresh and store the instance variables in your business methods. TheSavingsAccountBeanclass relies on the container to synchronize the instance variables with the database. Therefore, the business methods ofSavingsAccountBeanshould be associated with transactions.If the
ejbLoadandejbStoremethods cannot locate an entity in the underlying database, they should throw thejavax.ejb.NoSuchEntityException. This exception is a subclass ofEJBException. BecauseEJBExceptionis a subclass ofRuntimeException, you do not have to include it in thethrowsclause. WhenNoSuchEntityExceptionis thrown, the EJB container wraps it in aRemoteExceptionbefore returning it to the client.In the
SavingsAccountBeanclass,ejbLoadinvokes theloadRowmethod, which issues an SQLSELECTstatement and assigns the retrieved data to the instance variables. TheejbStoremethod calls thestoreRowmethod, which stores the instance variables in the database using an SQLUPDATEstatement. Here is the code for theejbLoadandejbStoremethods:public void ejbLoad() { try { loadRow(); } catch (Exception ex) { throw new EJBException("ejbLoad: " + ex.getMessage()); } } public void ejbStore() { try { storeRow(); } catch (Exception ex) { throw new EJBException("ejbStore: " + ex.getMessage()); } }The Finder Methods
The finder methods allow clients to locate entity beans. The
SavingsAccountClientprogram locates entity beans using three finder methods:SavingsAccount jones = home.findByPrimaryKey("836"); ... Collection c = home.findByLastName("Smith"); ... Collection c = home.findInRange(20.00, 99.00);For every finder method available to a client, the entity bean class must implement a corresponding method that begins with the prefix
ejbFind. TheSavingsAccountBeanclass, for example, implements theejbFindByLastNamemethod as follows:public Collection ejbFindByLastName(String lastName) throws FinderException { Collection result; try { result = selectByLastName(lastName); } catch (Exception ex) { throw new EJBException("ejbFindByLastName " + ex.getMessage()); } return result; }The finder methods that are specific to your application, such as
ejbFindByLastNameandejbFindInRange, are optional, but theejbFindByPrimaryKeymethod is required. As its name implies, theejbFindByPrimaryKeymethod accepts as an argument the primary key, which it uses to locate an entity bean. In theSavingsAccountBeanclass, the primary key is theidvariable. Here is the code for theejbFindByPrimaryKeymethod:public String ejbFindByPrimaryKey(String primaryKey) throws FinderException { boolean result; try { result = selectByPrimaryKey(primaryKey); } catch (Exception ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); } if (result) { return primaryKey; } else { throw new ObjectNotFoundException ("Row for id " + primaryKey + " not found."); } }The
ejbFindByPrimaryKeymethod may look strange to you, because it uses a primary key for both the method argument and the return value. However, remember that the client does not callejbFindByPrimaryKeydirectly. It is the EJB container that calls theejbFindByPrimaryKeymethod. The client invokes thefindByPrimaryKeymethod, which is defined in the home interface.The following list summarizes the rules for the finder methods that you implement in an entity bean class with bean-managed persistence:
- The
ejbFindByPrimaryKeymethod must be implemented.- A finder method name must start with the prefix
ejbFind.- The access control modifier must be
public.- The method modifier cannot be
finalorstatic.- The arguments and return type must be legal types for the Java RMI API. (This requirement applies only to methods defined in a remote--and not a local--home interface.)
- The return type must be the primary key or a collection of primary keys.
The
throwsclause can include thejavax.ejb.FinderExceptionand exceptions that are specific to your application. If a finder method returns a single primary key and the requested entity does not exist, the method should throw thejavax.ejb.ObjectNotFoundException(a subclass ofFinderException). If a finder method returns a collection of primary keys and it does not find any objects, it should return an empty collection.The Business Methods
The business methods contain the business logic that you want to encapsulate within the entity bean. Usually, the business methods do not access the database, and this allows you to separate the business logic from the database access code. The
SavingsAccountBeanclass contains the following business methods:public void debit(BigDecimal amount) throws InsufficientBalanceException { if (balance.compareTo(amount) == -1) { throw new InsufficientBalanceException(); } balance = balance.subtract(amount); } public void credit(BigDecimal amount) { balance = balance.add(amount); } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public BigDecimal getBalance() { return balance; }The
SavingsAccountClientprogram invokes the business methods as follows:BigDecimal zeroAmount = new BigDecimal("0.00"); SavingsAccount duke = home.create("123", "Duke", "Earl", zeroAmount); ... duke.credit(new BigDecimal("88.50")); duke.debit(new BigDecimal("20.25")); BigDecimal balance = duke.getBalance();The requirements for the signature of a business method are the same for session beans and entity beans:
- The method name must not conflict with a method name defined by the EJB architecture. For example, you cannot call a business method
ejbCreateorejbActivate.- The access control modifier must be
public.- The method modifier cannot be
finalorstatic.- The arguments and return types must be legal types for the Java RMI API. This requirement applies only to methods defined in a remote--and not a local--home interface.
The
throwsclause can include the exceptions that you define for your application. Thedebitmethod, for example, throws theInsufficientBalanceException. To indicate a system-level problem, a business method should throw thejavax.ejb.EJBException.The Home Methods
A home method contains the business logic that applies to all entity beans of a particular class. In contrast, the logic in a business method applies to a single entity bean, an instance with a unique identity. During a home method invocation, the instance has neither a unique identity nor a state that represents a business object. Consequently, a home method must not access the bean's persistence state (instance variables). (For container-managed persistence, a home method also must not access relationships.)
Typically, a home method locates a collection of bean instances and invokes business methods as it iterates through the collection. This approach is taken by the
ejbHomeChargeForLowBalancemethod of theSavingsAccountBeanclass. TheejbHomeChargeForLowBalancemethod applies a service charge to all savings accounts that have balances less than a specified amount. The method locates these accounts by invoking thefindInRangemethod. As it iterates through the collection ofSavingsAccountinstances, theejbHomeChargeForLowBalancemethod checks the balance and invokes thedebitbusiness method. Here is the source code of theejbHomeChargeForLowBalancemethod:public void ejbHomeChargeForLowBalance( BigDecimal minimumBalance, BigDecimal charge) throws InsufficientBalanceException { try { SavingsAccountHome home = (SavingsAccountHome)context.getEJBHome(); Collection c = home.findInRange(new BigDecimal("0.00"), minimumBalance.subtract(new BigDecimal("0.01"))); Iterator i = c.iterator(); while (i.hasNext()) { SavingsAccount account = (SavingsAccount)i.next(); if (account.getBalance().compareTo(charge) == 1) { account.debit(charge); } } } catch (Exception ex) { throw new EJBException("ejbHomeChargeForLowBalance: " + ex.getMessage()); } }The home interface defines a corresponding method named
chargeForLowBalance(see Home Method Definitions). Because the interface provides the client view, theSavingsAccountClientprogram invokes the home method as follows:SavingsAccountHome home; ... home.chargeForLowBalance(new BigDecimal("10.00"), new BigDecimal("1.00"));In the entity bean class, the implementation of a home method must adhere to these rules:
The
throwsclause can include exceptions that are specific to your application; it must not throw thejava.rmi.RemoteException.Database Calls
Table 26-1 summarizes the database access calls in the
SavingsAccountBeanclass. The business methods of theSavingsAccountBeanclass are absent from the preceding table because they do not access the database. Instead, these business methods update the instance variables, which are written to the database when the EJB container callsejbStore. Another developer might have chosen to access the database in the business methods of theSavingsAccountBeanclass. This choice is one of those design decisions that depend on the specific needs of your application.Before accessing a database, you must connect to it. For more information, see Chapter 31.
Home Interface
The home interface defines the
create, finder, and home methods. TheSavingsAccountHomeinterface follows:import java.util.Collection; import java.math.BigDecimal; import java.rmi.RemoteException; import javax.ejb.*; public interface SavingsAccountHome extends EJBHome { public SavingsAccount create(String id, String firstName, String lastName, BigDecimal balance) throws RemoteException, CreateException; public SavingsAccount findByPrimaryKey(String id) throws FinderException, RemoteException; public Collection findByLastName(String lastName) throws FinderException, RemoteException; public Collection findInRange(BigDecimal low, BigDecimal high) throws FinderException, RemoteException; public void chargeForLowBalance(BigDecimal minimumBalance, BigDecimal charge) throws InsufficientBalanceException, RemoteException; }create Method Definitions
Each
createmethod in the home interface must conform to the following requirements:
- It must have the same number and types of arguments as its matching
ejbCreatemethod in the enterprise bean class.- It must return the remote interface type of the enterprise bean.
- The
throwsclause must include the exceptions specified by thethrowsclause of the correspondingejbCreateandejbPostCreatemethods.- The
throwsclause must include thejavax.ejb.CreateException.- If the method is defined in a remote--and not a local--home interface, then the
throwsclause must include thejava.rmi.RemoteException.Finder Method Definitions
Every finder method in the home interface corresponds to a finder method in the entity bean class. The name of a finder method in the home interface begins with
find, whereas the corresponding name in the entity bean class begins withejbFind. For example, theSavingsAccountHomeclass defines thefindByLastNamemethod, and theSavingsAccountBeanclass implements theejbFindByLastNamemethod. The rules for defining the signatures of the finder methods of a home interface follow.
- The number and types of arguments must match those of the corresponding method in the entity bean class.
- The return type must be the entity bean's remote interface type or a collection of those types.
- The exceptions in the
throwsclause must include those of the corresponding method in the entity bean class.- The
throwsclause must contain thejavax.ejb.FinderException.- If the method is defined in a remote--and not a local--home interface, then the
throwsclause must include thejava.rmi.RemoteException.Home Method Definitions
Each home method definition in the home interface corresponds to a method in the entity bean class. In the home interface, the method name is arbitrary, provided that it does not begin with
createorfind. In the bean class, the matching method name begins withejbHome. For example, in theSavingsAccountBeanclass the name isejbHomeChargeForLowBalance, but in theSavingsAccountinterface the name is
HomechargeForLowBalance.The home method signature must follow the same rules specified for finder methods in the preceding section (except that a home method does not throw a
FinderException).Remote Interface
The remote interface extends
javax.ejb.EJBObjectand defines the business methods that a remote client can invoke. Here is theSavingsAccountremote interface:import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.math.BigDecimal; public interface SavingsAccount extends EJBObject { public void debit(BigDecimal amount) throws InsufficientBalanceException, RemoteException; public void credit(BigDecimal amount) throws RemoteException; public String getFirstName() throws RemoteException; public String getLastName() throws RemoteException; public BigDecimal getBalance() throws RemoteException; }The requirements for the method definitions in a remote interface are the same for session beans and entity beans:
- Each method in the remote interface must match a method in the enterprise bean class.
- The signatures of the methods in the remote interface must be identical to the signatures of the corresponding methods in the enterprise bean class.
- The arguments and return values must be valid RMI types.
- The
throwsclause must includejava.rmi.RemoteException.A local interface has the same requirements, with the following exceptions:
Running the SavingsAccountBean Example
Before you run this example, you must define the data source, create the database, and deploy the
SavingsAccountApp.earfile.Defining the Data Source
Follow the instructions in Creating a Data Source. This data source is a factory for database connections. For more information, see DataSource Objects and Connection Pools.
Creating the Database Table
The instructions that follow explain how to use the
SavingsAccountBeanexample with Derby, the database software that is included in the Application Server bundle.
Note: Application Server 8.2 includes a copy of the open source Derby database server. Application Server 8.0/8.1 includes the PointBase database server. If you are using Application Server 8.0/8.1, either follow the instructions in the J2EE Tutorial at
http://java.sun.com/j2ee/1.4/docs/tutorial-update6/doc/index.htmlthat works with Application Server 8.0/8.1 or upgrade to Application Server 8.2 (seehttp://java.sun.com/j2ee/1.4/download.html#appservto download).
Create the
savingsaccountdatabase table by running thecreate.sqlscript:Deploying the Application
For detailed instructions, see Deploying the J2EE Application.
Running the Client
To run the
SavingsAccountClientprogram, do the following:
- In a terminal window, go to this directory:
<INSTALL>/j2eetutorial14/examples/ejb/savingsaccount/- Type the following command on a single line:
appclient -client SavingsAccountAppClient.jar- The client should display the following lines:
balance = 68.25
balance = 32.55
456: 44.77
730: 19.54
268: 100.07
836: 32.55
456: 44.77
4.00
7.00To modify this example, see the instructions in Modifying the J2EE Application.