Lesson 6 | The entity bean's EJBObject |
Objective | Review the code for the EJBObject. |
entity bean's EJBObject
Structure and Purpose
The EJBObject for entity beans performs a function similar to that performed for session beans. Its structure and purpose are similar. The stub to the EJBObject is returned from the home object's create()
and findByPrimaryKey()
methods. The code for the Customer EJBObject is as follows:
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Customer extends EJBObject {
public void setName(String name) throws RemoteException;
public String getInfo() throws RemoteException;
}
Business methods in the bean
The business methods in the bean are invoked like they are for session beans. The methods in the Customer bean are as follows:
. . .
private String custnum = null; // customer number
private String name = null; // customer name
private EntityContext context;
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return "Customer Number = " + custnum +
" Name = " + name;
}
. . .
Note that the methods do nothing more than access the instances of the variables, either setting or getting their values. The work of loading and storing the data to and from the entity object is left to the container callbacks ejbLoad()
and ejbStore()
. These are covered in later lessons.
EJBObject methods
The EJBObject interface contains the same methods as the EJBObject for session beans. These were covered in the first course and are shown
here for review purposes only. The method getPrimaryKey()
, not valid for session beans, is the way the client retrieves its
primary key. This is useful in a situation where a bean instance is passed a remote reference to a bean's EJBObject from some other
program or client and wants to know its primary key.
public interface EJBObject extends java.rmi.Remote {
EJBHome getEJBHome();
Handle getHandle();
java.lang.Object getPrimaryKey();
boolean isIdentical(EJBObject obj);
void remove();
}
The next lesson examines entity bean container callbacks.