Solutions Products Services

Search
Download Contact Us
Documentation Site map
Support About Us
E-commerce SolutionsiPlanet



Creating a Container Managed Persistence Enterprise Java Bean

The container managed persistence EJB part of this tutorial is performed using the Oracle 8.1.6 database client using the Oracle JDBC driver. As a result, you must have an access to an Oracle database to run the example created in this section.

In this section of the tutorial you will:
 

Create a Table in an Oracle Database

The sample container-managed entity bean is stored in the database for persistence. Here are SQL statements that will create a table, named obcustomer, where the bean instances will be stored:

CREATE TABLE obcustomer
(
    ssn                 char(9)      NOT NULL ,
    username            varchar2(10) NOT NULL ,
    prefix              varchar2(4)  NULL     ,
    firstname           varchar2(30) NOT NULL ,
    middlename          varchar2(30) NULL     ,
    lastname            varchar2(30) NOT NULL ,
    address             varchar2(30) NOT NULL ,
    address2            varchar2(30) NULL     ,
    city                varchar2(25) NOT NULL ,
    state               char(2)      NOT NULL ,
    zip                 varchar2(9)  NOT NULL ,
    homephone           char(10)     NOT NULL ,
    workphone           char(10)     NOT NULL ,
    constraint PK_obcustomer primary key (ssn)
);
commit;

You may use any application of your choice, including Oracle SQL*Plus, to exeute the statements above.

Configure Oracle JDBC driver on iPlanet Application Server Version 6.5

In this subsection, you will configure Oracle JDBC database driver on iPlanet Application Server, naming the driver identifier ora-type4. If you already have configured Oracle JDBC driver, you may skip this section.

To configure Oracle JDBC driver on Solaris:

  1. Go to iasinstall/ias/bin, and run the following script:
  2. db_setup.sh

  3. Specify the directory where iPlanet Application Server has been installed, for example
  4. /usr/iplanet/ias65. Press Enter.

  5. Type 1 to configure 3rd party JDBC drivers.
  6. Enter ora-type4 for the driver identifier.
  7. Enter driver class name, for example Oracle.Jdbc.driver.OracleDriver
  8. Enter the driver class path, for example /Oracle/Ora81/jdbc/lib/classes12.zip
  9. Press Enter to leave the native driver directory blank.

To configure Oracle JDBC driver on Windows

  1. Go to iasinstall/ias/bin, and run the following program:
  2. jdbcsetup.exe

  3. In the Add tab of the Third Party JDBC Configuration window, specify a ora-type4 for Driver Identifier.
  4. Enter the driver class path for Driver Classpath, for example
  5. C:\Lang\Java\Oracle\lib\classes12.zip

  6. Enter oracle.jdbc.pool.OracleConnectionPoolDataSource for PooledDatasource class name.
  7. The window should look similar to below:

  8. Select the Add button to add the Oracle JDBC driver setting.
  9. Select the OK button to close the window.

Create the Container Managed Persistence EJB

In this subsection, you will create a Container-Managed Persistence EJB. To create the EJB:

  1. From the Explorer Filesystems node, right click to select Mount Directory.
  2. In this example, the directory is named EJB under the CMPExample directory.

  3. Right click on the EJB directory and select New Package.

  4. In the Create New Package dialog, enter customer in the name field.
  5. Right click on the Customer package node and select New | EJB | Entity Bean.
  6. When the New From Template Wizard dialog appears, for Entity EJB Type, select the Container Managed Persistence and the Select Database Table radio button, then click Next.
  7.  

  8. If you have an Oracle database connection, select it here. If not, then select Add Connection.
  9.  


     

  10. In the New Database Connection dialog enter the following data particular to your configuration:
  11. Name: select Oracle thin
    Driver: oracle.jdbc.driver.OracleDriver should already be displayed
    Database URL: jdbc:oracle:thin:@hostname:portnumber:servicename
    User Name: enter the username defined for the database connection
    Password: enter the password defined for the database connection

  12. Select OK. The database connection should be created and displayed.
  13. Expand the database connection node and Tables node.
  14. Select the OBCUSTOMER table and select Next.
  15.  

  16. Select the SSN field and select Edit.
  17.  

  18. Change the field name to customerID and select OK.
  19.  

    The Java Fields Name for selected entry is changed to customerID.
     

  20. Select Next.
  21.  

  22. Accept the default settings and select Finish to generate the CMP Enterprise Java Bean.
  23.  

    Once the CMP EJB is created, the following files are available from Explorer (Filesystems) window:

    Where:

    is the Obcustomer (EJB) node
    is the Obcustomer remote interface
    is the Obcustomer bean class
    is the Obcustomer home interface

    A reminder message displays in the output window prompting you to set the resource references and JNDI name.

