June 2004
Intermediate to advanced
792 pages
23h 17m
English
Let’s look at how a client would use an enterprise bean to do something useful. We’ll start with the Cabin EJB defined earlier. A cabin is a thing or place with a description that is stored in a database. To make the example a little more real, assume that there are other entity beans: Ship, Cruise, Ticket, Customer, Employee, and so on.
Imagine that a GUI client needs to display information about a particular cruise: the cruise name, the ship name, and a list of cabins. Using the cruise ID obtained from a text field, we can use our beans to look up data about the cruise. Here’s the code:
CruiseHomeRemote cruiseHome = ... ; // use JNDI to get the home // Get the cruise ID text field 1. String cruiseID = textField1.getText( ); // Create an EJB primary key from the cruise ID. Integer pk = new Integer(cruiseID); // Use the primary key to find the cruise. CruiseRemote cruise = cruiseHome.findByPrimaryKey(pk); // Set text field 2 to show the cruise name. textField2.setText(cruise.getName( )); // Get a remote reference to the ship that will be used // for the cruise from the cruise bean. ShipRemote ship = cruise.getShip( ); // Set text field 3 to show the ship's name. textField3.setText(ship.getName( )); // Get all the cabins on the ship. Collection cabins = ship.getCabins( ); Iterator cabinItr = cabins.iterator( ); // Iterate through the enumeration, adding the name of each cabin // to a list box. while(cabinItr.hasNext( )) ...
Read now
Unlock full access