Just as with BMP, CMP entity beans must respond appropriately in life cycle methods; however, due to the container's management of persistence, the timing and responses are often different. We saw in Entity Beans with Container-Managed Persistence that a CMP bean class must be declared abstract. As with BMP, the class must provide a public, no-arg constructor. The sequence of events (see The Entity Bean Life Cycle) is also the same.
Let's take a look at how the methods (first described in Entity Beans with Bean-Managed Persistence) work when using CMP:
-
public void setEntityContext(EntityContext ec) — Same as BMP.
-
public void unsetEntityContext() — Same as BMP.
-
public PrimaryKeyClass ejbCreate(...) — The method is now called before the actual creation mechanism executes. The code should only validate input arguments as necessary, and initialize the virtual fields with the input arguments using the setter methods. The return type is still the primary key type, but the return value should be null.
It is possible to write a bean without an ejbCreate() method method, when the class only deals with existing data. In this case, there must be no create() methods exposed on the home interface.
-
public void ejbPostCreate(...) — Same as BMP.
-
public <primary key type or Collection> ejbFind<METHOD>(...) — No finder methods are coded by the developer; all finder methods declared in the home interface are implemented by the container. All finder methods, except ejbFindByPrimaryKey(), are specified in the query deployment descriptor. The container uses the EJB Query Language string defined by the developer as the basis for the finder methods.
-
public void ejbLoad() — The container invokes this method immediately after reading from the datastore and setting the virtual fields. The only tasks the developer needs to do here would involve some sort of data transformation from the raw data kept in the datastore, or to recalculate/reset instance variables that depend on the persistent data.
-
public void ejbStore() — The container invokes this method before writing to the datastore. As with ejbLoad(), the only developer tasks to be performed here would have to do with data translation or preparation prior to persistence — for example, compressing text and invoking the appropriate setter method.
-
public void ejbRemove() — The container invokes this method before removing an entity from the datastore. The method can be used to perform any operations required before the data is deleted.
-
public void ejbActivate() — Same as BMP.
-
public void ejbPassivate() — Same as BMP.
-
public abstract type ejbSelect<METHOD>(...) — When using CMP entity beans, the developer can define internal query methods called select methods. The actual implementation is done at deployment time using vendor provided tools and EJB Query Language syntax.