Download
FAQ History |
API
Search Feedback |
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 PointBase, the database software that is included in the Application Server bundle.
- Start the PointBase server. For instructions, see Starting and Stopping the PointBase Database Server.
- Create the database tables by running the
create.sql
script.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.
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:pointbase:server://localhost:9092/sun-appserv-samples -username pbpublic -password pbpublic -table LEAGUE -table PLAYER -table TEAM -table TEAM_PLAYER -schemaname PBPUBLIC -driver com.pointbase.jdbc.jdbcUniversalDriver -out build/cmproster.dbschemaBuilding the Enterprise Beans
You will now build the enterprise beans.
Creating the Enterprise Application
Create a new application in
deploytool
calledRosterApp
.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
.Packaging LeagueBean, PlayerBean, and TeamBean
To package
LeagueBean
,PlayerBean
, andTeamBean
, follow these steps:
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 FileNewEnterprise Bean.- In the EJB JAR screen:
- In the General screen:
- In the Configuration Options screen, select No under Expose Bean as Web Service End Point.
- 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 FileNewEnterprise Bean.- In the EJB JAR screen:
- In the General screen:
- In the Configuration Options screen, select No under Expose Bean as Web Service End Point.
- 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 Select 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 Select Select Methods:
- Click OK.
- Select FileSave.
Establishing Relationships between Enterprise Beans
TeamJAR
has the relationships shown in Figure 27-3.
Figure 27-3 Relationships between Enterprise Beans in
TeamJAR
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:
- Click Add.
- In the Add Relationship dialog box:
Creating the Field and Relationship Mappings
To set the container-managed fields and relationships, do the following:
Setting RosterBean's Transaction Attributes
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 FileSave.
Packaging the Enterprise Application Client
To package the application client, do the following:
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 FileSave.
Deploying the Enterprise Application
You can now deploy the enterprise application by following these steps:
- Select ToolsDeploy.
- 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.
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.