The EJB implementation class implements the bean's business methods of the component interface and the methods dedicated to the EJB environment, the interface of which is explicitly defined in the EJB specification. The class must implement the javax.ejb.EntityBean interface, must be defined as public, cannot be abstract for CMP 1.1, and must be abstract for CMP 2.0 (in this case, the abstract methods are the get and set accessor methods of the bean's cmp and cmr fields). Following is a list of the EJB-environment dedicated methods that the EJB provider must develop.
The first set of methods are those corresponding to the create and find methods of the Home interface:
public PrimaryKeyClass ejbCreate(...);
This method is invoked by the container when a client invokes the corresponding create operation on the enterprise Bean's home interface. The method should initialize instance's variables from the input arguments. The returned object should be the primary key of the created instance. For bean-managed persistence, the bean provider should develop here the JDBC code to create the corresponding data in the database. For container-managed persistence, the container will perform the database insert after the ejbCreate method completes and the return value should be null.
public void ejbPostCreate(...);
There is a matching ejbPostCreate method (same input parameters) for each ejbCreate method. The container invokes this method after the execution of the matching ejbCreate(...) method. During the ejbPostCreate method, the object identity is available.
public <PrimaryKeyClass or Collection> ejbFind<method> (...); // for bean managed persistence only
The container invokes this method on a bean instance that is not associated with any particular object identity (some kind of class method ...) when the client invokes the corresponding method on the Home interface. The implementation uses the arguments to locate the requested object(s) in the database and returns a primary key (or a collection thereof). Currently, collections will be represented as java.util.Enumeration objects or java.util.Collection. The mandatory FindByPrimaryKey method takes as argument a primary key type value and returns a primary key object (it verifies that the corresponding entity exists in the database). For container-managed persistence, the bean provider does not have to write these finder methods; they are generated at deployment time by the EJB platform tools. The information needed by the EJB platform for automatically generating these finder methods should be provided by the bean programmer. The EJB 1.1 specification does not specify the format of this finder method description; for JOnAS, the CMP 1.1 finder methods description should be provided in the JOnAS-specific deployment descriptor of the Entity Bean (as an SQL query). Refer to Section 8.7 Configuring Database Access for Container-Managed Persistence. The EJB 2.0 specification defines a standard way to describe these finder methods, that is, in the standard deployment descriptor, as an EJB-QL query. Also refer to Section 8.7 Configuring Database Access for Container-Managed Persistence. Then, the methods of the javax.ejb.EntityBean interface must be implemented:
public void setEntityContext(EntityContext ic);
Used by the container to pass a reference to the EntityContext to the bean instance. The container invokes this method on an instance after the instance has been created. Generally, this method is used to store this reference in an instance variable.
public void unSetEntityContext();
Unset the associated entity context. The container calls this method before removing the instance. This is the last method the container invokes on the instance.
public void ejbActivate();
The container invokes this method when the instance is taken out of the pool of available instances to become associated with a specific EJB object. This method transitions the instance to the ready state.
public void ejbPassivate();
The container invokes this method on an instance before the instance becomes dissociated with a specific EJB object. After this method completes, the container will place the instance into the pool of available instances.
public void ejbRemove();
This method is invoked by the container when a client invokes a remove operation on the Enterprise Bean. For entity beans with bean-managed persistence, this method should contain the JDBC code to remove the corresponding data in the database. For container-managed persistence, this method is called before the container removes the entity representation in the database.
public void ejbLoad();
The container invokes this method to instruct the instance to synchronize its state by loading it from the underlying database. For bean-managed persistence, the EJB provider should code at this location the JDBC statements for reading the data in the database. For container-managed persistence, loading the data from the database will be done automatically by the container just before ejbLoad is called, and the ejbLoad method should only contain some "after loading calculation statements."
public void ejbStore();
The container invokes this method to instruct the instance to synchronize its state by storing it to the underlying database. For bean-managed persistence, the EJB provider should code at this location the JDBC statements for writing the data in the database. For entity beans with container-managed persistence, this method should only contain some "pre-store statements," since the container will extract the container-managed fields and write them to the database just after the ejbStore method call.
The following examples are for container-managed persistence with EJB 1.1 and EJB 2.0. For bean-managed persistence, refer to the examples delivered with your specific platform.
package eb; import java.rmi.RemoteException; import javax.ejb.EntityBean; import javax.ejb.EntityContext; import javax.ejb.ObjectNotFoundException; import javax.ejb.RemoveException; import javax.ejb.EJBException; public class AccountImplBean implements EntityBean { // Keep the reference on the EntityContext protected EntityContext entityContext; // Object state public Integer accno; public String customer; public double balance; public Integer ejbCreate(int val_accno, String val_customer, double val_balance) { // Init object state accno = new Integer(val_accno); customer = val_customer; balance = val_balance; return null; } public void ejbPostCreate(int val_accno, String val_customer, double val_balance) { // Nothing to be done for this simple example. } public void ejbActivate() { // Nothing to be done for this simple example. } public void ejbLoad() { // Nothing to be done for this simple example, // in implicit persistence. } public void ejbPassivate() { // Nothing to be done for this simple example. } public void ejbRemove() { // Nothing to be done for this simple example, // in implicit persistence. } public void ejbStore() { // Nothing to be done for this simple example, // in implicit persistence. } public void setEntityContext(EntityContext ctx) { // Keep the entity context in object entityContext = ctx; } public void unsetEntityContext() { entityContext = null; } public double getBalance() { return balance; } public void setBalance(double d) { balance = balance + d; } public String getCustomer() { return customer; } public void setCustomer(String c) { customer = c; } public int getNumber() { return accno.intValue(); } } |
import java.rmi.RemoteException; import javax.ejb.EntityBean; import javax.ejb.EntityContext; import javax.ejb.ObjectNotFoundException; import javax.ejb.RemoveException; import javax.ejb.CreateException; import javax.ejb.EJBException; public abstract class AccountImpl2Bean implements EntityBean { // Keep the reference on the EntityContext protected EntityContext entityContext; /*==== Abstract set and get accessors for cmp fields ====*/ public abstract String getCustomer(); public abstract void setCustomer(String customer); public abstract double getBalance(); public abstract void setBalance(double balance); public abstract int getAccno(); public abstract void setAccno(int accno); /*================= ejbCreate methods ===================*/ public Integer ejbCreate(int val_accno, String val_customer, double val_balance) throws CreateException { // Init object state setAccno(val_accno); setCustomer(val_customer); setBalance(val_balance); return null; } public void ejbPostCreate(int val_accno, String val_customer, double val_balance) { // Nothing to be done for this simple example. } /*========= javax.ejb.EntityBean implementation =========*/ public void ejbActivate() { // Nothing to be done for this simple example. } public void ejbLoad() { // Nothing to be done for this simple example, // in implicit persistence. } public void ejbPassivate() { // Nothing to be done for this simple example. } public void ejbRemove() throws RemoveException { // Nothing to be done for this simple example, // in implicit persistence. } public void ejbStore() { // Nothing to be done for this simple example, // in implicit persistence. } public void setEntityContext(EntityContext ctx) { // Keep the entity context in object entityContext = ctx; } public void unsetEntityContext() { entityContext = null; } /** * Business method to get the Account number */ public int getNumber() { return getAccno(); } } |