The example programs in Exercise 7.2 illustrate the remaining four types of entity-bean relationship:
No database is initialization needed; JBoss will create the needed tables at bean deployment.
Perform the following steps:
Open a command prompt or shell terminal and change to the
ex07_2
directory created by the extraction
process
Set the JAVA_HOME
and
JBOSS_HOME
environment variables to point to where
your JDK and JBoss 4.0 are installed. Examples:
Windows:C:workbookex07_2> set JAVA_HOME=C:jdk1.4.2 C:workbookex07_2> set JBOSS_HOME=C:jboss-4.0
|
Unix:$ export JAVA_HOME=/usr/local/jdk1.4.2 $ export JBOSS_HOME=/usr/local/jboss-4.0
|
Add ant to your execution path.
Windows:C:workbookex07_2> set PATH=..antin;%PATH%
|
Unix:$ export PATH=../ant/bin:$PATH
|
Perform the build by typing ant
.
As in the last exercise, you will see titan.jar
rebuilt, copied to the JBoss deploy
directory,
and redeployed by the application server.
No new concepts are introduced in the JBoss-specific deployment descriptors.
This exercise uses six example programs to demonstrate the various relationships described in the corresponding chapter of the EJB book. Note that you can rerun any of these examples as many times as you like because they clean up after themselves by removing all the entities they create.
Client_72a
Demonstrates the many-to-one unidirectional Cruise-Ship relationship, as well as the sharing of a reference between different beans.
Client_72b
Demonstrates the one-to-many bidirectional Cruise-Reservation relationship and how to use set methods to modify reservations that are associated with a cruise.
Client_72c
Expands on the Cruise-Reservation relationship, using the
addAll( )
method to modify the reservations
associated with a cruise.
Client_72d
Demonstrates the many-to-many bidirectional Customer-Reservation relationship.
Client_72e
Continues the demonstration of the Customer-Reservation relationship
by showing how to use setCustomers( )
to modify the
Customers for a Reservation.
Client_72f
Demonstrates the many-to-many unidirectional Cabin-Reservation relationship.
The business logic for this example is implemented in
com.titan.test.Test72Bean
, in the
test72a( )
method.
Client_72a
models the many-to-one unidirectional
Cruise-Ship relationships shown in Figure 7-12 of the EJB section of
this book.
First, this code creates the relationships described in the top half of the figure. Cruises 1 to 3 embark on Ship A; Cruises 4 to 6 set sail on Ship B.
cruises[0] = cruisehome.create("Cruise 1", shipA); cruises[1] = cruisehome.create("Cruise 2", shipA); cruises[2] = cruisehome.create("Cruise 3", shipA); cruises[3] = cruisehome.create("Cruise 4", shipB); cruises[4] = cruisehome.create("Cruise 5", shipB); cruises[5] = cruisehome.create("Cruise 6", shipB);
Next, the code switches Cruise 4 so that it is now handled by Ship A instead of Ship B. This relationship change is illustrated in the bottom half of Figure 7-12:
ShipLocal newship = cruises[0].getShip( ); cruises[3].setShip(newship);
In order to run Client_72a
, invoke the Ant task
run.client_72a
. Remember to set your
JBOSS_HOME
and PATH
environment
variables. The output should look something like this:
C:workbookex07_2>ant run.client_72a Buildfile: build.xml prepare: compile: run.client_72a: [java] Creating Ships [java] PK=1001 name=Ship A tonnage=30000.0 [java] PK=1002 name=Ship B tonnage=40000.0 [java] Creating Cruises [java] Cruise 1 is using Ship A [java] Cruise 2 is using Ship A [java] Cruise 3 is using Ship A [java] Cruise 4 is using Ship B [java] Cruise 5 is using Ship B [java] Cruise 6 is using Ship B [java] Changing Cruise 4 to use same ship as Cruise 1 [java] Cruise 1 is using Ship A [java] Cruise 2 is using Ship A [java] Cruise 3 is using Ship A [java] Cruise 4 is using Ship A [java] Cruise 5 is using Ship B [java] Cruise 6 is using Ship B [java] Removing created beans
The business logic for this example is implemented in
com.titan.test.Test72Bean
, in the
test72b( )
method.
Client_72b
models the one-to-many bidirectional
Cruise-Reservation relationships shown in Figure 7-14 of the EJB section of this book.
First, this code creates the relationships described in the top half of the figure. Reservations 1 to 3 are for Cruise A; Reservations 4 to 6 are for Cruise B:
for (int i = 0; i < 6; i++) { CruiseLocal cruise = (i < 3) ? cruiseA : cruiseB; reservations[i] = reservationhome.create(cruise,new ArrayList( )); reservations[i].setDate(date.getTime( )); reservations[i].setAmountPaid((i + 1) * 1000.0); date.add(Calendar.DAY_OF_MONTH, 7); }
Next, the code sets the reservations of Cruise B to be the reservations of Cruise A. Those relationships actually move from A to B. Afterward, Cruise A and Reservations 1-3 no longer have any Cruise-Reservation relationships, as you see in the bottom half of Figure 7-14:
Collection a_reservations = cruiseA.getReservations( ); cruiseB.setReservations( a_reservations );
To run Client_72b
, invoke the Ant task
run.client_72b
. Remember to set your
JBOSS_HOME
and PATH
environment
variables. The output will look something like this:
C:workbookex07_2>ant run.client_72b Buildfile: build.xml prepare: compile: run.client_72b: [java] Creating Cruises [java] name=Cruise A [java] name=Cruise B [java] Creating Reservations [java] Reservation date=11/01/2002 is for Cruise A [java] Reservation date=11/08/2002 is for Cruise A [java] Reservation date=11/15/2002 is for Cruise A [java] Reservation date=11/22/2002 is for Cruise B [java] Reservation date=11/29/2002 is for Cruise B [java] Reservation date=12/06/2002 is for Cruise B [java] Testing CruiseB.setReservations(CruiseA.getReservations( ) ) [java] Reservation date=11/01/2002 is for Cruise B [java] Reservation date=11/08/2002 is for Cruise B [java] Reservation date=11/15/2002 is for Cruise B [java] Reservation date=11/22/2002 is for No Cruise! [java] Reservation date=11/29/2002 is for No Cruise! [java] Reservation date=12/06/2002 is for No Cruise! [java] Removing created beans.
The business logic for this example is implemented in
com.titan.test.Test72Bean
, in the
test72c( )
method.
Client_72c
explores the use of
Collection.addAll( )
in the Cruise-Reservation
s shown in Figure 7-15
of the EJB section.
First, this code creates the relationships described in the top half of the figure. Reservations 1 to 3 are for Cruise A; Reservations 4 to 6 are for Cruise B:
for (int i = 0; i < 6; i++) { CruiseLocal cruise = (i < 3) ? cruiseA : cruiseB; reservations[i] = reservationhome.create(cruise,new ArrayList( )); reservations[i].setDate(date.getTime( )); reservations[i].setAmountPaid((i + 1) * 1000.0); date.add(Calendar.DAY_OF_MONTH, 7); }
Then the code changes all reservations of Cruise A to be for Cruise B instead. The result of this action can be seen in the bottom half of Figure 7-15:
Collection a_reservations = cruiseA.getReservations( ); Collection b_reservations = cruiseB.getReservations( ); b_reservations.addAll(a_reservations);
In order to run Client_72c,
invoke the Ant task
run.client_72c
. Remember to set your
JBOSS_HOME
and PATH
environment
variables. The output should look something like this:
C:workbookex07_2>ant run.client_72c Buildfile: build.xml prepare: compile: run.client_72c: [java] Creating Cruises [java] name=Cruise A [java] name=Cruise B [java] Creating Reservations [java] Reservation date=11/01/2002 is for Cruise A [java] Reservation date=11/08/2002 is for Cruise A [java] Reservation date=11/15/2002 is for Cruise A [java] Reservation date=11/22/2002 is for Cruise B [java] Reservation date=11/29/2002 is for Cruise B [java] Reservation date=12/06/2002 is for Cruise B [java] Testing using b_res.addAll(a_res) to combine reservations [java] Reservation date=11/01/2002 is for Cruise B [java] Reservation date=11/08/2002 is for Cruise B [java] Reservation date=11/15/2002 is for Cruise B [java] Reservation date=11/22/2002 is for Cruise B [java] Reservation date=11/29/2002 is for Cruise B [java] Reservation date=12/06/2002 is for Cruise B
The business logic for this example is implemented in
com.titan.test.Test72Bean
, in the
test72d( )
method.
Client_72d
explores the use of
Collection.addAll( )
in the Customer-Reservation
many-to-many bidirectional relationship shown in Figure 7-17 of the EJB section.
First, two sets of customers are created:
Set lowcustomers = new HashSet( ); Set highcustomers = new HashSet( ); CustomerLocal[] allCustomers = new CustomerLocal[6]; for (int kk=0; kk<6; kk++) { CustomerLocal cust = customerhome.create(new Integer(kk)); allCustomers[kk] = cust; cust.setName(new Name("Customer "+kk,"")); if (kk<=2) { lowcustomers.add(cust); } else { highcustomers.add(cust); } out.println(cust.getName( ).getLastName( )); }
Next, the code creates six reservations and relates them to one of the customer sets, as shown in the top half of Figure 7-17. Customers 1 to 3 have Reservation A; Customers 4 to 6 have Reservation B.
reservations[0] = reservationhome.create(cruiseA, lowcustomers); reservations[0].setDate(date.getTime( )); reservations[0].setAmountPaid(4000.0); date.add(Calendar.DAY_OF_MONTH, 7); reservations[1] = reservationhome.create(cruiseA, highcustomers); reservations[1].setDate(date.getTime( )); reservations[1].setAmountPaid(5000.0);
Finally, the code uses addAll( )
to relate
Customers 4 to 6 with Reservation A. They now have a reservation for
both Cruise A and Cruise B. The bottom half of Figure 7-17 illustrates this result:
Set customers_a = reservations[0].getCustomers( ); Set customers_b = reservations[1].getCustomers( ); customers_a.addAll(customers_b);
In order to run Client_72d
, invoke the Ant task
run.client_72d
. Remember to set your
JBOSS_HOME
and PATH
environment
variables. The output should look something like this:
C:workbookex07_2>ant run.client_72d Buildfile: build.xml prepare: compile: run.client_72d: [java] cruise.getName( )=Cruise A [java] ship.getName( )=Ship A [java] cruise.getShip( ).getName( )=Ship A [java] Creating Customers 1-6 [java] Customer 0 [java] Customer 1 [java] Customer 2 [java] Customer 3 [java] Customer 4 [java] Customer 5 [java] Creating Reservations 1 and 2, each with 3 customers [java] Reservation date=11/01/2002 is for Cruise A with customers Customer 2 Customer 1 Customer 0 [java] Reservation date=11/08/2002 is for Cruise A with customers Customer 5 Customer 4 Customer 3 [java] Performing customers_a.addAll(customers_b) test [java] Reservation date=11/01/2002 is for Cruise A with customers Customer 2 Customer 1 Customer 0 Customer 5 Custo mer 4 Customer 3 [java] Reservation date=11/08/2002 is for Cruise A with customers Customer 5 Customer 4 Customer 3 [java] Removing created beans
The business logic for this example is implemented in
com.titan.test.Test72Bean
, in the
test72e( )
method.
Client_72e
explores the use of
setCustomers( )
to share an entire collection, in
the Customer-Reservation many-to-many bidirectional relationship
shown in Figure 7-18 of the EJB section.
First, four sets of customers are created:
Set customers13 = new HashSet( ); Set customers24 = new HashSet( ); Set customers35 = new HashSet( ); Set customers46 = new HashSet( ); CustomerLocal[] allCustomers = new CustomerLocal[6]; for (int kk=0; kk<6; kk++) { CustomerLocal cust = customerhome.create(new Integer(kk)); allCustomers[kk] = cust; cust.setName(new Name("Customer "+kk,"")); if (kk<=2) { customers13.add(cust); } if (kk>=1 && kk<=3) { customers24.add(cust); } if (kk>=2 && kk<=4) { customers35.add(cust); } if (kk>=3) { customers46.add(cust); } }
Next, the code sets up the relationships between Customers and Reservations shown in the top half of Figure 7-18:
reservations[0] = reservationhome.create(cruiseA, customers13); reservations[0].setDate(date.getTime( )); reservations[0].setAmountPaid(4000.0); date.add(Calendar.DAY_OF_MONTH, 7); reservations[1] = reservationhome.create(cruiseA, customers24); reservations[1].setDate(date.getTime( )); reservations[1].setAmountPaid(5000.0); date.add(Calendar.DAY_OF_MONTH, 7); reservations[2] = reservationhome.create(cruiseA, customers35); reservations[2].setDate(date.getTime( )); reservations[2].setAmountPaid(6000.0); date.add(Calendar.DAY_OF_MONTH, 7); reservations[3] = reservationhome.create(cruiseA, customers46); reservations[3].setDate(date.getTime( )); reservations[3].setAmountPaid(7000.0);
Finally, the code sets up the relationships shown in the bottom half of the figure:
Set customers_a = reservations[0].getCustomers( ); reservations[3].setCustomers(customers_a);
In order to run Client_72e
, invoke the Ant task
run.client_72e
. Remember to set your
JBOSS_HOME
and PATH
environment
variables. The output should look something like this:
C:workbookex07_2>ant run.client_72e Buildfile: build.xml prepare: compile: run.client_72e: [java] Creating a Ship and Cruise [java] cruise.getName( )=Cruise A [java] ship.getName( )=Ship A [java] cruise.getShip( ).getName( )=Ship A [java] Creating Customers 1-6 [java] Creating Reservations 1-4 using three customers each [java] Reservation date=11/01/2002 is for Cruise A with customers Customer 2 Customer 1 Customer 0 [java] Reservation date=11/08/2002 is for Cruise A with customers Customer 3 Customer 2 Customer 1 [java] Reservation date=11/15/2002 is for Cruise A with customers Customer 4 Customer 3 Customer 2 [java] Reservation date=11/22/2002 is for Cruise A with customers Customer 5 Customer 4 Customer 3 [java] Performing reservationD.setCustomers(customersA) test [java] Reservation date=11/01/2002 is for Cruise A with customers Customer 2 Customer 1 Customer 0 [java] Reservation date=11/08/2002 is for Cruise A with customers Customer 3 Customer 2 Customer 1 [java] Reservation date=11/15/2002 is for Cruise A with customers Customer 4 Customer 3 Customer 2 [java] Reservation date=11/22/2002 is for Cruise A with customers Customer 2 Customer 1 Customer 0 [java] Removing created beans.
The business logic for this example is implemented in
com.titan.test.Test72Bean
, in the
test72f( )
method.
Client_72f
demonstrates removing beans in the
many-to-many unidirectional Cabin-Reservation relationship, as shown
in Figure 7-20 of the EJB section.
First, four sets of cabins are created:
Set cabins13 = new HashSet( ); Set cabins24 = new HashSet( ); Set cabins35 = new HashSet( ); Set cabins46 = new HashSet( ); CabinLocal[] allCabins = new CabinLocal[6]; for (int kk=0; kk<6; kk++) { CabinLocal cabin = cabinhome.create(new Integer(kk)); allCabins[kk] = cabin; cabin.setName("Cabin "+kk); if (kk<=2) { cabins13.add(cabin); } if (kk>=1 && kk<=3) { cabins24.add(cabin); } if (kk>=2 && kk<=4) { cabins35.add(cabin); } if (kk>=3) { cabins46.add(cabin); } out.println(cabin.getName( )); }
Next, the code creates the initial relationships between Reservations and Cabins, shown in the top half of Figure 7-20:
reservations[0] = reservationhome.create(cruiseA, null); reservations[0].setCabins(cabins13); reservations[0].setDate(date.getTime( )); reservations[0].setAmountPaid(4000.0); date.add(Calendar.DAY_OF_MONTH, 7); reservations[1] = reservationhome.create(cruiseA, null); reservations[1].setCabins(cabins24); reservations[1].setDate(date.getTime( )); reservations[1].setAmountPaid(5000.0); date.add(Calendar.DAY_OF_MONTH, 7); reservations[2] = reservationhome.create(cruiseA, null); reservations[2].setCabins(cabins35); reservations[2].setDate(date.getTime( )); reservations[2].setAmountPaid(6000.0); date.add(Calendar.DAY_OF_MONTH, 7); reservations[3] = reservationhome.create(cruiseA, null); reservations[3].setCabins(cabins46); reservations[3].setDate(date.getTime( )); reservations[3].setAmountPaid(7000.0);
Finally, the code removes some of the relationships, as shown in the bottom half of the figure:
Set cabins_a = reservations[0].getCabins( ); Iterator iterator = cabins_a.iterator( ); while (iterator.hasNext( )) { CabinLocal cc = (CabinLocal)iterator.next( ); out.println("Removing "+cc.getName( )+" from cabins_a"); iterator.remove( ); }
In order to run Client_72f,
invoke the Ant task
run.client_72f
. Remember to set your
JBOSS_HOME
and PATH
environment
variables. The output should look something like this:
C:workbookex07_2>ant run.client_72f Buildfile: build.xml prepare: compile: run.client_72f: [java] Creating a Ship and Cruise [java] cruise.getName( )=Cruise A [java] ship.getName( )=Ship A [java] cruise.getShip( ).getName( )=Ship A [java] Creating Cabins 1-6 [java] Cabin 0 [java] Cabin 1 [java] Cabin 2 [java] Cabin 3 [java] Cabin 4 [java] Cabin 5 [java] Creating Reservations 1-4 using three cabins each [java] Reservation date=11/01/2002 is for Cruise A with cabins Cabin 2 Cabin 1 Cabin 0 [java] Reservation date=11/08/2002 is for Cruise A with cabins Cabin 3 Cabin 2 Cabin 1 [java] Reservation date=11/15/2002 is for Cruise A with cabins Cabin 4 Cabin 3 Cabin 2 [java] Reservation date=11/22/2002 is for Cruise A with cabins Cabin 5 Cabin 4 Cabin 3 [java] Performing cabins_a collection iterator.remove( ) test [java] Removing Cabin 2 from cabins_a [java] Removing Cabin 1 from cabins_a [java] Removing Cabin 0 from cabins_a [java] Reservation date=11/01/2002 is for Cruise A with cabins [java] Reservation date=11/08/2002 is for Cruise A with cabins Cabin 3 Cabin 2 Cabin 1 [java] Reservation date=11/15/2002 is for Cruise A with cabins Cabin 4 Cabin 3 Cabin 2 [java] Reservation date=11/22/2002 is for Cruise A with cabins Cabin 5 Cabin 4 Cabin 3 [java] Removing created beans