O'Reilly logo

Enterprise JavaBeans, Third 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

Let’s look at how a client would work with an enterprise bean to do something useful. We’ll start with the Cabin EJB defined earlier. A cabin is a thing or place whose description is stored in a database. To make the example a little more real, imagine 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, 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:

CruiseHomeRemote cruiseHome = ... use JNDI to get the home // Get the cruise ID from a text field. String cruiseID = textFields1.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 ...

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