Download presentation
Presentation is loading. Please wait.
Published byAmberly Abigayle Briggs Modified over 8 years ago
1
14 Copyright © 2004, Oracle. All rights reserved. Achieving State Management in the Business Tier
2
14-2 Copyright © 2004, Oracle. All rights reserved. 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
14-3 Copyright © 2004, Oracle. All rights reserved. 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
14-4 Copyright © 2004, Oracle. All rights reserved. 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
14-5 Copyright © 2004, Oracle. All rights reserved. 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
14-6 Copyright © 2004, Oracle. All rights reserved.
7
14-7 Copyright © 2004, Oracle. All rights reserved. Implementing Methods in CMP Beans and BMP Beans ejbStore() ejbLoad() ejbcreate() setXXX() and getXXX() ejbFind() CMPBMPMethod abstract
8
14-8 Copyright © 2004, Oracle. All rights reserved.
9
14-9 Copyright © 2004, Oracle. All rights reserved. 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
10
14-10 Copyright © 2004, Oracle. All rights reserved. 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
11
14-11 Copyright © 2004, Oracle. All rights reserved. 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()
12
14-12 Copyright © 2004, Oracle. All rights reserved. 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;... }
13
14-13 Copyright © 2004, Oracle. All rights reserved. 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; }
14
14-14 Copyright © 2004, Oracle. All rights reserved. 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);...
15
14-15 Copyright © 2004, Oracle. All rights reserved. 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) { }...
16
14-16 Copyright © 2004, Oracle. All rights reserved. Bean Class: DepartmentsBean... public void ejbActivate() { } public void ejbLoad() { } public void ejbPassivate() { } public void ejbRemove() { } public void ejbStore() { } }
17
14-17 Copyright © 2004, Oracle. All rights reserved. Deployment Descriptor ejb-jar.xml The main tags of the deployment descriptor for a CMP bean are: : Type of primary key : Container : CMP 1.1 or 2.0 (default) : For each entity bean : List of CMP field names
18
14-18 Copyright © 2004, Oracle. All rights reserved.
19
14-19 Copyright © 2004, Oracle. All rights reserved. Deployment Descriptor ejb-jar.xml... Entity Bean ( CMP ) Departments mypackage1.DepartmentsHome mypackage1.Departments mypackage1.impl.DepartmentsBean Container java.lang.Long False 2.x...
20
14-20 Copyright © 2004, Oracle. All rights reserved. Deployment Descriptor ejb-jar.xml department_id department_name manager_id location_id department_id
21
14-21 Copyright © 2004, Oracle. All rights reserved. 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.
22
14-22 Copyright © 2004, Oracle. All rights reserved. Default Mapping of CMP Fields to 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
23
14-23 Copyright © 2004, Oracle. All rights reserved. Explicit Mapping of CMP Fields to Database Table Columns...
24
14-24 Copyright © 2004, Oracle. All rights reserved.
25
14-25 Copyright © 2004, Oracle. All rights reserved. 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;...
26
14-26 Copyright © 2004, Oracle. All rights reserved. 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());...
27
14-27 Copyright © 2004, Oracle. All rights reserved. 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) {...
28
14-28 Copyright © 2004, Oracle. All rights reserved. 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
29
14-29 Copyright © 2004, Oracle. All rights reserved. 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
30
14-30 Copyright © 2004, Oracle. All rights reserved.
31
14-31 Copyright © 2004, Oracle. All rights reserved.
32
14-32 Copyright © 2004, Oracle. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.