O'Reilly logo

Enterprise JavaBeans, Second Edition by Richard Monson-Haefel

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

Now that you actually have a bean to work with, let’s look at how a client would work with a bean to do something useful. We’ll start with the Cabin bean that was defined earlier. A cabin is a thing or place whose description is stored in a database. To make the example a little bit more real, imagine that there are other entity beans, including a Ship, Cruise, Ticket, Passenger, Employee, and so on.

Getting Information from an Entity Bean

Imagine that a GUI client needs to display information about a particular cruise, including the cruise name, the ship name, and a list of cabins. Using the cruise ID obtained from a text field, we can use some of our beans to populate the GUI with data about the requested cruise. Here’s what the code would look like:

CruiseHome cruiseHome = ... getCruiseHome(); // Get the cruise id from a text field. String cruiseID = textFields1.getText(); // Create an EJB primary key from the cruise id. CruisePrimaryKey pk = new CruisePrimaryKey(cruiseID); // Use the primary key to find the cruise. Cruise 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. Ship ship = cruise.getShip(); // Set text field 3 to show the ship's name. textField3.setText(ship.getName()); // Get a list of all the cabins on the ship as remote references // to the cabin beans. Cabin [] cabins = ...

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