Home > Articles > Programming > Java

Developing Entity Beans

This sample chapter will teach you some key points about working with Enterprise JavaBeans. Specifically, it will discuss developing CMP and BMP beans, and deploying and entity beans.
This chapter is from the book

This chapter is from the book

In This Chapter

  • A Closer Look at Entity Beans
  • Developing a CMP Bean
  • Developing a BMP Bean
  • Advanced Container-Managed Persistence
  • Transactions
  • Container Security
  • Design Guidelines

Entities have a singular purpose: to represent an entity of data from a given data source. This typically represents a record within a database. The beauty of this component is the capability to manipulate the data stored within a data store through a standard interface instead of having to send manipulation calls directly to the data source in any manner. This technique of wrapping manipulation to a data source is not new; you might know it as object to relational mapping.

This object mapping requires that an entity bean be responsible for inserting, updating, selecting, and removing data within the data source. This process of managing the communication between the component and the data source is called persistence. In other words, persistence is this process of writing the information to an external data source. As you discovered in Chapter 21, "An Architectural Overview of Enterprise JavaBeans," we defined two types of entity beans. Using the first entity type, the container manages all the persistence itself. This entity type is called container managed persistence (CMP). The other type informs the container that you, the developer, are required to manage all the manipulation to your given data source. This type is called bean managed persistence (BMP).

The most current version of entity beans is defined within the EJB 2.0 specification. The EJB 2.0 specification does not abandon support for 1.1 beans. There are a number of significant changes between 1.1 and 2.0, such that you cannot develop one bean that will work with both specifications.

As we work to understand Enterprise JavaBeans, and more specifically in this section entity beans, we will look at examples and even put them into practice. We will explore the following topics:

  • Developing CMP and BMP beans

  • Deploying entity beans

  • Using entity beans

A Closer Look at Entity Beans

As we look at an entity, you will see that it is an object-oriented representation of the data contained within a data source. This important feature allows you as a developer to easily represent data in a relational data source as an object within Java. Ultimately, Java requires data to be represented within an object footprint, and this makes the process simple.

It is important for you to understand that entity beans are a single representation of data, whereas a session bean can have multiple instances of the bean representing data. This implies an important difference between entity beans and session beans: The life span of a session bean is dependent on the life span of the client, whereas the life span of an entity bean is a relationship to data.

Bean Life Cycle

Unlike session beans, an entity bean survives beyond the client session using it. Even though the client creates an instance of an entity bean, the entity bean remains available until the container and only the container decides to remove it.

If we investigate the sequence diagram, shown in Figure 23.1, we can see that, as in the session bean, the entity's sequence diagram has a similar-looking state transition diagram (refer to Figure 22.2).

When the client invokes the create() method on the home interface, the bean is then created and placed within the pool of available beans. This pool contains all currently instantiated entity beans of this type. In the sequence diagram, you will see that the transition from Does not exist to pooled varies between BMP and CMP.

At some point, the container may decide to "garbage" collect pooled beans. The container calls unsetEntityContext() to deallocate the current associated entity of data. The EJB specification does not define the frequency or trigger for this action to execute, only that the bean must have the capability to accomplish this. In other words, different vendors trigger the garbage collector at different times. For example, Borland's Enterprise Server executes it on the length of inactivity of the bean.

If the client then invokes one of the finder methods, the bean finds the instance of the data in the data source, copies the primary key into the instance's member variables, and finally returns the bean to the pool. In many instances, this does not cause the entire instance to be loaded into the entity bean. This happens when the ejbLoad() method is called to synchronize the data with the data source.

Figure 23.1Figure 23.1 Client's perspective of the state of an entity bean.

What Are CMP and BMP Anyway?

The main goal for having two different types of entity beans is to give developers the ultimate flexibility for implementing entity beans as they see fit, but clients of the bean do not have to be aware or concerned with the implementation.