Set the Resource Reference and JNDI Name

Resource factory reference is an external reference to a resource, such as a database connection, that must be created so an enterprise bean can perform its work. The enterprise bean accesses the resource by making an JNDI lookup when it is running. In this subsection, you will add a new resource factory reference for the database connection whose name is dbsol0. Later in Register the Datasource, you will connect the name dbsol0 to the ora-type4 driver.

To add the reference:

  1. Select the  node.
  2. Right click and select Properties.
  3. From the Properties Editor, select the References tab.
  4. Click in the text entry area of the Resource Factory References field to call up the ellipsis   button; select the button.
  5.  

  6. Select Add.

  7.  

  8. From the Standard tab, enter the following data for each field:
  9. Name: the name of your datasource (in this example jdbc/dbsol0 is used)
    Description: any description you wish
    Type: select javax.sql.DataSource
    Authorization: select container

    The J2EE RI tab is not applicable for cmp EJBs.

  10. Select OK.
  11. The resource reference entries are then displayed in the Properties editor.

  12. Select OK.
  13. Select the iPlanet AS tab.
  14. Click in the text entry area of the Resource Reference Mappings to call up the ellipsis   button, and select the button
  15. Enter jdbc/dbsol0 in the JNDI Name field.
  16. Select OK.
  17. Close the Obcustomer (EJB) Properties window.

Map CMP fields and add CMP Business Methods

In this subsection you will map map the database fields to EJB fields and write business methods for accesing EJB fields. Follow the instruction below:

  1. From the Explorer, double click on the  to view it in the Source Editor.
  2. Scroll to locate the Container managed fields.
  3. Edit the address field to addressLine1.
  4. Edit the address2 field to addressLine2.
  5.  

  6. Right click within the Source Editor and save the file.
  7. From the Explorer, select the  node.
  8. Right click to select Properties.
  9. Select the iPlanet AS tab and select the Map DB to EJB Fields  button.
  10.  

  11. Double click on the address and address2 EJB Field Name to edit each field to addressLine1 and addressLine2, respectively.
  12.  

    Through this panel you can change the mapping settings of each field in your database table. If you edit the mappings here, you must also edit the corresponding Java file.

    NOTE: If the database changes, or you change the mapping information, you can edit the mappings through this panel.

     

  13. Select OK.
  14. From the Explorer, select the  node.
  15. Right click and select New Business Method.
  16. Enter the following data for each field of the New Business Method dialog:
  17. Name: setUsername
    Return Type: void
    Method Parameters: Select Add and enter the following data in the Enter Method Parameter dialog.

    Field name: Username
    Type: java.lang.String


     

  18. Select OK to dismiss the Method Parameters dialog.
  19. The New Business Method dialog should appear as follows:

  20. Select OK to dismiss the New Business Method dialog.
  21. Select the  node again and right click to select New Business Method.
  22. Enter the following data for each field:
  23. Name: getUsername
    Return Type: java.lang.String

    The New Business Method dialog should appear as follows:


     

  24. Select OK to dismiss the New Business Method dialog.
  25. Select the  node once again and right click to select New Business Method.
  26. Enter the following data for each field:
  27. Name: getFirstname
    Return Type: java.lang.String

    The New Business Method dialog should appear as follows:


     

  28. Select OK to dismiss the New Business Method dialog.
  29. Select the  node for the last time and right click to select New Business Method.
  30. Enter the following data for each field:
  31. Name: getLastname
    Return Type: java.lang.String

    The New Business Method dialog should appear as follows:


     

  32. Select OK to dismiss the New Business Method dialog.
  33. From the Explorer, double click on the  node to open it in the Source Editor.
  34. Verify that the business methods you just created have been added to the source code.
  35. Double click on the  node to open it in the source editor.
  36. Add the following create() method at the end of the file.
  37.  public Obcustomer create (String customerID, String username,
            String prefix, String firstname, String middlename, 
            String lastname, String addressLine1, String addressLine2, 
            String city, String state, String zip, String homephone, 
            String workphone)
        throws CreateException,RemoteException;
  38. Save the file.
  39. Double click on the  bean class to open it in the source editor.
  40. Verify that the business methods you created are in the bean class.
  41. Additionally, verify that the ejbCreate() and ejbPostCreate() methods are also created.
  42.  

     

  43. Add the following code for each business method:

  44.  
  45. Save the file.
  46. You are now ready to add the finder methods.

