ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Developing Your First Enterprise Beans, Part 1
Pages: 1, 2, 3, 4

Creating a new Cabin EJB

Once we have a remote reference to the EJB home, we can use it to create a new Cabin entity:



CabinRemote cabin_1 = home.create(new Integer(1));

We create a new Cabin entity using the create(Integer id) method defined in the remote home interface of the Cabin EJB. When this method is invoked, the EJB home works with the EJB server to create a Cabin EJB, adding its data to the database. The EJB server creates an EJB object to wrap the Cabin EJB instance and returns a remote reference to the EJB object. The cabin_1 variable then contains a remote reference to the Cabin EJB we just created. We don't need to use the PortableRemoteObject.narrow( ) method to get the EJB object from the home reference, because it was declared as returning the CabinRemote type; no casting was required. We don't need to explicitly narrow remote references returned by findByPrimaryKey( ) for the same reason.

With the remote reference to the EJB object, we can update the name, deckLevel, shipId, and bedCount of the Cabin EJB:

CabinRemote cabin_1 = home.create(new Integer(1));
cabin_1.setName("Master Suite");
cabin_1.setDeckLevel(1);
cabin_1.setShipId(1);
cabin_1.setBedCount(3);

Figure 4-4 shows how the relational database table we created should look after this code has been executed. It should contain one record.

Figure 4-4
Figure 4-4. CABIN table with one cabin record

A client locates entity beans using the findByPrimaryKey( ) method in the home interface. To look up the Cabin bean we just created, we create a primary key of the correct type - in this case, Integer. When we invoke the finder method on the home interface using the primary key, we get back a remote reference to the EJB object. We can now interrogate the remote reference returned by findByPrimaryKey( ) to get the Cabin EJB's name, deckLevel, shipId, and bedCount:

Integer pk = new Integer(1);

CabinRemote cabin_2 = home.findByPrimaryKey(pk);
System.out.println(cabin_2.getName( ));
System.out.println(cabin_2.getDeckLevel( ));
System.out.println(cabin_2.getShipId( ));
System.out.println(cabin_2.getBedCount( ));

We are ready to create and run the Client_1 application. Compile the client application and deploy the Cabin EJB into the container system (see the JBoss Workbook section of this book, Exercise 4.1). Then run the Client_1 application. The output should look something like this:

Master Suite
1
1
3

Congratulations! You just created and used your first entity bean. Of course, the client application doesn't do much. Before going on to create session beans, create another client that adds some test data to the database. Here we'll create Client_2, which is a modification of Client_1 that populates the database with a large number of cabins for three different ships:

package com.titan.cabin;

import com.titan.cabin.CabinHomeRemote;
import com.titan.cabin.CabinRemote;

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;

public class Client_2 {

    public static void main(String [] args) {
        try {
            Context jndiContext = getInitialContext( );
            
            Object ref = jndiContext.lookup("CabinHomeRemote");
            CabinHomeRemote home = (CabinHomeRemote)
                PortableRemoteObject.narrow(ref,CabinHomeRemote.class);
            // Add 9 cabins to deck 1 of ship 1.
            makeCabins(home, 2, 10, 1, 1);
            // Add 10 cabins to deck 2 of ship 1.
            makeCabins(home, 11, 20, 2, 1);
            // Add 10 cabins to deck 3 of ship 1.
            makeCabins(home, 21, 30, 3, 1);
            
            // Add 10 cabins to deck 1 of ship 2.
            makeCabins(home, 31, 40, 1, 2);
            // Add 10 cabins to deck 2 of ship 2.
            makeCabins(home, 41, 50, 2, 2);
            // Add 10 cabins to deck 3 of ship 2.
            makeCabins(home, 51, 60, 3, 2);
            
            // Add 10 cabins to deck 1 of ship 3.
            makeCabins(home, 61, 70, 1, 3);
            // Add 10 cabins to deck 2 of ship 3.
            makeCabins(home, 71, 80, 2, 3);
            // Add 10 cabins to deck 3 of ship 3.
            makeCabins(home, 81, 90, 3, 3);
            // Add 10 cabins to deck 4 of ship 3.
            makeCabins(home, 91, 100, 4, 3);

            for (int i = 1; i <= 100; i++){
                Integer pk = new Integer(i);
                CabinRemote cabin = home.findByPrimaryKey(pk);
                System.out.println("PK = "+i+", Ship = "+cabin.getShipId( )
                    + ", Deck = "+cabin.getDeckLevel( )
                    + ", BedCount = "+cabin.getBedCount( )
                    + ", Name = "+cabin.getName( ));
            }

        } catch (java.rmi.RemoteException re) {re.printStackTrace( );}
          catch (javax.naming.NamingException ne) {ne.printStackTrace( );}
          catch (javax.ejb.CreateException ce) {ce.printStackTrace( );}
          catch (javax.ejb.FinderException fe) {fe.printStackTrace( );}
    }

    public static javax.naming.Context getInitialContext( ) 
        throws javax.naming.NamingException{
    Properties p = new Properties( );
    // ... Specify the JNDI properties specific to the vendor.
    return new javax.naming.InitialContext(p);
    }

    public static void makeCabins(CabinHomeRemote home, int fromId, 
                                  int toId, int deckLevel, int shipNumber)
        throws RemoteException, CreateException {

    int bc = 3;
    for (int i = fromId; i <= toId; i++) {        
        CabinRemote cabin = home.create(new Integer(i));
        int suiteNumber = deckLevel*100+(i-fromId);
        cabin.setName("Suite "+suiteNumber);
        cabin.setDeckLevel(deckLevel);
        bc = (bc==3)?2:3;
        cabin.setBedCount(bc);
        cabin.setShipId(shipNumber);
        }
    }
}

Create and run the Client_2 application against the Cabin EJB we deployed earlier. Client_2 lists all the Cabin EJBs it added to the database:

PK = 1, Ship = 1, Deck = 1, BedCount = 3, Name = Master Suite
PK = 2, Ship = 1, Deck = 1, BedCount = 2, Name = Suite 100
PK = 3, Ship = 1, Deck = 1, BedCount = 3, Name = Suite 101
PK = 4, Ship = 1, Deck = 1, BedCount = 2, Name = Suite 102
PK = 5, Ship = 1, Deck = 1, BedCount = 3, Name = Suite 103
PK = 6, Ship = 1, Deck = 1, BedCount = 2, Name = Suite 104
PK = 7, Ship = 1, Deck = 1, BedCount = 3, Name = Suite 105
...

We now have 100 cabin records in our CABIN table, representing 100 cabin entities in our EJB system. This amount provides a good set of test data for the session bean we will create in the next section, and for subsequent examples throughout the book.

Next week, we'll conclude this two-part excerpt from Chapter 4 of Enterprise JavaBeans, 4th Edition with a look at how to develop a session bean that builds from the examples provided here.


Return to ONJava.com.