This is accomplished by giving you container managed persistence and bean managed persistence. First, container managed persistence addresses one of the main goals of Enterprise JavaBeans, which is to split work between the component developer and the container. The goals for container managed persistence are basic and important:

  • Free up the developer to work on business logic rather than persistence. This is accomplished by allowing the container to manage all the persistence and loading of data. For example, if you are developing an order entry system, you can communicate only to a data source using SQL, thus the communication is well defined and fairly repetitive except for the data context.

  • Persistence services can be developed by persistence specialists. In other words, this means that persistence is most likely going to run faster, with more transactional safety, and scale better. Using the EJB 2.0 specification has expanded greatly the performance and capabilities of the container managed persistence bean.

  • CMP beans are portable across EJB containers and data sources. EJB containers that support the EJB 2.0 specification facilitate the movement of an entity bean from one container to another rather seamlessly. In fact, some containers even attempt to work in specific database vendor features for performance and scalability but still allowing portability.

Finally, bean managed persistence is an alternative to using container managed persistence. Just as the name implies, it allows the bean to persist the instance in which the bean developer must implement. The goals of bean managed persistence are slightly more nebulous. For example, anything you cannot accomplish in a container managed persistence bean, you can accomplish in a bean managed persistence component. Some of these include the following:

  • You might have specific JDBC features that are required for your DBMS vendor. For example, you might need to accomplish your persistence with stored procedures or bulk inserts. Note: Some vendors have added stored procedure communication to container managed persistence.

  • You might have proprietary object-to-relational mapping provided by another tool. For example, TopLink is a popular object mapping tool that generates the code for bean managed persistence.

  • You might need to encapsulate communicating to a nonrelational data store. For example, you might want to communicate to a mainframe or mini using a proprietary data store for a given application.

It is unlikely that you cannot accomplish all of an application's persistence requirements using container managed persistence. The EJB 2.0 specifications include many new features for CMP beans that make BMP almost obsolete in most occasions.

EJB 2.0 Versus EJB 1.1

There were two large problems with the EJB 1.1 specification as it related to entity beans. The EJB 2.0 specifications focused on these two problems:

  • It was difficult to build coarse-grained entity beans.

  • Finder methods lacked portability.

The Enterprise JavaBean 2.0 specifications have provided a number of new features to fill some of the gaps found in the EJB 1.1 specification. Table 23.1 presents some of the problems found after many applications were written using the EJB 1.1 specification and how the 2.0 specification resolves them.

Table 23.1 EJB 1.1 Specification Versus EJB 2.0 Specifications

EJB 1.1 Problem

EJB 2.0 Solution

No good pattern to represent dependent objects within an entity bean

The introduction of local interfaces, which allow for modeling of dependent objects as another bean but still allow for good performance.

Load-on-demand and dirty checking of data

EJB 2.0 CMP has abstract methods available for the implementation of load-on-demand and dirty checking.

No portable way to ensure the contents of a collection within a container's persistence strategy

The EJB container maintains collection classes that simplify the object-to-relational mapping.

Data-aliasing contention created when two or more entities reference the same dependent object

Dependent objects are managed as entity objects by the container. The container then manages the state of the entity component. This ultimately allows for synchronization within the transaction with multiple entities being represented as multiple aliases.

No portable implementation of finder methods

A portable query language was implemented based on a subset of SQL.


With these new features, most of which have been added to container managed persistence, entity beans have become extremely powerful and efficient. A developer can simply model most if not all of the persistence requirements within JBuilder allowing you to work on your business methods rather than plumbing.

Primary Keys

As we have looked at entity beans to this point, the key consideration is that relational data is represented in the object world. Therefore, just as the relational world represents a unique instance of data with a primary key, you must do the same in the object world. This special class is called the primary key. Its sole purpose is to uniquely identify an instance of an entity bean. In other words, the primary key class contains all the information required to find an entity within the persistent data store. To accomplish this successfully, a number of rules must be followed:

  • The primary key instances must be any legal type in RMI/IIOP. In other words, it must be serializable.

  • It must provide a hashcode() and equals() method.

  • It must uniquely identify a single instance of the data stored within the persistent data store. For example, the primary key of the table is probably a good primary key for the entity.

  • In container managed persistence, the class must have a constructor that takes no arguments. Because the container is responsible for creating instances, it must have the capability to create a primary key class.

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 23.1).

Listing 23.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

  • Create (C)

  • Read (R)

  • Update (U)

  • Delete (D)

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020