Add the CMP Finder Methods

In this subsection, you will add 2 new finder methods, named findAll and findByFirstName, in addition to automaticall generated findByPrimaryKey method. A call to findAll will retrieve all Obcustomer instances. With findByFirstName, you can retrieved EJB instances with the specified firstname. To create the finder methods:

  1. Select the  node.
  2. Right click and select New Finder Method.
  3. Enter the following data in each field of the Finder Method dialog:
  4. Name: findAll
    Return type: select java.util.Collection

    The New Finder Method dialog should appear as follows:


     

  5. Select OK.
  6. Select the  node.
  7. Right click and select New Finder Method.
  8. Enter the following data in each field of the Finder Method dialog:
  9.  

    Name: findByFirstName
    Return type: select java.util.Collection
    Method Parameters: select Add and enter the following data in the Method Parameters dialog:

    Field name: firstname
    Type: java.lang.String

     
  10. Select OK to dismiss the Enter Method Parameter dialog.
  11. The Add a new Finder Method dialog should appear as follows:


     

  12. Select OK.
  13. Expand the  node.
  14. Expand the  node.
  15. Select the  node and right click on Properties.
  16. Click the iPlanet AS tab.

  17.  

     
     

  18. Click in the text entry area of the Finder Fields Mapping Tool field to call up the ellipsis   button; select the button
  19. The Property Editor: Finder Parameters Mapping Tool dialog is displayed.

     
     

     
     

  20. Click in the Cmp Field Name text area and select the firstname option from the drop down list.
  21. Select OK to close the dialog.
  22. You are now ready to validate and compile the EJB Classes.
     

Validate and Compile the EJB Classes

The IDE can run a validation check on an enterprise bean to ensure that it matches the J2EE specification. You can run a validation check after doing development work on a bean before you compile it; the validation check will catch J2EE errors not caught by the compiler.

In this subsection, you will validate the Obcustomer EJB, and compile it. To validate and compile:

  1. Select the  node.
  2. Right click and select Validate EJB.
  3. Once your EJB is validated, a Finished message is displayed in the status bar.
     

  4. Select the  node.
  5. Right click to select Compile EJB Classes.
  6. Once compilation is complete, the message Finished Obcustomer (EJB) is displayed in the status bar.
     

  7. Select the  node again.
  8. Right click and select New EJB Module...
  9. Select OK to accept defaults.
  10. You are now ready to register the datasource with the iPlanet Application Server.

Register the Datasource with the iPlanet Application Server

If your module/application depends on a data source, you need to add the datasource and register it so that when it is deployed to the iPlanet Application Server the identfied datasource is used.

  1. From the Explorer, select the Runtime tab.
  2. Expand the Server Instance node.
  3. Select the Data Sources node and right click to select the Add new Data Source...
  4. Enter the following data for each field:
  5. DataBase the server name (required only for a Sybase server; leave it as displayed)
    Database URL jdbc:oracle:thin:@hostname:portnumber:servicename
    DataSource your datasource name (in this example, dbsol0 is used)
    Driver the registered driver name (ora-type4 was created earlier in this tutorial)
    User Name yourusername
    Password yourpassword


     

  6. Select the datasourcename node and right click to select Register from the context menu.
  7. Select the server instance you want to register this datasource to.
  8. Select Register.
  9. After a few moments, the Output window iPlanet Deployment Tab displays a message indicating that deployment is finished.

    Also, the status message window displays the message Deployment to iPlanet Application Server finished.

    You are now ready to create a servlet to call your Container Managed Persistence EJB.

