Lesson 2 | The entity bean home interface and the create() methods |
Objective | Program the entity bean create() methods. |
Entity Bean Home interface
The home interface performs a similar function for both 1) entity beans and 2) session beans.
There is a single home interface for a particular bean in a container.
The home interface allows the client to create and find bean instances.
It extends EJBHome in the same way as for session beans. The code below shows the import statements and class signature:
import javax.ejb.CreateException;
import java.rmi.RemoteException;
import javax.ejb.*;
import java.util.*;
public interface CustomerHome extends EJBHome {
. . .
The methods of the EJBHome were covered under session beans in
the first course
in this series and do not change here.
They are:
// EJBHome is provided as part of the EJB API.
public interface EJBHome extends Remote {
void remove(javax.ejb.Handle handle) throws
RemoteException;
void remove(PrimaryKey key) throws RemoteException;
javax.ejb.EJBMetaData get EJBMetaData()throws
RemoteException;
HomeHandle getHomeHandle() throws RemoteException;
}
remove(PrimaryKey) for Entity Beans
These methods behave in exactly the same manner as they do with session beans. However, the remove(PrimaryKey)
method was not valid for session beans but is valid for entity beans. Entity names can be confusing. An entity object is the underlying persistent data object. An instance, or entity bean instance, is an EJB that represents the in-memory representation of the underlying entity object.
Create() methods
The create()
methods in the Home interface are invoked by the client when it wishes to create a new persistent entity object. Its arguments represent, at a minimum, enough information to create a unique persistent entity object. There can be zero or more create()
methods. If there are none, then the client has no way of creating a bean and must use existing entity objects. This is
a valid situation, as not all applications need to create new entity objects. The create()
methods return an EJBObject stub in the same way as session bean create()
methods. The create()
methods in the home interface for the Customer bean are as follows:
public interface CustomerHome extends EJBHome {
public Customer create(String custnum) throws
CreateException, RemoteException;
public Customer create(String custnum, String name)
throws CreateException, RemoteException;
In the example, the first create()
method will create an empty row for a specific customer. The second create()
method creates a complete row. There may be many arguments in some create()
methods if the persistent entity is large.
The next lesson explains how the create()
methods in the home interface map to the ejbCreate()
methods in the bean.