SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Creating Bean-Managed Persistence (BMP) Entity Beans

 Introduction

DATA is the heart of most business applications. In J2EE applications, entity beans represent the business objects that are stored in a database. For entity beans with bean-managed persistence, you must write the code for the database access calls.
Types of Enterprise Beans are
  • Session Beans
    1. Stateless Session Beans
    2. Stateful Session Beans
  • Entity Beans
    1.  Bean Managed Persistent (BMP) Entity Beans
    2. Container Managed Persistent (CMP) Entity Beans
  • Message-Driven Bean

 Life cycle of BMP Entity Beans

Life cycle of BMP Entity Beans:
  • The three stages in the life cycle of a BMP entity bean are:
    • Pooled
    • Ready
    • Does Not Exist
 The Pooled Stage

  • At the beginning of the life cycle, an entity bean is in the Pooled stage.
  • EJB container creates instances of an entity bean by invoking the newInstance() method. This method instantiates an entity bean by calling its default constructor.
  • EJB container invokes the setEntityContext() method, after instantiating the entity bean to associate the bean with context information.
  • The setEntityContext() method can also be used to allocate the resources that will be used by the instance of the entity bean until it moves to the Does Not Exist stage.
  • The entity bean, in the pooled stage can use the following methods:
    1. ejbCreate(): This method validates the arguments supplied by the client in order to initialize an entity bean. It can contain the SQL Insert statements for inserting data in a database
    2. ejbPostCreate(): This method passes the reference of an EJB object to other entity beans. It should have the same parameters as the ejbCreate() method and should have only one ejbPostCreate() method for each corresponding ejbCreate() method. The return type of the ejbPostCreate() must be void
    3. ejbFind<..>(): This method enables you to locate entity bean instances based on its primary key or fields. (For example, the ejbFindByPrimaryKey() method locates a bean instance based on its primary key. Similarly, ejbFindByAddress() or ejbFindByName() is used to locate entity bean instances based on its fields)
    4. ejbHome<..>(): This method performs operations that are not linked to a specific data instance. (For example, ejbHomeGetCustomers() method can contain the code to calculate the total number of customers in a bank. It is called when a client calls the home method from the local home interface or the home interface)
    5. unsetEntityContext(): This method allows you to unset the entity context. (In this method we can release the resources occurred by the entity bean using the setEntityContext() method. It destroys all references to the entity bean)
 The Ready Stage

  • An entity bean moves to the Ready stage to service the requests generated by a client.
  • EJB container calls the ejbCreate() and ejbPostCreate() methods to move the entity bean from the Pooled stage to the Ready stage. It can also use the ejbActivate() method to move the entity bean to the Ready stage from the Pooled stage. This ejbActivate() method can be used when there is no service at the start. Simply it is used to transfer to the Ready stage
  • The methods used in the Ready stage are:
    1. ejbLoad(): This method enables an entity bean instance to read data from a database.
    2. ejbStore(): This method transfers the modified information from the entity bean instance to a database.
  • A client moves an entity bean from the Ready stage to the Pooled stage by calling the remove() method.
  • EJB container then calls the ejbRemove() method that removes the information associated with the entity bean.
  • EJB container calls the ejbStore() method before calling the ejbPassivate() method . This ejbStore() method stores the recent state of the instance in the databasse
  • An entity bean can also move from the Ready stage to the Pooled stage when EJB container calls the ejbPassivate() method.
 The Does Not Exist stage

  • At the end of its life cycle, an entity bean is in the Does Not Exist stage.
  • In this stage, an entity bean is removed from the pool.
  • The unsetEntityContext() method can be implemented to destroy all resources acquired by an instance before removing it from the pool.
 EJB Entity Context

  • An enterprise bean has a context object that contains the information about its environment, such as security and transaction information.
  • In an entity bean, the javax.ejb.EntityContext interface contains the information about the environment of the entity bean.
  • Methods specified in the javax.ejb.EntityContext interface are:
    1. getEJBObject(): This method returns the REMOTE [component] interface of an entity bean.
    2. getEJBLocalObject(): This method returns the LOCAL [component] interface of an entity bean.
    3. getPrimaryKey(): Returns the primary key that is associated with an entity bean.
  • The EntityContext extends the javax.ejb.EJBContext interface. The EJBContext object is used in an entity bean to find the status of a transaction.
  • The methods in the javax.ejb.EJBContext interface are:
    1. getEJBHome(): This method returns the remote home interface of an entity bean.
    2. getEJBLocalHome(): This method returns the local home interface of an entity bean.
    3. getCallerPrincipal(): This method returns the java.security.Principal interface, which is used to find information about the caller of the instance of the entity bean.
    4. isCallerInRole(): This method checks if the caller of the instance of the entity bean instance is associated with a particular role.
    5. setRollbackOnly(): Enables an instance to mark a transaction for rollback.
    6. getRollbackOnly(): Enables an instance to check if a transaction is marked for rollback.
    7. getUserTransaction(): Returns the javax.transaction.UserTransaction interface. This method is used only in those session and message-driven beans, which implement bean-managed transactions.

 Click Next To Continue ...
<- PREVIOUSNEXT ->