Java Reference
In-Depth Information
The home interface controls the life cycle of the EJB objects. For a session bean, the purpose of the
home interface is to define the create methods that a remote client may invoke to create its reference
to the EJB object. You may define multiple create methods with different signatures. The default
method without any argument is used to instantiate EJB objects in the container.
Note that create methods are different from constructors. A constructor is an initializer for an object
(which may exist for a very long time). A create method is used by clients to initialize an EJB instance
in an EJB container. An EJB instance may be composed of one object or a variety of objects over its life
cycle. As such, it has different initialization mechanisms.
Understanding the life cycle is critical in mastering EJBs. Unfortunately, that is beyond the scope of this
book. The interested reader can find extensive discussions on EJB life cycles in numerous EJB topics.
Note
Do not assume that create methods are the same as constructors.
As is the case for the remote interface, the signatures of the create methods defined in the home
interface must correspond to those of its corresponding ejbCreate methods in the implementation
class. The throws clause of the create method must include java.rmi.RemoteException and
the javax.ejb.CreateException . The home interface of the HelloEJB is shown in Listing 20-2 .
Only one create method is defined in this example.
Listing 20-2: Home interface of HelloEJB
package java_database.ch20.HelloSLBean;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface HelloHome extends EJBHome {
public Hello create() throws CreateException, RemoteException;
}
Implementation class
Most of the work that you have to do as a bean developer occurs in the bean class itself. There are a
number of methods the bean class must provide. An important method, and perhaps the most confusing
one, is ejbCreate .
Because an enterprise bean runs inside an EJB container, a client cannot directly instantiate the bean.
Only the EJB container can instantiate an enterprise bean. During instantiation, the example program
performs the following steps:
1. The client invokes a create method on the home object
2. The EJB container instantiates the EJB instance.
3. The EJB container invokes the appropriate ejbCreate method in the implementation class;
typically, an ejbCreate method initializes the state of the EJB instance.
create and ejbCreate method guidelines
Typically, an ejbCreate method initializes the state of the EJB instance. The guidelines for writing
such methods are:
 
Each create method defined in the home interface must have a corresponding ejbCreate
method in the bean-implementation class.
 
The number of arguments and argument data types between the ejbCreate and the
corresponding create methods must be the same.
Search WWH ::




Custom Search