· Create a Servlet that uses CMP EJB

In this subsection, you will create a servlet that will use the Obcustomer EJB. The servlet creates Obcustomer instances and looks them up with the findByPrimaryKey, findByFirstName, and findAll methods. To create the servlet:

  1. From the File menu, select New.
  2. Expand the JSP & Servlet node, select Web Module, then select Next.
  3. Select the  button to create a new directory.
  4. Create a directory named EJBClient under the CMPExample directory and select Add.
  5. Select Finish.
  6. You may see the following dialog informiing you of an alternate project view.

  7. Select OK to dismiss the dialog.

  8.  
  9. From the Explorer, expand the EJBClient node and the WEB-INF node.
  10. Right click on the Classes node and select New Package from the context menu.
  11. In the Create New Package Name field enter cmpPack and select OK.
  12. Right click on the cmpPack node and select New -> JSP & Servlet -> Servlet.
  13. Enter ObClient in the name field and select Finish.
  14. The Source Editor opens the ObClient.java file.
     

  15. Add the following import statements to the ObClient.java file.

  16. import javax.ejb.*;
    import javax.naming.*;

    import java.rmi.*;
    import java.util.*;
    import customer.*;
     

    Your ObClient.java file should look like this:
     


     

  17. Edit the processRequest() method as follows:
  18. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter();
        String JNDI = "java:comp/env/Obcustomer";
        Object beanObject = null;
        out.println("<html>");
        out.println("<head>");
        out.println("<title>ObClient Servlet Output</title>");  
        out.println("</head>");
        out.println("<body>");
        try
        {
           InitialContext ctx = new InitialContext();
           beanObject = ctx.lookup(JNDI);
           System.out.println("Lookup OK");
           ObcustomerHome home = (ObcustomerHome) beanObject;
           System.out.println("before emptying database");
           Collection c = (java.util.Collection) home.findAll();
           Iterator i = c.iterator();
           while (i.hasNext()) {
             Obcustomer cust =( Obcustomer)i.next();
             cust.remove();
           }
           System.out.println("Before Home.create");
           Obcustomer remote = (Obcustomer) home.create("987654321","SRoberts","Mr","Steve","o","Roberts","Main Street","Suite 20","Santa Clara","CA","95054","4081112222","4082223333");
           out.println("<h1>CMP Bean Sample Application<br /></h1>");
           out.println("<h5>Inserting the Record for Steve Roberts in DB: ");
           out.println("FirstName : " + remote.getFirstname()+ "<br />");
           out.println("LastName : " + remote.getLastname()+ "<br />");
           out.println("UserID : " + remote.getUsername()+ "<br />");
           out.println("<P> Changing the UserID for Steve Roberts from " + remote.getUsername() + " to " + "Roberts :</P>");
           remote.setUsername("Roberts");
           out.println("Accesing DB for UserID for Steve Roberts: <br />");
           out.println("The UserID for Steve Roberts now is : " + remote.getUsername()+ "<br />");
           remote = (Obcustomer) home.create("876543219","LHall","Mr","Larry","M","Hall","South Street","#89","Santa Clara","CA","95054","4088889999","4086667777");
           remote = (Obcustomer) home.create("789123456","LAllen","Mrs","Lora","C","Allen","First Street","# 1895","Santa Clara","CA","95054","4083334444","4085558888");
           remote = (Obcustomer) home.create("678912345","LReeves","Mrs","Lora","K","Reeves","Market Street","# 6677","Santa Clara","CA","95054","4087779999","4088880098");
           System.out.println("Now calling findByPrimaryKey");
           Obcustomer primKey = home.findByPrimaryKey("876543219");
           System.out.println("primKey: "+ primKey.toString()+"\n");
           String id = (String)primKey.getPrimaryKey(); 
           System.out.println(id);
           out.println("<P> Using findByPrimaryKey : Getting User record for SSN = " + id);
           out.println("FirstName : " + primKey.getFirstname()+"<br />");
           out.println("LastName : " + primKey.getLastname()+"<br />");
           out.println("UserID : " + primKey.getUsername()+"<br />");
           System.out.println("calling findByFirstname");
           c = home.findByFirstName("Lora");
           i = c.iterator();
           out.println("<P> Using findByFirstname : Getting User record for : Lora");
           while (i.hasNext()) {
              Obcustomer cust = (Obcustomer)i.next();
              String customerId = (String)cust.getPrimaryKey();
              String firstname = cust.getFirstname();
              String lastname = cust.getLastname();
              out.println(customerId + ": " + firstname + " " + lastname + "<br />");
            }
            System.out.println("calling findAll");    
            out.println("<P> Calling findAll");
            c = (java.util.Collection) home.findAll();
            i = c.iterator();
            while (i.hasNext()) {
               Obcustomer cust =( Obcustomer)i.next();
               String customerId = (String)cust.getPrimaryKey();
               String firstname = cust.getFirstname();
               String lastname = cust.getLastname();
               out.println(customerId + ": " + firstname + " " + lastname + "<br />");
             }
         } catch (Exception ex) {
               System.err.println("Caught an exception." );
               ex.printStackTrace();
         }
         out.println("</body>");
         out.println("</html>");
         out.close();
    } 
    
  19. Save the file.
  20. Select the web.xml node and select Properties.
  21. Select the References tab.
  22. Select the EJB references field.
  23. From the EJB References dialog select Add.

  24.  
  25. Enter the following data in each field of the Add EJB Reference dialog:
  26. Reference Name: Obcustomer
    Description: any description you like
    Referenced EJB Name: Obcustomer
    Type: Entity
    Home Interface: customer.ObcustomerHome
    Remote Interface: customer.Obcustomer

  27. Select iPlanet App Server tab and specify JNDI Name: ejb/EJBModule_Obcustomer/Obcustomer
  28.  
     

  29. Select OK.
  30. The EJB References dialog should appear as follows:
     


     

  31. Select OK.
  32. Expand the cmpPack folder node.
  33. Right click on the ObClient node and select Compile.
  34. Right click on the ObClient node and select Properties.
  35. Specify URI field as /ObClient
  36. Right click on the web.xml node and select Properties.
  37. Select Servlet field. The Property Editor: Servlets dialog is displayed.
  38. Select the entry and select Edit. The Edit Servlet dialog is displayed.
  39. Specify Servlet Name = ObClient, Display Name = ObClient, and Servlet Class = cmpPack.ObClient
  40. Select Mappings. The Edit Servlet Mappings dialog is displayed.

  41.  

     
     

  42. Select the entry and select Edit.
  43. Specify Servlet Name = ObClient and URL Pattern = /ObClient
  44. Select OK for both dialogs
  45. Select OK on Edit Servlet dialog to accept the changes.
  46. Select OK on Property Editor: Servlet dialog.
  47. Select Welcome Files fields and remove all files.
  48. Select the WEB-INF node and right click on Properties. Specify Context Root field as EJBClient.

Create and Run a J2EE Application

In this subsection, you will create, deploy, and run a J2EE application. The application contains the Obcustomer EJB and ObClient servlet created previously. To run the sample application:

  1. Mount directory CMPExample.
  2. Select CMPExample node, right click and select New | J2EE | Application.
  3. Enter name as CMPExample and then select Finish.
  4. Select the CMPExample application node, right click and select Add Module... Navigate to the EJBModule_Obcustomer node, select and click OK to add EJB module.
  5. Repeat the previous step to add EJBClient web module, select WEB-INF node and click OK to add web module.
  6. Select CMPExample application node, right click and select Deploy.
  7. The Progress Monitor dialog is displayed. It will disappear when the deployment process is finished.

  8. Right click on the ObClient servlet node of the EJBClient web module and select Execute.
  9. The web browser launches and displays the following screen.


 
 

The next step in the iPlanet Application Server Integration Module Tutorial is deploying and executing the CDShopCart Forte for Java.

 
   

Legal Notices


Search