Tutorials
EnterPrise Java Beans
Tutorial Index Page

Using the Entity Bean

Look in the "Props.java" file created by the wizard.  This is your "remote" interface.  Recall that the "remote" interface is the way how anybody interacts with the entity bean.

Of course, before interacting with the entity bean, one must find it first!  Unless one is creating a new entity bean.  The interface that allows clients to create and find entity beans, is in PropsHome.java.  Take a look in this file to see what this "home" interface looks like.

Getting the home interface

The first step in using an entity bean is to get a reference to the home object.  This is done via JNDI, as shown:
import javax.naming.*;
import javax.ejb.*;
import javax.rmi.*;
import java.util.*;
import props.*;

// ...
    Properties env = new Properties();
    env.put( "java.naming.factory.initial",
          "desisoft.ejb.client.JRMPFactory" );
    env.put( "desisoft.ejb.nameServer1",
          "localhost:2050" );
    Context ctx = new InitialContext( env );
    PropsHome home =
          (PropsHome) ctx.lookup( "Props" );
Notes:
  • If you are doing this in a JSP page or a servlet in Blazix, you do not need to pass an "env" to the InitialContext constructor, because the default environment is already initialized.  Instead, in a JSP page or a servlet, add the prefix "java:comp/env/ejb/" when doing the lookup, e.g.

  • PropsHome home = (PropsHome)
       ctx.lookup("java:comp/env/ejb/Props");
  • The "localhost:2050" should be replaced by the actual hostname and port number of the EJB server.
  • The last line is show above for simplicity of explanation.  Due to implementation reasons, the correct Java way to do the casting is as follows:

  • Propshome home = (PropsHome)
      PortableRemoteObject.narrow(
            ctx.lookup( "Props" ),
            PropsHome.class );
    To keep the code portable, always use PortableRemoteObject for casting the home interfaces.  (But if you are using JDK 1.2, PortableRemoteObject will not be available, so you must do direct casting without PortableRemoteObject as shown in the example.  You can also download and install IIOP separately, which will make PortableRemoteObject available.)
  • Note that desisoft.ejb.client.JRMPFactory and desisoft.ejb.nameServer1 are Blazix specific strings.  Each EJB container will have its own way of getting the initial context and its own method of locating the EJB server (unless the container requires that the EJB server run bundled with a web-server.)  These properties can be kept in a JNDI properties file for keeping the code container-independent -- JNDI documentation explains how to do this.

Using the entity bean

Once you have a home interface, creating or finding a bean is simple, just call the appropriate create or find method from the home interface.

When you create or find a bean, you get the bean's "remote" interface back.

To use the bean, now just call methods in the bean's remote interface.

The client code

There is a sample file included here to show how to put all this together.  There are two versions of the file, EntityClientSample.java for JDK 1.2 and EntityClientSample.java for JDK 1.3.  The only difference between these two versions is that the JDK 1.2 version does not use PortableRemoteObject.

To compile and use this file, you need to add the EJB jar file C:\Ejbtut\PropsEjb.jar in your classpath.  (Do not place the client code in C:\Ejbtut to avoid confusion with the existing classes there!)

Usage of this class is shown below:

java EntityClientSample put name myname
java EntityClientSample get name
java EntityClientSample put name anotherName
java EntityClientSample get name
(The EJB server must be running when you try these commands.)

The first command above puts the value "myname" on the property "name".  The second command retrieves the value on the property "name".  The third command changes the property, and the fourth command retrieves the new property.

You can put a property on one machine, and then access it from another machine!  And once you put the property, even when you stop the program and turn your machine off the property will stay that way, so when you restart the program and lookup the property you will get the same value back.  You can even stop and restart the server, and entity bean object will be there when the server is restarted.

Concepts

The entity bean created has two fields, "key" and "value".  The EJB framework guarantees that the fields of any given bean will be persistent, and will stay the same until changed.

This sample provides a very simple entity bean where the fields are returned un-changed to the client of the bean, and the only bean methods are getters and setters.  But entity beans can provide any other methods besides getters and setters.  While the underlying fields will be persistent, any necessary computation can be performed on the fields before returning a result to the client.

Exercise:   Write a client program for the employees EJB you have created.  This program should be able to (a) add new employees, given their SSN and other information (b) retrieve and print information about an employee, given the SSN, (c) change the information for an employee, given the SSN and the new information (restrict "c" to only one field, e.g. salary, the idea is just to make sure you are familiar with the capabilities.)

Troubleshooting Tips

Next tutorial: Finder Methods