October 27, 2016
Hot Topics:

Developing a CMP Bean

  • July 24, 2003
  • By Saleem Siddiqui, Michael Landy, & Jeff Swisher
  • Send Email »
  • More Articles »

Building Entity Beans with JBuilder

You may look at the amount of code you need to accomplish a simple task, but JBuilder offers a number of time-saving features to make this process efficient. These features offer a number of capabilities regardless of the EJB vendor you are using. The following are some of those features:

Many of these features were discussed in Chapter 22, "Developing Session Beans." We will concentrate on the ones that specialize in entity beans. We will build the same example we built earlier to access our Employee entity and wrap the access with a session façade.

  1. Create a project by choosing File, New Project from the menu. Call the project EntityBeanSample. After you create the project, make sure that the enterprise server is selected using the Project Properties Editor. If you set it as the default for all projects, it will not need to be set.

  2. Create a new EJB module called EntityBean. Make sure the version property is set to EJB 2.0 compliant.

  3. Double-click on the new EJB module node. This loads the EJB Designer.

  4. Right-click on the data sources in the button on the left pane of the IDE and select Import Schema from Database. This process loads all the information from an existing data structure, as shown in Figure 4.

  5. Figure 4

    Import data source definition from external database.

  6. Fill in the connection information to your data source. In this case, use the employee jDatastore database that ships with JBuilder. This process loads the entire schema from the specified database. This is accomplished by clicking on the Choose Existing Connection button.

  7. Right-click on the table Employee and create a CMP 2.0 Entity Bean. This creates an entity bean configured to manage the persistence of the entity to the Employee table.

  8. Now place both a local and remote interface on the new entity bean. This is accomplished by selecting the bean in the Designer, and then changing the interface's property to local/remote.

  9. Let's look at what was generated by JBuilder and make sure that you understand the purpose of each generated file (see Table 1).

    Table 1 Files Generated when Creating a CMP Entity Bean






    Interface for providing the accessors and mutators to the attributes



    Home interface for creating, finding, and removing an entity



    Implementation class for CMP beans



    Interface for providing the accessors and mutators to the attributes for remote usage



    Home interface for creating, finding, and removing an entity for remote usage

  10. Compile your application to create a JAR for your EJB entity beans.

  11. Add to the project a test client generated for our new Employee entity bean, or write your own client as seen in Listing 10.

  12. Listing 10 Test Client to Verify Your Entity Bean Operations

     public EmployeeTestClient() {
      long startTime = 0;
      if (logging) {
       log("Initializing bean access.");
       startTime = System.currentTimeMillis();
      try {
       //get naming context
       Context ctx = new InitialContext();
       //look up jndi name
       Object ref = ctx.lookup("EmployeeRemote");
       //cast to Home interface
       employeeRemoteHome = (EmployeeRemoteHome) PortableRemoteObject.narrow(ref, EmployeeRemoteHome.class);
       if (logging) {
        long endTime = System.currentTimeMillis();
        log("Succeeded initializing bean access.");
        log("Execution time: " + (endTime - startTime) + " ms.");
       /* Test your component Interface */
       this.findByPrimaryKey(new java.lang.Short("5"));
      catch(Exception e) {
       if (logging) {
        log("Failed initializing bean access.");
  13. Choose Tools, Borland Enterprise Server Management Agent from the menu.

  14. Right-click and run either your JAR file or your EAR. This launches a version of the enterprise server inside JBuilder.

  15. Run your test application and verify the exercising of the entity bean (see Listing 11).

  16. Listing 11 Results of the Test Client Exercising Your Beans Methods

    -- Initializing bean access.
    -- Succeeded initializing bean access.
    -- Execution time: 2944 ms.
    -- Calling findByPrimaryKey(5)
    -- Succeeded: findByPrimaryKey(5)
    -- Execution time: 1452 ms.
    -- Return value from findByPrimaryKey(5): Stub[repository_id=RMI:entitybeansample.EmployeeRemote:000000 ...
    -- Calling getEmpNo()
    -- Succeeded: getEmpNo()
    -- Execution time: 40 ms.
    -- Return value from getEmpNo(): 5.

About the Authors

Saleem Siddiqui is a technical architect and trainer with Dunn Solutions Group. He is also a Sun Certified Developer and a Borland Certified JBuilder Instructor. At Dunn, he provides consulting services and business application development.

Michael Landy is Director of Business Applications for Dunn Solutions Group directing the strategic solutions for clients.

Jeff Swisher is a technical manager for the Dunn Solutions Group business applications. He works as a programmer, trainer, architect, and instructional designer. He is a Sun Certified Java Developer and a Borland Certified Instructor in JBuilder.

Source of this material

This material is from Chapter 23: Developing Entity Beans from the book JBuilder Developer's Guide (ISBN: 0-672-32427-X) written by Saleem Siddiqui, Michael Landy, and Jeff Swisher, published by Sams Publishing.

To access the full Table of Contents for the book.

Other Chapters from Sams Publishing:

Web Services and Flows (WSFL)
Overview of JXTA
Introduction to EJBs
Processing Speech with Java
The Java Database Control in BEA Weblogic
Databases and Tomcat
Working with JAX-RPC
Understanding Servlets

Page 4 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel