Entity beans have ejbPostCreate() methods that are invoked by the container after the corresponding ejbCreate() method. For every ejbCreate(args...) there is a corresponding ejbPostCreate(args...) method. The post create method has the same parameters as its matching ejbCreate() method but returns a null instead of returning the primary key.
The code below illustrates this:
public void ejbPostCreate(String custnum) {
// perform any additional initialization here
}
Motive for the method
Why do we have this method? The bean may want to pass a remote reference to itself to another bean. In the ejbPostCreate()
method, it can connect to the bean and pass its remote reference. Why cannot this be done in the ejbCreate() method?
Let me explain. To pass a reference to itself, the bean needs the reference of the EJBObject. This is normally acquired from the
EntityContext using the getEJBObject() method. However, in the ejbCreate() method, the EntityContext does not exist. To understand why, we need to review what happens after the ejbCreate() returns.
The return from the ejbCreate() is the primary key of the newly created entity. At the point when the primary key is returned
from the bean, the container, then and only then, creates the EJBObject and associates it with the primary key. Following this, it invokes the
setEntityContext() method on the bean instance. Now that the EntityContext is set, methods in the bean can retrieve their
EJBObject references. Thus the container invokes the ejbPostCreate() so that the bean can get and use the EJBObject reference.
If it did not exist, there would no way to get the reference until another business method was called. View the diagram.
In the next lesson you will learn to use the primary key to find persistent objects.