Entering content frame

Procedure documentation Using JDO with BMP Entity Beans Locate the document in its SAP Library structure

Use

The EJB standard defines two types of entity beans:

·        With container-managed persistence (CMP beans)

·        With bean-managed persistence (BMP beans).

CMP beans do not require manual coding of the persistence logic – the container manages the communication with the database and the data storing process.

BMP beans are required to define the entire logic of working with the database, which complicates their creation. JDO simplifies the development of BMP beans by providing a convenient way to implement persistence logic.

For more information, see Developing Entity Beans.

Prerequisites

Entity beans are required to use container-managed transaction demarcation.

Procedure

The life cycle of an entity bean is more complex than the life cycle of a session or message-driven bean. It consists of the following phases:

       1.      The container creates a new bean instance and invokes its setEntityContext() method to associate the instance to an entity context. Then the instance passes to a pooled state.

       2.      The instance transitions to a method-ready state after the container calls either the ejbCreate() and the corresponding ejbPostCreate() methods, or the ejbActivate() method on a previously passivated instance.

       3.      The ejbLoad() and ejbStore() methods are used to synchronize the data view in the instance with the one in the database.

       4.      An invocation of the ejbPassivate() or ejbRemove() methods returns the instance to the pool. However, the second method also destroys the entity object associated to the instance.

       5.      Finally, by invoking unsetEntityContext() the container removes the instance from the pool.

The JDO usage in a BMP entity bean is implemented following its life cycle. You must declare references to the bean’s entity context, to a PersistenceManagerFactory, to a PersistenceManager, and to one or more instances of persistence capable classes. The PersistenceManagerFactory is obtained in the setEntityContext() method:

Example

public class MyEntityBean implements EntityBean {

   EntityContext sc;

   PersistenceManagerFactory pmf;

   PersistenceManager pm;

   PCClass pcc;

public void setEntityContext(EntityContext ec) {

      this.ec = ec;

      try{

         Context ctx = new InitialContext();

         pmf = (PersistenceManagerFactory)ctx.lookup("java:comp/env/jdo/defaultPMF");

      }catch (Exception e){

         }

   }

...

 

The ejbCreate() method creates an entity object associated to the entity bean instance. In this method you must obtain an instance of the PersistenceManager, create a new instance of the referenced persistence capable class, which corresponds to the primary key of the bean and makes the JDO instance persistent. The ejbCreate() method for entity beans returns the primary key of the bean. For each ejbCreate() method you must also define an empty ejbPostCreate() method with return type voidand the same parameters as the ejbCreate().

Example

public class MyEntityBean implements EntityBean {

   private EntityContext ec;

   private MyEntityBeanPK primKey;

   PersistenceManagerFactory pmf;

   PersistenceManager pm;

   PCClass pcc;

   public String ejbCreate (int pccId){

      pm = pmf.getPersistenceManager();  

      pcc = new PCClass (pccId);

      pm.makePersistent(pcc);

      MyEntityBeanPK primKey = (MyEntityBeanPK) JDOHelper.getObjectId(pcc);  

      return primKey;

   }

   public void ejbPostCreate (int pccId){}

 

In the ejbLoad() method you should invoke PersistenceManager.getObjectById() with the primary key associated to the bean instance as the parameter. This retrieves the JDO instance with the data from the data store, which enables further modifications on the data. The JDO specification requires that this method be invoked in an active transaction. Therefore, it is called in ejbLoad() and not in ejbActivate(), which according to the EJB specification is not necessarily invoked within a transaction.

Example

public void ejbLoad(){

   if(pm==null || pm.isClosed())

   pm = pmf.getPersistenceManager();

   MyEntityBeanPK primKey = (MyEntityBeanPK) ec.getPrimaryKey();

   pm.getObjectById(primKey,true);

}

 

The ejbStore() method is used to synchronize the changed data in the JDO instance with the underlying data store. In the SAP J2EE Engine, the EJB Container invokes this method only once throughout the instance’s life cycle; therefore, you may choose to close and nullify the references to the PersistenceManager and to the JDO instances in this method. You can also do this in the ejbPassivate() method, which clears the association of the bean to a primary key and returns the instance to a pooled state.

Example

public void ejbPassivate() {

   pcc = null;

   pm.close();

   pm = null;

}

 

To delete the persistent instance in the data store, you can invoke PersistenceManager.deletePersistent() in the ejbRemove() method of the bean.

Example

public void ejbRemove() throws RemoveException {

   pm.deletePersistent(pcc);

   pcc = null;

   pm.close();

   pm = null;

}

 

Finally, in the unsetEntityContext() method of the bean you must nullify its reference to the PersistenceManagerFactory:

Example

public void unsetEntityContext() {

   pmf = null;

}

 

In each of the finder methods of an entity bean, you must acquire an instance of the PersistenceManager, because the methods might not be invoked within the scope of a single transaction. The following code presents a sample implementation of the ejbFindByPrimaryKey() method for MyEntityBean:

Example

public MyEntityBeanPK ejbFindByPrimaryKey(MyEntityBeanPK primKey) throws FinderException {

   PersistenceManager pm = null;

   try{

       pm  = pmf.getPersistenceManager();

       pcc = (PCClass)pm.getObjectById(primKey, true);

   }catch (Exception e){

      throw new FinderException(); 

   }finally {

      if (pm != null && !pm.isClosed())

      pm.close();

   }

   return primKey;

}

 

The business methods of the entity bean are implemented as accessors and modifiers (getter and setter methods). They work directly with the JDO instance:

Example

public String getName(){

   String name = pcc.getName();

   return name;

}

 

public void setName(String name){

   pcc.setName(name);

}

 

 

See also:

Working with Persistent Objects

 

 

Leaving content frame