Download
FAQ History |
API
Search Feedback |
Method Invocations in RosterApp
To show how the various components interact, this section describes the sequence of method invocations that occur for particular functions. The source code for the components is in the
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster
directory.Creating a Player
1. RosterClient
The
RosterClient
invokes thecreatePlayer
business method of theRosterBean
session bean to create a new player. In the following line of code, the type of themyRoster
object isRoster
, the remote interface ofRosterBean
. The argument of thecreatePlayer
method is aPlayerDetails
object, which encapsulates information about a particular player.2. RosterBean
The
createPlayer
method of theRosterBean
session bean creates a new instance of thePlayerBean
entity bean. Because the access ofPlayerBean
is local, thecreate
method is defined in the local home interface,LocalPlayerHome
. The type of theplayerHome
object isLocalPlayerHome
. Here is the source code for thecreatePlayer
method:public void createPlayer(PlayerDetails details) { try { LocalPlayer player = playerHome.create(details.getId(), details.getName(), details.getPosition(), details.getSalary()); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }3. PlayerBean
The
ejbCreate
method assigns the input arguments to the bean's persistent fields by calling theset
access methods. At the end of the transaction that contains the create call, the container saves the persistent fields in the database by issuing an SQLINSERT
statement. The code for theejbCreate
method follows.public String ejbCreate (String id, String name, String position, double salary) throws CreateException { setPlayerId(id); setName(name); setPosition(position); setSalary(salary); return null; }Adding a Player to a Team
1. RosterClient
The
RosterClient
calls theaddPlayer
business method of theRosterBean
session bean to add player P1 to team T1. TheP1
andT1
parameters are the primary keys of thePlayerBean
andTeamBean
instances, respectively.2. RosterBean
The
addPlayer
method performs two steps. First, it callsfindByPrimaryKey
to locate thePlayerBean
andTeamBean
instances. Second, it invokes theaddPlayer
business method of theTeamBean
entity bean. Here is the source code for theaddPlayer
method of theRosterBean
session bean:public void addPlayer(String playerId, String teamId) { try { LocalTeam team = teamHome.findByPrimaryKey(teamId); LocalPlayer player = playerHome.findByPrimaryKey(playerId); team.addPlayer(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }3. TeamBean
The
TeamBean
entity bean has a relationship field namedplayers
, aCollection
that represents the players that belong to the team. The access methods for theplayers
relationship field are as follows:The
addPlayer
method ofTeamBean
invokes thegetPlayers
access method to fetch theCollection
of relatedLocalPlayer
objects. Next, theaddPlayer
method invokes theadd
method of theCollection
interface. Here is the source code for theaddPlayer
method:public void addPlayer(LocalPlayer player) { try { Collection players = getPlayers(); players.add(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }Removing a Player
1. RosterClient
To remove player
P4
, the client would invoke theremovePlayer
method of theRosterBean
session bean:2. RosterBean
The
removePlayer
method locates thePlayerBean
instance by callingfindBy-PrimaryKey
and then invokes theremove
method on the instance. This invocation signals the container to delete the row in the database that corresponds to thePlayerBean
instance. The container also removes the item for this instance from theplayers
relationship field in theTeamBean
entity bean. By this removal, the container automatically updates theTeamBean-PlayerBean
relationship. Here is theremovePlayer
method of theRosterBean
session bean:public void removePlayer(String playerId) { try { LocalPlayer player = playerHome.findByPrimaryKey(playerId); player.remove(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }Dropping a Player from a Team
1. RosterClient
To drop player
P2
from teamT1
, the client would call thedropPlayer
method of theRosterBean
session bean:2. RosterBean
The
dropPlayer
method retrieves thePlayerBean
andTeamBean
instances by calling theirfindByPrimaryKey
methods. Next, it invokes thedropPlayer
business method of theTeamBean
entity bean. ThedropPlayer
method of theRosterBean
session bean follows:public void dropPlayer(String playerId, String teamId) { try { LocalPlayer player = playerHome.findByPrimaryKey(playerId); LocalTeam team = teamHome.findByPrimaryKey(teamId); team.dropPlayer(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }3. TeamBean
The
dropPlayer
method updates theTeamBean-PlayerBean
relationship. First, the method retrieves theCollection
ofLocalPlayer
objects that correspond to theplayers
relationship field. Next, it drops the targetplayer
by calling theremove
method of theCollection
interface. Here is thedropPlayer
method of theTeamBean
entity bean:public void dropPlayer(LocalPlayer player) { try { Collection players = getPlayers(); players.remove(player); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } }Getting the Players of a Team
1. RosterClient
The client can fetch a team's players by calling the
getPlayersOfTeam
method of theRosterBean
session bean. This method returns anArrayList
ofPlayerDetails
objects. APlayerDetail
object contains four variables--playerId
,name
,position
, andsalary
--which are copies of thePlayerBean
persistent fields. TheRosterClient
calls thegetPlayersOfTeam
method as follows:2. RosterBean
The
getPlayersOfTeam
method of theRosterBean
session bean locates theLocalTeam
object of the target team by invoking thefindByPrimaryKey
method. Next, thegetPlayersOfTeam
method calls thegetPlayers
method of theTeamBean
entity bean. Here is the source code for thegetPlayersOfTeam
method:public ArrayList getPlayersOfTeam(String teamId) { Collection players = null; try { LocalTeam team = teamHome.findByPrimaryKey(teamId); players = team.getPlayers(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } return copyPlayersToDetails(players); }The
getPlayersOfTeam
method returns theArrayList
ofPlayerDetails
objects that is generated by thecopyPlayersToDetails
method:private ArrayList copyPlayersToDetails(Collection players) { ArrayList detailsList = new ArrayList(); Iterator i = players.iterator(); while (i.hasNext()) { LocalPlayer player = (LocalPlayer) i.next(); PlayerDetails details = new PlayerDetails(player.getPlayerId(), player.getName(), player.getPosition(), player.getSalary()); detailsList.add(details); } return detailsList; }3. TeamBean
The
getPlayers
method of theTeamBean
entity bean is an access method of theplayers
relationship field:This method is exposed to local clients because it is defined in the local interface,
LocalTeam
:When invoked by a local client, a
get
access method returns a reference to the relationship field. If the local client alters the object returned by aget
access method, it also alters the value of the relationship field inside the entity bean. For example, a local client of theTeamBean
entity bean could drop a player from a team as follows:LocalTeam team = teamHome.findByPrimaryKey(teamId); Collection players = team.getPlayers(); players.remove(player);If you want to prevent a local client from modifying a relationship field in this manner, you should take the approach described in the next section.
Getting a Copy of a Team's Players
In contrast to the methods discussed in the preceding section, the methods in this section demonstrate the following techniques:
1. RosterClient
If you wanted to hide the salary of a player from a remote client, you would require the client to call the
getPlayersOfTeamCopy
method of theRosterBean
session bean. Like thegetPlayersOfTeam
method, thegetPlayersOfTeamCopy
method returns anArrayList
ofPlayerDetails
objects. However, the objects returned bygetPlayersOfTeamCopy
are different: theirsalary
variables have been set to zero. TheRosterClient
calls thegetPlayersOfTeamCopy
method as follows:2. RosterBean
Unlike the
getPlayersOfTeam
method, thegetPlayersOfTeamCopy
method does not invoke thegetPlayers
access method that is exposed in theLocalTeam
interface. Instead, thegetPlayersOfTeamCopy
method retrieves a copy of the player information by invoking thegetCopyOfPlayers
business method that is defined in theLocalTeam
interface. As a result, thegetPlayersOfTeamCopy
method cannot modify theplayers
relationship field ofTeamBean
. Here is the source code for thegetPlayersOfTeamCopy
method ofRosterBean
:public ArrayList getPlayersOfTeamCopy(String teamId) { ArrayList playersList = null; try { LocalTeam team = teamHome.findByPrimaryKey(teamId); playersList = team.getCopyOfPlayers(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } return playersList; }3. TeamBean
The
getCopyOfPlayers
method ofTeamBean
returns anArrayList
ofPlayerDetails
objects. To create thisArrayList
, the method iterates through theCollection
of relatedLocalPlayer
objects and copies information to the variables of thePlayerDetails
objects. The method copies the values ofPlayerBean
persistent fields--except for thesalary
field, which it sets to zero. As a result, a player's salary is hidden from a client that invokes thegetPlayersOfTeamCopy
method. The source code for thegetCopyOfPlayers
method ofTeamBean
follows.public ArrayList getCopyOfPlayers() { ArrayList playerList = new ArrayList(); Collection players = getPlayers(); Iterator i = players.iterator(); while (i.hasNext()) { LocalPlayer player = (LocalPlayer) i.next(); PlayerDetails details = new PlayerDetails(player.getPlayerId(), player.getName(), player.getPosition(), 0.00); playerList.add(details); } return playerList; }Finding the Players by Position
1. RosterClient
The client starts the procedure by invoking the
getPlayersByPosition
method of theRosterBean
session bean:2. RosterBean
The
getPlayersByPosition
method retrieves theplayers
list by invoking thefindByPosition
method of thePlayerBean
entity bean:public ArrayList getPlayersByPosition(String position) { Collection players = null; try { players = playerHome.findByPosition(position); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } return copyPlayersToDetails(players); }3. PlayerBean
The
LocalPlayerHome
interface defines thefindByPosition
method:Because the
PlayerBean
entity bean uses container-managed persistence, the entity bean class (PlayerBean
) does not implement its finder methods. To specify the queries associated with the finder methods, EJB QL queries must be defined in the bean's deployment descriptor. For example, thefindByPosition
method has this EJB QL query:At runtime, when the container invokes the
findByPosition
method, it will execute the corresponding SQLSELECT
statement.For details about EJB QL, please refer to Chapter 29. To learn how to view and edit an EJB QL query in
deploytool
, see the section Finder/Select Methods Dialog Box (PlayerBean).Getting the Sports of a Player
1. RosterClient
The client invokes the
getSportsOfPlayer
method of theRosterBean
session bean:2. RosterBean
The
getSportsOfPlayer
method returns anArrayList
ofString
objects that represent the sports of the specified player. It constructs theArrayList
from aCollection
returned by thegetSports
business method of thePlayerBean entity
bean. Here is the source code for thegetSportsOfPlayer
method of theRosterBean
session bean:public ArrayList getSportsOfPlayer(String playerId) { ArrayList sportsList = new ArrayList(); Collection sports = null; try { LocalPlayer player = playerHome.findByPrimaryKey(playerId); sports = player.getSports(); } catch (Exception ex) { throw new EJBException(ex.getMessage()); } Iterator i = sports.iterator(); while (i.hasNext()) { String sport = (String) i.next(); sportsList.add(sport); } return sportsList; }3. PlayerBean
The
getSports
method is a wrapper for theejbSelectSports
method. Because the parameter of theejbSelectSports
method is of typeLocalPlayer
, thegetSports
method passes along a reference to the entity bean instance. ThePlayerBean
class implements thegetSports
method as follows:public Collection getSports() throws FinderException { LocalPlayer player = (team.LocalPlayer)context.getEJBLocalObject(); return ejbSelectSports(player); }The
PlayerBean
class defines theejbSelectSports
method:The bean's deployment descriptor specifies the following EJB QL query for the
ejbSelectSports
method:Because
PlayerBean
uses container-managed persistence, when theejbSelectSports
method is invoked the EJB container will execute its corresponding SQLSELECT
statement.
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.