The first method called by the container after a bean instance is created is setEntityContext( ). This method passes the bean instance a reference to its javax.ejb.EntityContext, which is really the instance’s interface to the container.

The setEntityContext( ) method should be implemented by the entity bean developer so that it places the EntityContext reference in an instance field of the bean where it will be kept for the life of the instance. The definition of EntityContext is as follows:

public interface javax.ejb.EntityContext extends javax.ejb.EJBContext {
    public EJBLocalObject getEJBLocalObject( ) throws IllegalStateException
    public abstract EJBObject getEJBObject( ) throws IllegalStateException;
    public abstract Object getPrimaryKey( ) throws IllegalStateException;

As the bean instance is swapped from one EJB object to the next, the information obtainable from the EntityContext changes to reflect the EJB object to which the instance is assigned. This change is possible because the EntityContext is an interface, not a static class definition, so the container can implement the EntityContext with a concrete class that it controls. As the entity bean instance is swapped from one EJB object to another, the information made available through the EntityContext will also change.

The EntityContext.getEJBObject( ) method returns a remote reference to the bean instance’s EJB object. The EntityContext.getEJBLocalObject( ) method, on the other hand, returns a local ...

Get Enterprise JavaBeans, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.