Components of an Entity Bean

Figure 12.4 shows the parts that compose the entity bean. Figure 12.5 depicts the interactions between these components and an entity bean client.

Figure 12.4. Class diagram of the different components of an entity bean.

graphics/12fig04.gif

Figure 12.5. Sequence diagram of client interaction with an entity bean.

graphics/12fig05.gif

Remote Interface

The remote interface of an entity bean defines the business methods required to access the data in the database. Client applications utilize these methods to retrieve business data as their requirements warrant. The remote interface of an entity bean also extends from the javax.ejb.EJBObject interface, which in turn extends from the java.rmi.Remote interface, thus guaranteeing remote invocation of the entity bean.

Note

For entity beans deployed in the same JVM of the EJB container, you can improve performance by defining the business methods in a local interface for the entity bean. A local interface of an entity bean extends from the javax.ejb.EJBLocalObject interface.


A code snippet follows:

 public interface MyRemoteIF extends javax.ejb.EJBObject  {        public void businessMethod1() throws javax.rmi.RemoteException        {        }        public String businessMethod2() throws javax.rmi.RemoteException        {        } } 

The preceding code snippet shows the entity bean's remote interface named MyRemoteIF that extends the javax.ejb.EJBObject interface. The entity bean provides two types of services represented by the methods businessMethod1() and businessMethod2(). Because the entity bean can be accessed remotely, all the methods in the entity bean's remote interface throw the java.rmi.RemoteException.

Home Interface

The home interface of an entity bean enables client applications to interact with an entity bean by creating, querying, finding, and removing the entity bean. Client applications for an entity bean can be Java classes or other EJBs. The home interface of an entity bean represents the client-view design contract. An entity bean's home interface extends the javax.ejb.EJBHome interface. The important differences between the methods of the home interface of an entity bean and the home interface of a session bean are the create() and finder methods. The create() method of an entity bean takes the parameters required to initialize the mandatory fields of an entity bean. The creation of an entity bean can also be carried out using other create methods as long as they have the prefix create in the method name. This is required because an entity bean represents data in the database which enforces constraints on the data based on business rules. Also, entity beans define the ability to retrieve data based on the primary key of the data and/or record in the database. For this purpose, a finder method called findByPrimaryKey() is present in the home interface. Other than this finder method, a bean provider should define other finder methods that have the prefix find.

The following code snippet shows the different methods for creating, finding, and removing an entity bean as defined in the home interface:

 public interface MyHomeIF extends javax.ejb.EJBHome  {        // create methods        public MyRemoteIF create(String param1, String param2, int param3)                throws javax.rmi.RemoteException, javax.ejb.CreateException        {        }        public MyRemoteIF createMyBusinessObject(String param1, String param2,                int param3) throws javax.rmi.RemoteException, javax.ejb.CreateException        {        }        // finder methods        public MyRemoteIF findByPrimaryKey(String paramKey)                throws javax.rmi.RemoteException, javax.ejb.FinderException        {        }        // remove method        public void remove(String paramKey) throws javax.rmi.RemoteException,                javax.ejb.RemoveException        {        } } 

Entity Bean Implementation Class

The bean implementation class of an entity provides the functionality for creating, updating, querying, and removing the entity bean. It also provides functionality for the business methods defined in the home and remote (or local) interfaces of the entity bean. The entity bean implementation class implements the javax.ejb.EntityBean interface as a part of the component design contract of the EJB specification. By implementing the javax.ejb.EntityBean interface, the entity bean implementation class guarantees that bean life-cycle methods such as ejbCreate(), ejbActivate(), ejbPassivate() and so on are implemented by the entity bean implementation class. It also guarantees that the EJB container will invoke these callback methods at different stages of the life cycle of the entity bean implementation class. Because the EJB specification defines that the client application may never obtain a direct handle to the entity bean, the entity bean implementation class never needs to implement the remote interface. The EJB container provider generates an intermediate implementation class that intercepts requests from the client application and delegates the business methods similar to the case for session beans. Take a look at the following code snippet, which demonstrates the different lifecycle methods of the bean implementation class:

 public interface MyBean implements javax.ejb.EntityBean  {        public void setEntityContext(javax.ejb.EntityContext)                throws javax.rmi.RemoteException        {               ... // functionality as required        }        // create methods        public MyRemoteIF ejbCreate(String param1, String param2, int param3)                throws javax.rmi.RemoteException, javax.ejb.CreateException        {        }        public MyRemoteIF ejbCreateMyBusinessObject(String param1, String param2,                int param3) throws javax.rmi.RemoteException,                javax.ejb.CreateException        {        }        public void ejbActivate() throws javax.rmi.RemoteException        {               ... // functionality implemented by container's tool        }        public void ejbPassivate() throws javax.rmi.RemoteException        {               ... // functionality implemented by container's tool        }        public void ejbRemove() throws javax.rmi.RemoteException        {               ... // functionality implemented by container's tool        }        // business methods        public void businessMethod1() throws javax.rmi.RemoteException        {               ... // business method functionality implemented by container's tool        }        public String businessMethod2() throws javax.rmi.RemoteException        {               ... // business method functionality implemented by container's tool        }        // finder methods        public MyRemoteIF findByPrimaryKey(String paramKey) throws                javax.rmi.RemoteException, javax.ejb.FinderException        {               ...// finder functionality implemented by container's tool        }        public MyRemoteIF findXXX(String param1, int param2...) throws                javax.rmi.RemoteException, javax.ejb.FinderException        {               ...// finder functionality implemented by container's tool        }        // select methods        abstract public MyRemoteIF ejbSelect(String param1, int param2 ...)                throws javax.ejb.FinderException        {            ...// select method functionality implemented by container's tool        }        // remove method        public void remove(String paramKey) throws javax.rmi.RemoteException,                javax.ejb.RemoveException        {        } } 

An additional class not built while developing a session bean but required when building an entity bean is the PrimaryKey class. Because entity beans represent data in a database, constraints such as the uniqueness of data also need to be modeled by the entity bean. To encapsulate this unique feature of an entity bean, you also need to define a PrimaryKey class. Now you'll take a quick look at the PrimaryKey class.

PrimaryKey Class

The PrimaryKey class of an entity bean contains all the fields of the entity bean that constitute the primary key of the record in the database. This is because a table can have a composite primary key made up of different fields in the table. The PrimaryKey class is a simple Java class with its only restriction being that it can be serialized, that is, it must implement the serializable interface. A code snippet follows:

 package com.sams.learnweblogic7.ejb.cmp;  import java.io.Serializable; public class MySAMSPK implements Serializable {        // define the elements of the composite key or single element key        public String myPkey;        public ItemPK()        {            this.myPkey = "";        }        public ItemPK(String inpKey){               this.myPkey = inpKey;        }        public int hashCode(){               return myPkey;        }        public boolean equals(Object that){               if(!(that instanceof ItemPK)){                      return false;               }               ItemPK tmp = (ItemPK)that;               return (this.myPkey == tmp.myPkey);        } } 

The preceding primary key class MySAMSPK implements the serializable interface. It contains the string variable myPKey, which is the primary key of the entity bean class. The MySAMSPK primary key class defines a default constructor as well as a constructor that accepts the primary key value to be initialized. The MySAMSPK primary key class also defines an object comparison method called equals() that performs an object content comparison, and it also defines the hashCode() method.

This completes your study of the different components of an entity bean. In the next section you will take a look at some of the EJB API classes and interfaces and how they are used for entity beans.



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