What is an entity bean?

Richard Monson-Haefel

An entity bean is a type of enterprise bean; a type of EJB server-side component. Entity bean components implement the javax.ejb.EntityBean interface and can be container-managed (CMP) or bean-managed (BMP). Entity beans are designed to represent data in the database; they wrapper data with business object semantics and read and update data automatically.

Entity beans have identity, where each bean represents a unique set of data. An entity bean of type Customer, for example, would have thousands of identities -- one for each customer in the database. Entity beans can be safely shared by many clients, so that several applications can access the same bean identity at the concurrently.

A bean developer might choose to create a Customer bean to represent customer data in the database. The bean developer decides what data from the database represents a customer and how customers are uniquely identified. The bean developer also decides what business methods the Customer bean expose and what data can be changed or read. Below is the server-side component definition of a Customer bean.


public class CustomerBean implements javax.ejb.EntityBean {
      
         // persistent fields
         public long customerNumber;
         public Date lastActive;
         public String lastName;
         public String firstName;

         // initialization method for creating a new customer
         public CustomerKey ejbCreate(long id, PersonName name){
                   customerNumber = id;
                   setName(name);
                   lastActive = new Date();
                   return new CustomerKey(id);
        }
         
         // business methods
         public PersonName getName(  ){
                return new PersonName(firstName, lastName);
         }
         public void setName(PersonName name){
               lastName = name.getLastName();
               fistName = name.getFirstName();
         }
         public Date lastActive( ){
                    return lastActive;
         }
         ... 
}

Note: Only a subset of the bean code normally implemented is shown here for brevity. Ordinarily the bean would also implement special EJB specific callback methods that are not important to this FAQ entry.

The ejbCreate( ) method is used only once in the life type of a bean identity when its first created. In this case we initialize the bean with the customer's id number and name. The lastActive value is set within the method. The ejbCreate( ) creates and returns the customer bean's identity, which is called a primary key in EJB. A primary key is a object that represents a unique identifier for a bean. In the case of the Customer bean the primary key is the CustomerKey which is an object that wrappers the customer's id. Primary keys can be as simple as a String value or more be more complicated like a key that wraps several values that together represent a unique index to the bean identity. Below is the definition of the CustomerKey object.


public class CustomerKey implements java.io.Serializable {
          public long id;
          
          public CustomerKey(long l)
         { 
                   id = l;
         }
         public int hashCode( ) { 
                   return (int)id;
          }
          public boolean equals(Object otherKey){
                    if(otherKey instanceof CustomerKey)
                           return ((CustomerKey)otherKey).id == this.id;
                   else
                           return false;
          }
}

You will notice that the business methods do not exactly match the persistent fields. This illustrates that the persistent state of the bean need not map exactly to the business methods, which provides more flexibility in how components are designed. For example, instead of having an accessor for lastName and firstName, the Customer bean uses a serializable PersonName to pass this information to and from the bean's client. In addition, you will have noticed that the lastActive can only be read by a bean client. The bean itself is responsible for updating this value.

In addition to the business methods and the ejbCreate( ) methods there are a number of notification methods that the bean container (EJB server) uses to alert that bean that some significant event in its lifecycle is about to, or just has, occurred. Of particular importance are the two notification methods (ejbLoad( ) and ejbStore( )) used to alert the bean when its being synchronized with the database. The behavior of these methods changes depending on whether the bean is designed for BMP or CMP. This is investigated in more detail under the FAQ entries Java:API:EJB:EntityBean:BMP and Java:API:EJB:EntityBean:CMP.

Comment and Contribute

 

 

 

 

 


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