Internal Classes and Interfaces of an Entity Bean

In the previous section, you saw the hierarchy of the classes and interfaces of the different components of an entity bean. Now take a look at some of these different interfaces defined in the EJB specification. You'll skip the interfaces common in functionality that you saw earlier, while studying session beans. The interfaces that you need to focus on now are the ones that require specific changes for an entity bean.

javax.ejb.EJBHome Interface

The javax.ejb.EJBHome interface is the first object that a client application interacts with when a client application wants to use an EJB. The javax.ejb.Home interface contains methods that act as a factory for creating EJB bean object instances. Every home interface of an enterprise bean must extend the javax.ejb.EJBHome interface. A developer defines create() methods with different signatures in the home interface for its enterprise bean. The methods in the javax.ejb.EJBHome interface like remove(), remove(Handle), getHomeHandle() and getEJBMetaData() are implemented by the automatically generated classes of the EJB container provider/vendor (in this case the EJB container in the WebLogic application server). These classes are automatically generated during deployment of the EJB. The bean provider, in this case the developer, must define any additional methods in the home interface such as create() or find() methods, as required.

A client application obtains a reference to the home interface of the EJB by looking up the object reference using the JNDI API for naming and directory services. The bean provider/deployer must ensure that the home interface that will be used by client applications is registered in the JNDI. The client application then invokes methods in the home interface object reference to initiate the EJB instance's life cycle. Take a look at the methods in the javax.ejb.EJBHome interface:

  • create() The create() method of an entity bean is different from the create() method for session beans. With entity beans, the create() method provides a way for the client application to initialize the entity bean. It also inserts a row of data in the database schema represented by the entity bean. That's why the create() method of an entity bean takes the minimum number of parameters required to initialize the entity bean and then inserts a row of data with these parameters as the minimum columns required.

    Because the home interface is used by client applications to generate and create bean instances, the bean provider, that is, the developer, must define the create() methods with different signatures in the home interface in BMP. The EJB bean provider provides an implementation of the create() method in the bean class. For CMP, the container provider's tools generate the entity bean creation functionality.

  • findByPrimaryKey() The findByPrimaryKey() is the default finder method defined in the home interface of the entity bean. For entity beans using CMP, the container provider's tools will generate the functionality for this method. In BMP, the bean provider develops the functionality for the findByPrimaryKey() method.

  • findXXX() The findXXX() methods, called the finder methods, are defined in the home interface of the entity bean. Even though the actual implementation code of the findXXX() finder method is generated by the container provider's tools, the bean provider must still supply the query using the EJB Query Language (EJB QL). In BMP, the findXXX() methods must be implemented by the bean provider.

  • remove() For removing the PrimaryKey object of an entity bean, the overloaded remove() method is used. This overloaded method takes a PrimaryKey object as a parameter. Because PrimaryKey objects can be implemented by any entity beans, this remove() method takes a java.lang.Object as a parameter and enables any PrimaryKey object to be destroyed using this overloaded remove() method.

javax.ejb.EntityBean Interface

The javax.ejb.EntityBean interface is a design contract defined by the EJB specification between an entity bean and the EJB container. This means that in order for the EJB container to manage and interact with the entity bean, the entity bean must implement the methods in the javax.ejb.EntityBean interface. Because the entity bean guarantees implementation of the methods in the javax.ejb.EntityBean interface, (which the EJB container is aware of) the EJB container can invoke these methods. The methods in the javax.ejb.EntityBean interface are callback methods. Each of these methods is invoked by the EJB container at certain stages during the life cycle of an entity bean. Recall the methods that were listed for each stage in the section titled "Life Cycle of an Entity Bean." In that section, you saw that the methods ejbCreate(), ejbPostCreate(), ejbActivate(), ejbPassivate(), ejbLoad(), ejbStore(), ejbRemove() and setEntityContext() were invoked by the EJB container at different life-cycle stages. Now you will look at each of these methods in detail:

  • ejbLoad() A callback method invoked by the EJB container to maintain synchronization of the state of the entity bean with the database state. In CMP, the container provider's tool generates the JDBC code to interact with the database. In BMP, the bean provider writes the JDBC code for interacting with the database.

  • ejbStore() Invoked by the EJB container to synchronize the database state with the state of the entity bean. This is the opposite of what is done in the ejbLoad() method. In CMP, the container provider's tool generates the JDBC code to interact with the database. In BMP, the bean provider writes the JDBC code for interacting with the database.

  • ejbActivate() A callback method invoked by the EJB container when the bean is moved from the available pool of entity bean instances and is associated with the EJBObject. Because the bean moves from the pooled state to the ready state, the entity bean's state must also be synchronized with the database. Therefore, immediately after invoking the ejbActivate() method, the EJB container invokes the ejbLoad() method on the entity bean.

  • ejbPassivate() The opposite of the ejbActivate() method. The EJB container invokes the ejbPassivate() method when an entity bean is moved from the ready state back to the entity bean pool. Before moving the entity bean to the pool, the EJB container needs to synchronize the database with the current state of the entity bean. Immediately after invoking the ejbPassivate() method, the EJB container invokes the ejbStore() method in the entity bean.

After covering the basic concepts of entity beans, you have now gained a sufficient understanding of the internal workings of an entity bean. Through the remaining part of today, you will study the concepts of CMP and begin to understand how entity beans use it.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net