1 / 26

Achieving State Management in the Business Tier

Learn about bean-managed persistent and container-managed persistent entity beans, their features, benefits, and how to develop and deploy them.

shriver
Download Presentation

Achieving State Management in the Business Tier

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Achieving State Management in the Business Tier

  2. Objectives • After completing this lesson, you should be able to do the following: • Describe the features of bean-managed persistent (BMP) entity beans • Identify the features of a container-managed persistent (CMP) entity bean • Describe the benefits of a CMP bean as compared with a BMP bean • Develop and deploy a CMP bean • Develop a client for a CMP bean

  3. Features of BMP Entity Beans • The Bean provider provides the code for persistence management. • BMP beans provide more flexibility in managing persistent data. • The bean class of a BMP entity bean contains code for data access and manipulation. • Callback methods contain code to map the entity bean instance to the data, load the data, and store the data in persistent storage. • BMP beans are less portable.

  4. Developing a BMP Entity Bean To develop a BMP entity bean, you must create the following: • A remote interface for the bean • A home interface • The primary key class • The bean class implementation • Support classes, such as exceptions • Necessary database tables • The J2EE deployment descriptor • A client application to access the EJB

  5. Features of CMP Entity Beans • Developers need not code the persistence logic in the bean because the containers provide it. • CMP beans provide: • Portability across all EJB-compliant containers • A layer of data independence • Container-managed fields, which represent the persistent fields in the database • Bean developers concentrate on the business logic and provide empty implementations of the callback methods.

  6. Implementing Methods in CMP Beans and BMP Beans Method BMP CMP ejbFind() ejbcreate() ejbLoad() ejbStore() setXXX() and getXXX() abstract

  7. Developing a CMP Entity Bean • To develop a CMP entity bean, you must: • Create a component interface • Create a home interface • Define a primary key for the bean • Create the bean class implementation • Create optional support classes such as exceptions • Create the bean deployment descriptor • Ensure that the correct database table exists for the bean

  8. CMP Bean: Example • The example in this lesson creates a CMP entity bean to represent the DEPARTMENTS table with the following components: • Remote interface: Departments • Home interface: DepartmentsHome • Bean class: DepartmentsBean • Deployment descriptor:ejb-jar.xml • Client for Departments bean: DepartmentsClient

  9. Bean Class of a CMP EJB: CMP Fields • The bean class of a CMP EJB is an abstract class because it does not implement persistence logic. • CMP fields in the deployment descriptor: • Correspond to columns of the persistent data table • Have names starting with a lowercase letter • Must be Java primitive types or Java serializable objects • CMP field accessor methods in the bean instance: • Must be abstract public methods; the container implements these methods • Must have names in the following form: getFieldname()or setFieldname()

  10. Remote Interface: Departments ... import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Departments extends EJBObject { // set and get methods for CMP fields Long getDepartment_id() throws RemoteException; void setDepartment_id(Long newDepartment_id) throws RemoteException; String getDepartment_name()throws RemoteException; void setDepartment_name(String newDepartment_name) throws RemoteException; ... }

  11. Home Interface: DepartmentsHome import javax.ejb.EJBHome; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.FinderException; import java.util.Collection; public interface DepartmentsHome extends EJBHome { Departments create() throws RemoteException, CreateException; Departments findByPrimaryKey(Long primaryKey) throws RemoteException, FinderException; Collection findAll() throws RemoteException, FinderException; Departments create(Long department_id, String department_name) throws RemoteException, CreateException; }

  12. Bean Class: DepartmentsBean ...import javax.ejb.EntityBean; import javax.ejb.EntityContext;public abstract class DepartmentsBean implements EntityBean { private EntityContext context; public void setEntityContext(EntityContext ctx) { context = ctx; } public void unsetEntityContext() { context = null; } public abstract Long getDepartment_id(); public abstract void setDepartment_id(Long newDepartment_id); ...

  13. Bean Class: DepartmentsBean ... public Long ejbCreate() { return null; }public void ejbPostCreate() { }public Long ejbCreate(Long department_id, String department_name) { setDepartment_id(department_id); setDepartment_name(department_name); return department_id; }public void ejbPostCreate(Long department_id, String department_name) { } ...

  14. Bean Class: DepartmentsBean ... public void ejbActivate() { } public void ejbLoad() { } public void ejbPassivate() { } public void ejbRemove() { } public void ejbStore() { } }

  15. Deployment Descriptor ejb-jar.xml • The main tags of the deployment descriptor for a CMP bean are: • <prim-key-class>: Type of primary key • <persistence-type>: Container • <cmp-version>: CMP 1.1 or 2.0 (default) • <abstract-schema-name>: For each entity bean • <cmp-field><field-name>: List of CMP field names

  16. Deployment Descriptor ejb-jar.xml ... <ejb-jar> <enterprise-beans> <entity> <description>Entity Bean ( CMP )</description> <display-name>Departments</display-name> <ejb-name>Departments</ejb-name> <home>mypackage1.DepartmentsHome</home> <remote>mypackage1.Departments</remote> <ejb-class> mypackage1.impl.DepartmentsBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.Long</prim-key-class> <reentrant>False</reentrant> <cmp-version>2.x</cmp-version>...

  17. Deployment Descriptor ejb-jar.xml <cmp-field> <field-name>department_id</field-name> </cmp-field> <cmp-field> <field-name>department_name</field-name> </cmp-field> <cmp-field> <field-name>manager_id</field-name> </cmp-field> <cmp-field> <field-name>location_id</field-name> </cmp-field> <primkey-field>department_id</primkey-field> </entity> </enterprise-beans> </ejb-jar>

  18. Mapping CMP Fields to Database Table Columns • Accept the defaults that are supplied by the container. • Map explicitly to the columns in a database table by using the orion-ejb-jar.xml file: • Deploy the application by using only ejb-jar.xml • Modify the generated orion-ejb-jar.xml file with the custom database, table, and column details • Use for more complex data representation of a bean • JDeveloper provides easy mapping of the persistent fields to database tables.

  19. Default Mapping of CMP Fieldsto Database Table Columns • Database: Default database as specified in the data-sources.xml file, which can be customized • Table: Default table is created by a container with a unique name that combines the following: • EJB name defined in the deployment descriptor • JAR file name including .jar extension • Application name defined during deployment • An underscore and five-character hash code • Example: DeptBean_dept_jar_dept_xxxxx • Column: With the same name as the CMP fields defined in the deployment descriptor; Java data types are translated to database data types in oracle.xml

  20. Explicit Mapping of CMP Fieldsto Database Table Columns <enterprise-beans> <entity-deployment name="Departments" data-source="jdbc/hrDS" table="DEPARTMENTS"> <primkey-mapping> <cmp-field-mapping name="department_id“ persistence-name="DEPARTMENT_ID" persistence-type="NUMBER(4)"/> </primkey-mapping> <cmp-field-mapping name="department_id“ persistence-name="DEPARTMENT_ID" persistence-type="NUMBER(4)"/> <cmp-field-mapping name="department_name“ persistence-name="DEPARTMENT_NAME“ persistence-type="VARCHAR2(30)"/> ...

  21. Client for Departments Bean import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; ... public class DepartmentsClient { public static void main(String [] args) { DepartmentsClient departmentsClient = new DepartmentsClient(); try { Context context = getInitialContext(); DepartmentsHome departmentsHome = (DepartmentsHome)PortableRemoteObject.narrow(context.lookup("Departments"), DepartmentsHome.class); Departments departments; ...

  22. Client for Departments Bean System.out.println(" CREATING A NEW DEPARTMENT WITH ID 404... ") ;departments =departmentsHome.create(new Long(404), "Security"); System.out.println(" SUCCESSFUL "); System.out.println("Get the DEPARTMENT_NAME " +departments.getDepartment_name());System.out.println("Changing the DEPARTMENT_NAME to Security Services " );departments.setDepartment_name("Security Services");System.out.println("PRINTING THE DEPARTMENT_ID AND DEPARTMENT_NAME");System.out.println(departments.getDepartment_id() + " " + departments.getDepartment_name()); ...

  23. Client for Departments Bean // Retrieve all instances using the findAll() method Collection coll = departmentsHome.findAll();Iterator iter = coll.iterator(); while (iter.hasNext()) { departments = (Departments)iter.next(); System.out.println("department_id = " + departments.getDepartment_id()); System.out.println("department_name = " + departments.getDepartment_name()); System.out.println("manager_id = " + departments.getManager_id()); System.out.println("location_id = " + departments.getLocation_id()); System.out.println(); } } catch(Throwable ex){...

  24. Summary • In this lesson, you should have learned how to: • Describe the features of a BMP entity bean • List the components that have to be created to develop a BMP bean • Describe the features of a CMP entity bean • List the steps in developing a CMP bean • Develop and deploy a CMP bean • Develop a client for CMP EJB

  25. Practice 14-1: Overview • This practice covers the following topics: • Developing a CMP entity bean to represent and update the EMPLOYEES table • Creating a remote interface, and a home interface for a CMP entity bean by using Oracle JDeveloper 10g • Implementing the create() methods, callback methods, and methods of the remote interface in the entity bean class • Testing the entity bean with a client application

More Related