August 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Developing Entity Beans

  • July 17, 2003
  • By Saleem Siddiqui, Michael Landy, & Jeff Swisher
  • Send Email »
  • More Articles »

The Interface

The beauty of entity beans is that, whenever you develop a CMP or BMP, the interface is the same to the client. This allows for the user of the bean to be isolated from the overall implementation of the bean. This is where session beans and entity beans differ greatly; a session bean has a method for each business method required. For CMP beans, you let the container provide the implementation of the interface, whereas with BMP beans you, as the developer, implement the interface. We'll now take a look at the interface provided for Enterprise JavaBeans (see Listing 1).

Listing 1 EntityBean Interface for Implementation by the Entity Bean

// JBuilder API Decompiler stub source generated from class file
// Jul 5, 2002
// -- implementation of methods is not available

package javax.ejb;

// Imports
import java.rmi.RemoteException;

public abstract interface EntityBean extends EnterpriseBean {

 // Methods
 void ejbActivate() throws EJBException, RemoteException;
 void ejbLoad() throws EJBException, RemoteException;
 void ejbPassivate() throws EJBException, RemoteException;
 void ejbRemove() throws RemoveException, EJBException, RemoteException;
 void ejbStore() throws EJBException, RemoteException;
 void setEntityContext(EntityContext entityContext) 
throws EJBException, RemoteException;
 void unsetEntityContext() throws EJBException, RemoteException;
}

In addition to implementation of the preceding interface, the bean developer is required to also implement an ejbCreate() and an ejbPostCreate() that correspond with each create() method signature within the home interface.

As you look at this interface, you will see that it supports the four basic operations required of any persistent data store; they are known as CRUD. The four basic operations you might want to perform on data are

Each of the four operations of a persistent data store is represented by a corresponding entity interface callback method. Create relates to ejbCreate() and ejbPostCreate(). Reading is implemented using ejbLoad(). Update is implemented using the ejbStore(), and finally delete is implemented using ejbRemove(). These callbacks are then managed by the container based entirely on the life cycle of the entity bean.

In addition to the CRUD requirements, you also have a few other methods to implement. If you compare the ejbActivate() and ejbPassivate() methods to that defined within a session bean, you will notice they are similar. By default, an entity bean saves its instance data to a secondary data store so that the requirements of activation and passivation are not as important. More importantly, it uses ejbActivate() as a notification mechanism when the entity bean's instance has been associated with a primary key class. ejbPassivate() is then called to notify the entity that the primary key is being disassociated with the primary key class and is available to another instance.

Finally, the last two methods are setEntityContext() and unsetEntityContext(). The setEntityContext() method allows a bean to access the bean's context. This is important when using entity beans because you use the context to associate the primary key to an instance. The unsetEntityContext() allows you to remove the allocation of resources.

About the Authors

Saleem Siddiqui is a technical architect and trainer with Dunn Solutions Group. He is also a Sun Certified Developer and a Borland Certified JBuilder Instructor. At Dunn, he provides consulting services and business application development.

Michael Landy is Director of Business Applications for Dunn Solutions Group directing the strategic solutions for clients.

Jeff Swisher is a technical manager for the Dunn Solutions Group business applications. He works as a programmer, trainer, architect, and instructional designer. He is a Sun Certified Java Developer and a Borland Certified Instructor in JBuilder.

Source of this material

This material is from Chapter 23: Developing Entity Beans from the book JBuilder Developer's Guide (ISBN: 0-672-32427-X) written by Saleem Siddiqui, Michael Landy, and Jeff Swisher, published by Sams Publishing.

To access the full Table of Contents for the book.


Other Chapters from Sams Publishing:

Web Services and Flows (WSFL)
Overview of JXTA
Introduction to EJBs
Processing Speech with Java
The Java Database Control in BEA Weblogic
Databases and Tomcat
Working with JAX-RPC
Understanding Servlets






Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel