Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Session 7 Tp7


Published on

Published in: Technology
  • Be the first to comment

Session 7 Tp7

  1. 1. Container-Managed Persistent Entity Bean Session 7
  2. 2. Session Objectives <ul><li>Define a container-managed Persistent entity bean (CMP EJB) </li></ul><ul><li>Define a primary key class </li></ul><ul><li>Recognize the implementation guidelines needed to deploy a CMP EJB </li></ul><ul><li>Create and deploy multiple CMP EJBs </li></ul><ul><li>Describe EJB Query Language </li></ul>
  3. 3. Review <ul><li>All the BMPs have to implement the javax.ejb.EntityBean interface. </li></ul><ul><li>The setEntityContext() method is called when the container has to increase the pool size. </li></ul><ul><li>The unsetEntityContext() method dissociates the bean from its environment. It is called just before the instance of the entity bean is destroyed. </li></ul><ul><li>The bean-managed state fields are persistent fields, which load and store data in the database. </li></ul><ul><li>The ejb-jar.xml file and the jaws.xml files are packaged into a directory called META-INF. </li></ul><ul><li>The bean instance can service any number of finder methods while in the pool. </li></ul>
  4. 4. Container-managed Fields <ul><li>The container manages all the data access logic </li></ul><ul><li>Database operations are executed behind the scenes </li></ul><ul><li>Every field that has to be managed should follow the rules for java object serialization </li></ul><ul><li>In a CMP, the container can persist every field </li></ul><ul><li>Features of Container-managed fields are: </li></ul><ul><ul><li>EJB Container does the job of persistence </li></ul></ul><ul><ul><li>Container provides tools that generate code in the EJB class that maps methods in the bean to a result set. </li></ul></ul><ul><ul><li>Advanced features like connection pooling and caching become easy. </li></ul></ul><ul><ul><li>High level of reuse. </li></ul></ul>
  5. 5. Container-Managed Relationships (CMR) <ul><li>EJB 2.0 specifications support persistent relationships between enterprise beans in the same container </li></ul><ul><li>These relationships may be one-to-one, one-to-many or many-to-many </li></ul><ul><li>Each of the relationships can be uni-directional or bi-directional </li></ul><ul><li>Relationships are maintained as abstract bean fields that are accessed by public abstract get and set accessor methods in the enterprise bean class </li></ul><ul><li>An entity bean must provide a local client view which is used as the target for the CMR </li></ul>
  6. 6. Primary Key Class <ul><li>Has to be a serializable class, similar to the bean-managed persistent entity bean. </li></ul><ul><li>The fields present in the primary key class have to come from the container-managed fields of the entity bean class. </li></ul>
  7. 7. Introduction Implementing CMP EJB’s <ul><li>All the CMP EJBs have to implement the javax.ejb.EntityBean interface </li></ul><ul><li>The callback methods invoked on the bean by the container are defined by this interface. </li></ul><ul><li>Container‑managed Persistent Beans do not require the developer to provide data access logic. </li></ul><ul><li>Container provides the implementation to map the bean instances from and to data storage. </li></ul>
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. Remote Interface <ul><li>Implements the business methods of the entity bean </li></ul><ul><li>Extends the javax.ejb.EJBObject as all remote interfaces do </li></ul><ul><li>Throws RemoteException </li></ul><ul><li>Extends EJBObject </li></ul>
  13. 13. Local Interface <ul><li>Provides a separation between the entity bean class (defined by the bean developer) and its persistent representation </li></ul><ul><li>Extends from EJBLocalObject as opposed to the remote interface which extends from EJBObject </li></ul><ul><li>Contains the business methods of the bean </li></ul>
  14. 14. 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
  15. 15. LocalHome Interface <ul><li>This interface is implemented by the EJB container and extends EJBLocalHome </li></ul><ul><li>The implemented object is called the local home object and serves as a factory for EJB local objects </li></ul><ul><li>Contains create(), find(), and remove() methods which call the corresponding ejbCreate(), ejbFind(), and ejbRemove() methods of the bean. </li></ul>
  16. 16. Primary Key Class <ul><li>Implements Serializable interface </li></ul><ul><li>Must be serializable for container-managed persistence </li></ul>import; /** * The primary key class for the Item Container-Managed Entity * Bean */ public class ItemPK implements { . . . }
  17. 17. CMP Bean Class <ul><li>Extends the EntityBean class </li></ul><ul><li>Container-Managed fields are all public. </li></ul><ul><li>Implements the business methods of the bean </li></ul><ul><li>Implements the EJB-Required methods such as ejbCreate, ejbRemove </li></ul>
  18. 18. Client Class Client Class Getter/Setter methods NamingContextFactory Remote Interface Initial Context lookup() create() find() Property 1 Property 2 Property 3
  19. 19. The Deployment Descriptors- ejb-jar.xml file (1) <ul><li>Lists details of the </li></ul><ul><li>Bean class name </li></ul><ul><li>Type and name of the primary key </li></ul><ul><li>Home Interface name </li></ul><ul><li>Remote Interface </li></ul><ul><li>Persistence type </li></ul><ul><li>CMP fields </li></ul><ul><li>Query method names </li></ul>
  20. 20. The jaws.xml file <ul><li>JBoss uses jaws.xml file to access the database </li></ul><ul><li>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. </li></ul><ul><li>Lists the CMP field’s name, type and the database table-name </li></ul>
  21. 21. The JBoss.xml descriptor <ul><li>Provides information about the JNDI mapping names. </li></ul><ul><li>This name is used by the client to perform a JNDI lookup and the container to locate the particular bean. </li></ul>
  22. 22. 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.
  23. 23. Facts about CMP EJBs <ul><li>The container takes care of all system-level activities like performing data access logic </li></ul><ul><li>CMP reduces the size of the code as the developer can declare the logic declaratively </li></ul><ul><li>It is difficult for the developer to figure out the operations being performed behind the scenes by the container. </li></ul>
  24. 24. The EJB Query Language (EJB QL) <ul><li>The new EJB 2.0 specifications introduced a standard query language for declaring the behavior of find and select methods. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>The EJB QL relies on the cmp-fields and the cmr-fields of the related entity beans. </li></ul>
  25. 25. The Two Query Methods (1) <ul><li>There are mainly two types of query methods: find() methods and select() methods. </li></ul><ul><li>Find methods are used by client applications or beans for locating remote and local object references to specific entity beans. </li></ul><ul><li>A simple find() query can be shown as: </li></ul>public interface CustomerHomeLocal extends javax.ejb.EJBLocalHome { public CustomerLocal findByPrimaryKey(Integer primmaryKey) throws javax.ejb.FinderException; public Collection findByProductId() throws javax.ejb.FinderException; }
  26. 26. Query for a find() method <ul><li>The EJB Query for the sample findByProductId() method is as shown below. </li></ul><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>
  27. 27. The Two Query Methods (2) <ul><li>The select() method is used to select entity objects or other values derived from an entity bean’s abstract schema type. </li></ul><ul><li>A simple select() query can be shown as: </li></ul>public class AddressBean implements javax.ejb.EntityBean { public abstract String ejbSelectAreaCode() throws FinderException; }
  28. 28. Query for a select() method <ul><li>The EJB Query for the sample ejbSelectAreaCode() method is as shown below. </li></ul><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>
  29. 29. Summary (1) <ul><li>A container-managed persistent entity bean will let the container handle all the data access logic. </li></ul><ul><li>The container in a CMP can persist every field and it does this behind the scenes. </li></ul><ul><li>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. </li></ul><ul><li>In a container-managed persistent entity bean, the container manages the persistent operations. </li></ul><ul><li>The home interface of the bean defines the create() method. This is defined so that a new item can be created in the database. </li></ul><ul><li>The container-managed fields of the entity bean class have the primary key fields. </li></ul>
  30. 30. Summary (2) <ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>CMP entity beans help to develop persistent code, which results in rapid development of the application. </li></ul>