O'Reilly logo

Enterprise JavaBeans, Fourth Edition by Richard Monson-Haefel, Bill Burke, Sacha Labourey

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Enterprise Beans

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.

Getting Information from an Entity Bean

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( )) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required