Building and Running the RosterApp Example
Now that you understand the structure of the
RosterApp
example EAR file, you will assemble the enterprise application and the application client and then run the example. This section gives detailed instructions on how to build and run theRosterApp
example, which is located at<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/
.Creating the Database Tables
The
RosterApp
application uses the database tables shown in Figure 27-2.Figure 27-2 Database Tables in
RosterApp
The instructions that follow explain how to use the
RosterApp
example with Derby, the database software that is included in the Application Server bundle.
- Create the database tables by running the
create.sql
script.
- In a terminal window, go to this directory:
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/
- Type the following command, which runs the
create.sql
script:
asant create-db_common
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.html
that works with Application Server 8.0/8.1 or upgrade to Application Server 8.2 (seehttp://java.sun.com/j2ee/1.4/download.html#appserv
to download).Creating the Data Source
You must create the
jdbc/ejbTutorialDB
data source. For instructions on creating this resource in the Admin Console, see Creating a Data Source.Capturing the Table Schema
You will now create a database schema file, which will allow you to map fields from the enterprise beans to columns in the database tables created earlier.
- Make sure that the Derby server is running.
- In a terminal window, go to
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/
- Type the following command to create the database schema file, named
cmproster.dbschema
, in the ./build/
directory:
asant capture-db-schema
The
capture-db-schema
task calls thecapture-schema
utility to output an XML file,cmproster.dbschema
, which represents the structure of the database tables you created in Creating the Database Tables. Thecmproster.dbschema
file will be used to automatically map the enterprise bean fields to database columns.The command that's called when you run the
capture-db-schema
task is as follows:capture-schema -dburl jdbc:derby://localhost:1527/sun-appserv-samples -username APP -password APP-table LEAGUE -table PLAYER -table TEAM -table TEAM_PLAYER -schemaname APP -driver org.apache.derby.jdbc.ClientDriver -out build/cmproster.dbschemaBuilding the Enterprise Beans
You will now build the enterprise beans.
- In a terminal window, go to this directory:
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/
- Type the following command:
asant build
Creating the Enterprise Application
Create a new application in
deploytool
calledRosterApp
.
- In
deploytool
select FileRight ArrowNewRight ArrowApplication.- In the Application File Name field, click Browse.
- Navigate to
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/
.- In the File Name field enter
RosterApp
.- Click New Application.
- Click OK.
Packaging the Enterprise Beans
You will now package the four enterprise beans:
RosterBean
,LeagueBean
,PlayerBean
, andTeamBean
. Note thatRosterBean
, a stateful session bean, will be packaged inRosterJAR
. The others (LeagueBean
,PlayerBean
, andTeamBean
) are entity beans using container-managed persistence, and will be packaged inTeamJAR
.Packaging RosterBean
RosterBean
is a stateful session bean that accesses the data in the entity beans. Clients will access and manipulate that data throughRosterBean
.
- Create a new enterprise bean in
RosterApp
by selecting FileRight ArrowNewRight ArrowEnterprise Bean.- In the EJB JAR screen:
- Select Create New JAR Module in Application.
- Enter
RosterJAR
under JAR Name.- Click Edit Contents.
- Navigate to
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/build/
.- Select the
roster
andutil
directories.- Click Add.
- Click OK.
- Click Next.
- In the General screen:
- Select
roster.RosterBean
under Enterprise Bean Class.- Enter
RosterBean
under Enterprise Bean Name.- Select Stateful Session under Enterprise Bean Type.
- Select
roster.RosterHome
under Remote Home Interface.- Select
roster.Roster
under Remote Interface.- Select Next.
- Click Finish.
Packaging LeagueBean, PlayerBean, and TeamBean
To package
LeagueBean
,PlayerBean
, andTeamBean
, follow these steps:
- Create a new enterprise bean in
RosterApp
by selecting FileRight ArrowNewRight ArrowEnterprise Bean.- In the EJB JAR screen:
- Select Create New JAR Module in Application.
- Enter
TeamJAR
under JAR Name.- Click Edit Contents.
- Navigate to
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/build/
.- Select the
team
andutil
directories, and thecmproster.dbschema
file.- Click Add.
- Click OK.
- Click Next.
- In the General screen:
- Select
team.LeagueBean
under Enterprise Bean Class.- Enter
LeagueBean
under Enterprise Bean Name.- Select
team.LocalLeagueHome
under Local Home Interface.- Select
team.LocalLeague
under Local Interface.- Click Next.
Note: Be sure to enter the correct name in the Enterprise Bean Name field for
LeagueBean
,PlayerBean
, andTeamBean
to allow the automatic mapping of persistent fields and relationships.
- In the Entity Settings screen:
- In the Persistence Management Type field, select Container-Managed Persistence (2.0).
- In the Fields To Be Persisted frame, check
name
,leagueId
, andsport
.- In the Abstract Schema Name field, enter
League
.- In the Primary Key Class field, choose Select an Existing Field.
- Select
leagueId
[java.lang.String
].- Click Next.
- Click Finish.
Now we'll add
PlayerBean
toTeamJAR
.
- Create a new enterprise bean in
TeamJAR
by selecting FileRight ArrowNewRight ArrowEnterprise Bean.- In the EJB JAR screen:
- Select Add To Existing JAR Module.
- Select
TeamJAR (RosterApp)
under Add To Existing JAR Module.- Click Next.
- In the General screen:
- Select
team.PlayerBean
under Enterprise Bean Class.- Enter
PlayerBean
under Enterprise Bean Name.- Select
team.LocalPlayerHome
under Local Home Interface.- Select
team.LocalPlayer
under Local Interface.- Click Next.
- In the Entity Settings screen:
- In the Persistence Management Type field, select Container-Managed Persistence (2.0).
- In the Fields To Be Persisted frame, check
name
,position
,playerId
, andsalary
.- In the Abstract Schema Name field, enter
Player
.- In the Primary Key Class field choose Select an Existing Field.
- Select
playerId
[java.lang.String
].- Click Next.
- Click Finish.
Now we'll add
TeamBean
toTeamJAR
.
- Create a new enterprise bean in
TeamJAR
by selecting FileRight ArrowNewRight ArrowEnterprise Bean.- In the EJB JAR screen:
- Select Add To Existing JAR Module.
- Select
TeamJAR (RosterApp)
under Add To Existing JAR Module.- Click Next.
- In the General screen:
- Select
team.TeamBean
under Enterprise Bean Class.- Enter
TeamBean
under Enterprise Bean Name.- Select
team.LocalTeamHome
under Local Home Interface.- Select
team.LocalTeam
under Local Interface.- Click Next.
- In the Entity Settings screen:
- In the Persistence Management Type field, select Container-Managed Persistence (2.0).
- In the Fields To Be Persisted frame, check
name
,teamId
, andcity
.- In the Abstract Schema Name field, enter
Team
.- In the Primary Key Class field, choose Select an Existing Field.
- Select
teamId
[java.lang.String
].- Click Next.
- Click Finish.
Adding EJB QL Queries to PlayerBean
PlayerBean
contains finder and selector methods that use EJB QL queries. These steps will add the appropriate EJB QL queries to the methods. See Chapter 29 for more details.
- Select
PlayerBean
in the tree indeploytool
.- Select the Entity tabbed pane.
- Click Find/Select Queries.
- In Show Local Finders:
- For the
findAll
method, enter
select object(p) from Player p
- For the
findByCity
method, enter
select distinct object(p) from Player p,
in (p.teams) as t
where t.city = ?1- For the
findByHigherSalary
method, enter
select distinct object(p1)
from Player p1, Player p2
where p1.salary > p2.salary and
p2.name = ?1- For the
findByLeague
method, enter
select distinct object(p) from Player p,
in (p.teams) as t
where t.league = ?1- For the
findByPosition
method, enter
select distinct object(p) from Player p
where p.position = ?1- For the
findByPositionAndName
method, enter
select distinct object(p) from Player p
where p.position = ?1 and p.name = ?2- For the
findBySalaryRange
method, enter
select distinct object(p) from Player p
where p.salary between ?1 and ?2- For the
findBySport
method, enter
select distinct object(p) from Player p,
in (p.teams) as t
where t.league.sport = ?1- For the
findByTest
method, enter
select distinct object(p) from Player p
where p.name = ?1- For the
findNotOnTeam
method, enter
select object(p) from Player p
where p.teams is empty- In Show Select Methods:
- For the
ejbSelectLeagues
method, enter
select distinct t.league
from Player p, in (p.teams) as t
where p = ?1- For the
ejbSelectSports
method, enter
select distinct t.league.sport
from Player p, in (p.teams) as t
where p = ?1- Under Return EJBs of Type, select None for
ejbSelectSports
.- Click OK.
- Select FileRight ArrowSave.
Establishing Relationships between Enterprise Beans
TeamJAR
has the relationships shown in Figure 27-3.Figure 27-3 Relationships between Enterprise Beans in
TeamJAR
Relationships should always be mapped to the primary key field(s) of the related table.
To create the container-managed relationships between the enterprise beans, do the following:
- Select
TeamJAR
in the tree indeploytool
.- Select the Relationships tabbed pane.
- Click Add.
- In the Add Relationship dialog box:
- In the Multiplicity field, select
Many to Many (*:*)
.- In the Enterprise Bean A section:
- In the Enterprise Bean Name field, select
TeamBean
.- In the Field Referencing Bean B field, select
players
.- In the Field Type field, select
java.util.Collection
.- In the Enterprise Bean B section:
- In the Enterprise Bean Name field, select
PlayerBean
.- In the Field Referencing Bean A field, select
teams
.- In the Field Type field, select
java.util.Collection
.- Click OK.
- Click Add.
- In the Add Relationship dialog box:
- In the Multiplicity field, select
One to Many (1:*)
.- In the Enterprise Bean A section:
- In the Enterprise Bean Name field, select
LeagueBean
.- In the Field Referencing Bean B field, select
teams
.- In the Field Type field, select
java.util.Collection
.- In the Enterprise Bean B section:
- In the Enterprise Bean Name field, select
TeamBean
.- In the Field Referencing Bean A field, select
league
.- Check Delete When Bean A Is Deleted.
- Click OK.
Creating the Field and Relationship Mappings
To set the container-managed fields and relationships, do the following:
- Select
TeamJAR
from the tree indeploytool
.- Select the General tabbed pane.
- Click Sun-specific Settings.
- In the Sun-specific Settings dialog box:
- In the JNDI Name field, enter
jdbc/ejbTutorialDB
.- Click Create Database Mappings.
- In the Create Database Mappings dialog box:
- Select Map to Tables in Database Schema File.
- Select
cmproster.dbschema
under Database Schema Files in Module.- Click OK.
- Confirm that all the fields and relationships have been mapped.
- Click Close.
Setting RosterBean's Transaction Attributes
- Select
RosterBean
indeploytool
's tree.- Click the Transactions tabbed pane.
- In the Transaction Management field click, Container-Managed.
Setting the Enterprise Bean References
First, you'll set the enterprise bean reference for
ejb/SimpleLeague
.
- Select
RosterBean
indeploytool
's tree.- Click the EJB Ref's tabbed pane.
- Click Add.
- In the Add Enterprise Bean Reference dialog box:
- In the Coded Name field, enter
ejb/SimpleLeague
.- In the EJB Type field, select
Entity
.- In the Interfaces field, select
Local
.- In the Home Interface field, enter
team.LocalLeagueHome
.- In the Local/Remote Interface field, enter
team.LocalLeague
.- Under Target EJB, select
ejb-jar-ic1.jar#LeagueBean
in the Enterprise Bean Name drop-down list.- Click OK.
Next, you'll set the enterprise bean reference for
ejb/SimplePlayer
.
- Click Add.
- In the Add Enterprise Bean Reference dialog box:
- In the Coded Name field, enter
ejb/SimplePlayer
.- In the EJB Type field, select
Entity
.- In the Interfaces field, select
Local
.- In the Home Interface field, enter
team.LocalPlayerHome
.- In the Local/Remote Interface field, enter
team.LocalPlayer
.- Under Target EJB, select
ejb-jar-ic1.jar#PlayerBean
in the Enterprise Bean Name drop-down list.- Click OK.
Finally, you'll set the enterprise bean reference for
ejb/SimpleTeam
.
- Click Add.
- In the Add Enterprise Bean Reference dialog box:
- In the Coded Name field, enter
ejb/SimpleTeam
.- In the EJB Type field, select
Entity
.- In the Interfaces field, select
Local
.- In the Home Interface field, enter
team.LocalTeamHome
.- In the Local/Remote Interface field, enter
team.LocalTeam
.- Under Target EJB, select
ejb-jar-ic1.jar#TeamBean
in the Enterprise Bean Name drop-down list.- Click OK.
- Select FileRight ArrowSave.
Packaging the Enterprise Application Client
To package the application client, do the following:
- Create a new application client in
RosterApp
by selecting FileRight ArrowNewRight ArrowApplication Client.- In the JAR File Contents screen:
- Select
RosterApp
under Create New AppClient Module in Application.- Enter
RosterClient
under AppClient Name.- Click Edit Contents.
- Navigate to
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/build/
.- Select the
client
directory.- Click Add.
- Click OK.
- Click Next.
- In the General screen:
- Select
client.RosterClient
under Main Class.- Select
(Use container-managed authentication)
under Callback Handler Class.- Click Next.
- Click Finish.
Setting the Enterprise Bean Reference
You must map the coded JNDI name in the client to the
RosterBean
stateful session bean. To do this, follow these steps:
- Select RosterClient in
deploytool
's tree.- Select the EJB Ref's tabbed pane.
- Click Add.
- In the Add Enterprise Bean Reference dialog box:
- In the Coded Name field enter
ejb/SimpleRoster
.- In the EJB Type field, select
Session
.- In the Interfaces field, select
Remote
.- In the Home Interface field, enter
roster.RosterHome
.- In the Local/Remote Interface field, enter
roster.Roster
.- Under Target EJB, select JNDI Name.
- Select
RosterBean
under JNDI Name.- Click OK.
- Select FileRight ArrowSave.
Deploying the Enterprise Application
You can now deploy the enterprise application by following these steps:
- Select ToolsRight ArrowDeploy.
- In the Deploy Module
RosterApp
dialog box enter the user name and password.- Under Application Client Stub Directory, check Return Client Jar.
- Confirm that the path in the field below the checkbox is
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/
. If it isn't, click Browse and navigate to<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/build/
.- Click OK.
- Confirm that the application deployed and started correctly and that the client stub JAR was created at
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/build/
.- Click Close.
Running the Client Application
To run the client, follow these steps:
- In a terminal, go to
<
INSTALL
>/j2eetutorial14/examples/ejb/cmproster/
.- Type the following command:
appclient -client RosterAppClient.jar
- In the terminal window, the client displays the following output:
P7 Rebecca Struthers midfielder 777.0
P6 Ian Carlyle goalkeeper 555.0
P9 Jan Wesley defender 100.0
P10 Terry Smithson midfielder 100.0
P8 Anne Anderson forward 65.0
T2 Gophers Manteca
T5 Crows Orland
T1 Honey Bees Visalia
P2 Alice Smith defender 505.0
P5 Barney Bold defender 100.0
P25 Frank Fletcher defender 399.0
P9 Jan Wesley defender 100.0
P22 Janice Walker defender 857.0
L1 Mountain Soccer
L2 Valley Basketball
Note: Re-create the database tables using the
create-db_common
task before re-running the client.