• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Session 7 Tp7
 

Session 7 Tp7

on

  • 738 views

 

Statistics

Views

Total Views
738
Views on SlideShare
735
Embed Views
3

Actions

Likes
0
Downloads
45
Comments
0

1 Embed 3

http://itnlu.info 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Session 7 Tp7 Session 7 Tp7 Presentation Transcript

    • Container-Managed Persistent Entity Bean Session 7
    • Session Objectives
      • Define a container-managed Persistent entity bean (CMP EJB)
      • Define a primary key class
      • Recognize the implementation guidelines needed to deploy a CMP EJB
      • Create and deploy multiple CMP EJBs
      • Describe EJB Query Language
    • Review
      • All the BMPs have to implement the javax.ejb.EntityBean interface.
      • The setEntityContext() method is called when the container has to increase the pool size.
      • The unsetEntityContext() method dissociates the bean from its environment. It is called just before the instance of the entity bean is destroyed.
      • The bean-managed state fields are persistent fields, which load and store data in the database.
      • The ejb-jar.xml file and the jaws.xml files are packaged into a directory called META-INF.
      • The bean instance can service any number of finder methods while in the pool.
    • Container-managed Fields
      • The container manages all the data access logic
      • Database operations are executed behind the scenes
      • Every field that has to be managed should follow the rules for java object serialization
      • In a CMP, the container can persist every field
      • Features of Container-managed fields are:
        • EJB Container does the job of persistence
        • Container provides tools that generate code in the EJB class that maps methods in the bean to a result set.
        • Advanced features like connection pooling and caching become easy.
        • High level of reuse.
    • Container-Managed Relationships (CMR)
      • EJB 2.0 specifications support persistent relationships between enterprise beans in the same container
      • These relationships may be one-to-one, one-to-many or many-to-many
      • Each of the relationships can be uni-directional or bi-directional
      • Relationships are maintained as abstract bean fields that are accessed by public abstract get and set accessor methods in the enterprise bean class
      • An entity bean must provide a local client view which is used as the target for the CMR
    • Primary Key Class
      • Has to be a serializable class, similar to the bean-managed persistent entity bean.
      • The fields present in the primary key class have to come from the container-managed fields of the entity bean class.
    • Introduction Implementing CMP EJB’s
      • All the CMP EJBs have to implement the javax.ejb.EntityBean interface
      • The callback methods invoked on the bean by the container are defined by this interface.
      • Container‑managed Persistent Beans do not require the developer to provide data access logic.
      • Container provides the implementation to map the bean instances from and to data storage.
    • Implementation Guidelines for CMP EJB (1) Requests for resources needed by the instance are to be made. The bean will be in a pool and will not have any specific data in the database. This method associates the bean with context information. The bean will be able to access information about its environment. setEntityContext() There is no need to implement this method for CMP. The container handles the finding of data. This is why finder methods are not written for CMPs. ejbFind<…>(<…>) Database data should not be created in this method. It is used to check the client parameters. This method is responsible for creating new data in the database and initializing the fields of the bean instance. ejbCreate(<…>) Implementation Explanation Method
    • Implementation Guidelines for CMP EJB (2) When the instance of the bean has been associated with an EJB object, the container calls this method. This method is also used in order to reset some transaction-related parameters. For instance, a flag could be used in the bean to indicate if a field has been changed. For every ejbCreate() , there always has to be an ejbPostCreate. This method is called after every ejbCreate() method. ejbPostCreate(<…>) Bean-specific resources have to be acquired in this method. This method is used when the container wants to call a bean from the pool and get it ready. ejbActivate() Implementation Explanation Method
    • Implementation Guidelines for CMP EJB (3) Data from the database is read before this method is called. All the utilities that are needed to work with the data are performed here. This method is called to load the database data into the instance of the bean. ejbLoad() The EJB container will update the database automatically. The container–managed fields are prepared to be written to the database. This method is called to update the database with new values of the in-memory fields. ejbStore() Implementation Explanation Method
    • Implementation Guidelines for CMP EJB (4) Resources allocated should be released. This method will dissociate the bean from the environment. unsetEntityContext() All resources have to be released before the ejbPassivate () method is called. When the entity bean has to return the bean to the pool, this method is called. ejbPassivate() Perform all necessary functions before calling this method. This method is called to destroy the database data. ejbRemove() Implementation Explanation Method
    • Remote Interface
      • Implements the business methods of the entity bean
      • Extends the javax.ejb.EJBObject as all remote interfaces do
      • Throws RemoteException
      • Extends EJBObject
    • Local Interface
      • Provides a separation between the entity bean class (defined by the bean developer) and its persistent representation
      • Extends from EJBLocalObject as opposed to the remote interface which extends from EJBObject
      • Contains the business methods of the bean
    • Remote Home Interface Remote Home Interface create() find() Database New Row CreateException success Creates new row in the database failure FinderException failure success Returns new instance Or Enumeration Used by Client RemoteException
    • LocalHome Interface
      • This interface is implemented by the EJB container and extends EJBLocalHome
      • The implemented object is called the local home object and serves as a factory for EJB local objects
      • Contains create(), find(), and remove() methods which call the corresponding ejbCreate(), ejbFind(), and ejbRemove() methods of the bean.
    • Primary Key Class
      • Implements Serializable interface
      • Must be serializable for container-managed persistence
      import java.io.Serializable; /** * The primary key class for the Item Container-Managed Entity * Bean */ public class ItemPK implements java.io.Serializable { . . . }
    • CMP Bean Class
      • Extends the EntityBean class
      • Container-Managed fields are all public.
      • Implements the business methods of the bean
      • Implements the EJB-Required methods such as ejbCreate, ejbRemove
    • Client Class Client Class Getter/Setter methods NamingContextFactory Remote Interface Initial Context lookup() create() find() Property 1 Property 2 Property 3
    • The Deployment Descriptors- ejb-jar.xml file (1)
      • Lists details of the
      • Bean class name
      • Type and name of the primary key
      • Home Interface name
      • Remote Interface
      • Persistence type
      • CMP fields
      • Query method names
    • The jaws.xml file
      • JBoss uses jaws.xml file to access the database
      • When the bean is deployed, JAWS looks for the jaws.xml file and on locating it, reads it and configures the names of the database tables.
      • Lists the CMP field’s name, type and the database table-name
    • The JBoss.xml descriptor
      • Provides information about the JNDI mapping names.
      • This name is used by the client to perform a JNDI lookup and the container to locate the particular bean.
    • Creating a jar file Bean class Home interface class Remote interface class META-INF JAR FILE + The jar file has to be put into the deploy directory on the server.
    • Facts about CMP EJBs
      • The container takes care of all system-level activities like performing data access logic
      • CMP reduces the size of the code as the developer can declare the logic declaratively
      • It is difficult for the developer to figure out the operations being performed behind the scenes by the container.
    • The EJB Query Language (EJB QL)
      • The new EJB 2.0 specifications introduced a standard query language for declaring the behavior of find and select methods.
      • EJB QL resembles the standard Structured Query language (SQL) but it has been adapted to work with the abstract persistence schema of EJB 2.0.
      • Is optimizable and portable across databases and data schemas since it can be compiled to a target language, and the execution of queries is shifted to the native language facilities provided by the persistent store instead of executing them on the runtime representation of the entity beans state.
      • The EJB QL relies on the cmp-fields and the cmr-fields of the related entity beans.
    • The Two Query Methods (1)
      • There are mainly two types of query methods: find() methods and select() methods.
      • Find methods are used by client applications or beans for locating remote and local object references to specific entity beans.
      • A simple find() query can be shown as:
      public interface CustomerHomeLocal extends javax.ejb.EJBLocalHome { public CustomerLocal findByPrimaryKey(Integer primmaryKey) throws javax.ejb.FinderException; public Collection findByProductId() throws javax.ejb.FinderException; }
    • Query for a find() method
      • The EJB Query for the sample findByProductId() method is as shown below.
      <query> <query-method> <method-name>findByProductId</method-name> <method-params/> <query-method> <ejb-ql> SELECT OBJECT(c) FROM Customer c WHERE c.hasProductId = TRUE </ejb-ql> </query>
    • The Two Query Methods (2)
      • The select() method is used to select entity objects or other values derived from an entity bean’s abstract schema type.
      • A simple select() query can be shown as:
      public class AddressBean implements javax.ejb.EntityBean { public abstract String ejbSelectAreaCode() throws FinderException; }
    • Query for a select() method
      • The EJB Query for the sample ejbSelectAreaCode() method is as shown below.
      <query> <query-method> <method-name>ejbSelectAreaCode</method-name> <method-params> <method-param>java.lang.String<method-param> </method-params> <query-method> <ejb-ql> SELECT a.areacode FROM Address AS a WHERE a.areacode.STATE = 1 </ejb-ql> </query>
    • Summary (1)
      • A container-managed persistent entity bean will let the container handle all the data access logic.
      • The container in a CMP can persist every field and it does this behind the scenes.
      • The fields present in the primary key class have to come from the container-managed fields of the entity bean. This will allow the container to set and extract the entity bean’s primary key fields.
      • In a container-managed persistent entity bean, the container manages the persistent operations.
      • The home interface of the bean defines the create() method. This is defined so that a new item can be created in the database.
      • The container-managed fields of the entity bean class have the primary key fields.
    • Summary (2)
      • When the bean is deployed, JAWS will look for the jaws.xml file and on locating it, will read and configure the names of the database tables.
      • A jar file is created to package the three Java files, namely, the remote interface, the bean class, and the home interface. The jar file will build an archive, which will contain the three classes and the XML files. The XML files are present in the META-INF directory.
      • In CMP, the container performs persistence, and so it is very difficult for the developer to know the operations being performed. The container has to be decompiled, and only then will the code be available.
      • CMP entity beans help to develop persistent code, which results in rapid development of the application.