Download
FAQ History |
API
Search Feedback |
The PlayerBean Code
The
PlayerBean
entity bean represents a player in a sports league. Like any local entity bean with container-managed persistence,PlayerBean
needs the following code:The source code for this example is in the
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster
directory.Entity Bean Class
The code of the entity bean class must meet the container-managed persistence syntax requirements. First, the class must be defined as
public
andabstract
. Second, the class must implement the following:The entity bean class must not implement these methods:
Differences between Container-Managed and Bean-Managed Code
Because it contains no calls to access the database, an entity bean with container-managed persistence requires a lot less code than one with bean-managed persistence. For example, the
PlayerBean.java
source file discussed in this chapter is much smaller than theSavingsAccountBean.java
code documented in Chapter 26. Table 27-1 compares the code of the two types of entity beans.
Note that for both types of persistence, the rules for implementing business and home methods are the same. See the sections The Business Methods and The Home Methods in Chapter 26.
Access Methods
An entity bean with container-managed persistence has persistent and relationship fields. These fields are virtual, so you do not code them in the class as instance variables. Instead, you specify them in the bean's deployment descriptor. To permit access to the fields, you define abstract
get
andset
methods in the entity bean class.Access Methods for Persistent Fields
The EJB container automatically performs the database storage and retrieval of the bean's persistent fields. The deployment descriptor of
PlayerBean
specifies the following persistent fields:The
PlayerBean
class defines the access methods for the persistent fields as follows:public abstract String getPlayerId(); public abstract void setPlayerId(String id); public abstract String getName(); public abstract void setName(String name); public abstract String getPosition(); public abstract void setPosition(String position); public abstract double getSalary(); public abstract void setSalary(double salary);The name of an access method begins with
get
orset
, followed by the capitalized name of the persistent or relationship field. For example, the accessor methods for thesalary
field aregetSalary
andsetSalary
. This naming convention is similar to that of JavaBeans components.Access Methods for Relationship Fields
In the
RosterApp
application, a player can belong to multiple teams, so aPlayerBean
instance may be related to manyTeamBean
instances. To specify this relationship, the deployment descriptor ofPlayerBean
defines a relationship field namedteams
. In thePlayerBean
class, the access methods for theteams
relationship field are as follows:Finder and Select Methods
Finder and select methods use EJB QL queries to return objects and state information of entity beans using container-managed persistence.
A select method is similar to a finder method in the following ways:
However, a select method differs significantly from a finder method:
- A select method can return a persistent field (or a collection thereof) of a related entity bean. A finder method can return only a local or remote interface (or a collection of interfaces).
- Because it is not exposed in any of the local or remote interfaces, a select method cannot be invoked by a client. It can be invoked only by the methods implemented within the entity bean class. A select method is usually invoked by either a business or a home method.
- A select method is defined in the entity bean class. For bean-managed persistence, a finder method is defined in the entity bean class, but for container-managed persistence it is not.
The
PlayerBean
class defines these select methods:public abstract Collection ejbSelectLeagues(LocalPlayer player) throws FinderException; public abstract Collection ejbSelectSports(LocalPlayer player) throws FinderException;The signature for a select method must follow these rules:
Business Methods
Because clients cannot invoke select methods, the
PlayerBean
class wraps them in thegetLeagues
andgetSports
business methods:public Collection getLeagues() throws FinderException { LocalPlayer player = (team.LocalPlayer)context.getEJBLocalObject(); return ejbSelectLeagues(player); } public Collection getSports() throws FinderException { LocalPlayer player = (team.LocalPlayer)context.getEJBLocalObject(); return ejbSelectSports(player); }Entity Bean Methods
Because the container handles persistence, the life-cycle methods in the
PlayerBean
class are nearly empty.The
ejbCreate
method initializes the bean instance by assigning the input arguments to the persistent fields. At the end of the transaction that contains the create call, the container inserts a row into the database. Here is the source code for theejbCreate
method:public String ejbCreate (String id, String name, String position, double salary) throws CreateException { setPlayerId(id); setName(name); setPosition(position); setSalary(salary); return null; }The
ejbPostCreate
method returnsvoid
, and it has the same input parameters as theejbCreate
method. If you want to set a relationship field to initialize the bean instance, you should do so in theejbPostCreate
method. You cannot set a relationship field in theejbCreate
method.Except for a debug statement, the
ejbRemove
method in thePlayerBean
class is empty. The container invokesejbRemove
before removing the entity object.The container automatically synchronizes the state of the entity bean with the database. After the container loads the bean's state from the database, it invokes the
ejbLoad
method. In like manner, before storing the state in the database, the container invokes theejbStore
method.Local Home Interface
The local home interface defines the
create
, finder, and home methods that can be invoked by local clients.The syntax rules for a
create
method follow:
- The name must begin with
create
.- It must have the same number and types of arguments as its matching
ejbCreate
method in the entity bean class.- It must return the local interface type of the entity bean.
- The
throws
clause must include the exceptions specified by thethrows
clause of the correspondingejbCreate
method.- The
throws
clause must contain thejavax.ejb.CreateException
.These rules apply for a finder method:
An excerpt of the
LocalPlayerHome
interface follows.package team; import java.util.*; import javax.ejb.*; public interface LocalPlayerHome extends EJBLocalHome { public LocalPlayer create (String id, String name, String position, double salary) throws CreateException; public LocalPlayer findByPrimaryKey (String id) throws FinderException; public Collection findByPosition(String position) throws FinderException; ... public Collection findByLeague(LocalLeague league) throws FinderException; ... }Local Interface
This interface defines the business and access methods that a local client can invoke. The
PlayerBean
class implements two business methods:getLeagues
andgetSports
. It also defines severalget
andset
access methods for the persistent and relationship fields. Theset
methods are hidden from the bean's clients because they are not defined in theLocalPlayer
interface. However, theget
methods are exposed to the clients by the interface:package team; import java.util.*; import javax.ejb.*; public interface LocalPlayer extends EJBLocalObject { public String getPlayerId(); public String getName(); public String getPosition(); public double getSalary(); public Collection getTeams(); public Collection getLeagues() throws FinderException; public Collection getSports() throws FinderException; }
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.